Installing a database (MariaDB / MySQL)
Here's how to install a database for your website or bot on an Ubuntu server. MariaDB works almost the same way as MySQL, so you can use most MySQL resources as they are.
1. Install
sudo apt update
sudo apt install -y mariadb-server
sudo systemctl enable --now mariadb
2. Basic security settings
sudo mariadb-secure-installation
When the questions come up, answering as below is a safe choice.
- Remove anonymous users →
Y - Disallow root login remotely →
Y - Remove test database →
Y
3. Create a DB and an account just for your app
It's safer for your app to use an account just for that app instead of root.
sudo mariadb
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'long-complex-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Put these values in your app's settings.
- Host:
localhost(or127.0.0.1) / Port:3306 - DB name:
myapp/ Account:myapp/ Password: the value you set above
⚠️ Don't open your DB to the internet
If the DB port (3306) is open to the outside, automated login attempts come in from all over the world all day long. Especially if the root account has a short password, it gets broken into quickly.
- If your app and DB are on the same server, you don't need outside access. MariaDB on Ubuntu only accepts connections from inside the server (
127.0.0.1) by default, so leave it that way. - If you want to connect with a DB management program on your PC (HeidiSQL, DBeaver, etc.), don't open the port — use an SSH tunnel. Most programs have an "SSH tunnel" settings section, and you just enter your SSH connection info.
- If you run your DB with Docker, be sure to read the firewall warning in the Docker guide.
4. Backups
sudo mysqldump myapp > myapp-$(date +%F).sql
For regular backups and how to download them, see Backing up your server data.