VPS/VDS Hosting

Install MariaDB on a Linux VPS

Install and harden MariaDB on Ubuntu/Debian: user, test database, firewall and best practices for your apps.

intermediate~3 min read

MariaDB is a MySQL-compatible engine ideal for WordPress, panels and web apps on your Infrawire Linux VPS. This guide installs it, secures it and creates a dedicated database and user for one application.

MariaDB is a drop-in replacement for MySQL: most PHP, Python and Node.js applications connect to it with the same drivers and the same SQL. Use it when your application needs a relational database on the same server. If the data must be shared between several servers, or the VPS is very small and the app only stores a few records, a separate database host or SQLite may be a better fit.

📋 Prerequisites

  • Ubuntu 22.04+ or Debian 11+ Linux VPS with sudo
  • At least 1 GB RAM (2 GB recommended for production)
  • Open app ports in UFW if remote DB access is needed

Install MariaDB

The mariadb-server package installs the database server and its client. systemctl enable --now starts the service immediately and makes it start again after every reboot. Afterwards, check the version and the service state.

Bash
sudo apt update sudo apt install -y mariadb-server sudo systemctl enable --now mariadb
Bash
mariadb --version sudo systemctl status mariadb --no-pager

You should see active (running). On AlmaLinux and Rocky Linux the package has the same name and is installed with dnf; its configuration lives in /etc/my.cnf.d/ instead of /etc/mysql/.

Bash
sudo dnf install -y mariadb-server sudo systemctl enable --now mariadb

Secure the install

Set root password, remove anonymous users, disable remote root, drop test DB if unused. The script asks several yes/no questions and the safe answer is "yes" to all of them. On Debian and Ubuntu the database root account already authenticates through the local system account (unix_socket), so you can keep it that way: that is why sudo mariadb works without a password, and a root password would never be requested from a remote attacker.

Bash
sudo mariadb-secure-installation

Create database and user

Create one database and one user per application instead of using root in your code. utf8mb4 stores the full Unicode range, including emojis, and the collation controls sorting and comparison. The user is limited to localhost, and GRANT gives it rights on its own database only, which limits the damage if the application is compromised. Replace CHANGE_ME_STRONG_PASSWORD with a long random password and keep it in your application's configuration file, not in your shell history. FLUSH PRIVILEGES is harmless but not required after GRANT.

Bash
sudo mariadb -e "CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" sudo mariadb -e "CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'CHANGE_ME_STRONG_PASSWORD';" sudo mariadb -e "GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';" sudo mariadb -e "FLUSH PRIVILEGES;"

Verify

The command asks for the password of the myapp user and then lists the tables of the myapp database. An empty result with no error is correct for a new database: it proves that the login and the privileges work.

Bash
sudo mariadb -u myapp -p myapp -e "SHOW TABLES;"

Remote access and backups

By default MariaDB on Debian and Ubuntu listens on 127.0.0.1 only, which is what you want when the application runs on the same VPS. Confirm it with the command below; it should show 127.0.0.1:3306 and not 0.0.0.0:3306.

Bash
sudo ss -tlnp | grep 3306

If you must reach the database from your own computer, prefer an SSH tunnel such as the command below, then connect to 127.0.0.1:3306 locally. Exposing port 3306 to the internet requires changing bind-address in the server configuration, creating a user for a specific host and allowing only your IP in UFW; avoid it unless you really need it.

Bash
ssh -L 3306:127.0.0.1:3306 user@VPS_IP

Back up before every major change. mariadb-dump writes a consistent SQL file, and the second line restores it.

Bash
sudo mariadb-dump --single-transaction myapp > myapp.sql sudo mariadb myapp < myapp.sql # restore

Troubleshooting

  • ERROR 1698 (28000): Access denied for user 'root'@'localhost': run the client with sudo mariadb instead of mariadb -u root -p.
  • ERROR 1045 (28000): Access denied for user 'myapp': the password is wrong or the user was created for another host; list users with SELECT user, host FROM mysql.user;.
  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket: the service is not running; start it with sudo systemctl start mariadb.
  • The service stops on a small VPS: the kernel may have killed it for lack of memory, so add swap or lower the buffer settings.
  • The service refuses to start after a configuration change: read the last log lines.
Bash
sudo journalctl -u mariadb -n 50 --no-pager

Happy deploying! 🚀

Latest tutorials

Stay up to date with the latest offers, discounts and special promotions.

View promotions

We use cookies

We use cookies to measure and improve our site. Privacy policy