Linux Tutorials

How to Install MySQL on a VPS: Setup, Security, Remote Access and Backups

By NileVPS Team · Published · 7 min read

اقرأ المقال بالعربي

The database sits at the heart of most applications: online stores, dashboards, and sites built on WordPress or Laravel. Running MySQL on your own VPS gives you full control over configuration, performance and backups, without the limits of shared hosting. In this guide we install MySQL on Ubuntu 24.04 (with notes for Debian 12), secure it, create a database and a user with the smallest set of privileges it needs, open remote access safely, and finish with backups and basic performance tuning.

Before You Start

  • A VPS running Ubuntu 24.04 or Debian 12 with at least 1 GB of RAM (2 GB is more comfortable for a database). If you don't have a server yet, see our VPS plans.
  • A user with sudo privileges and a working SSH connection. If that is new to you, start with our guide on connecting to a Linux VPS over SSH.
  • Update the system first:
sudo apt update && sudo apt upgrade -y

Installing MySQL

On Ubuntu 24.04

MySQL 8.0 is available straight from the Ubuntu repositories:

sudo apt install -y mysql-server
sudo systemctl enable --now mysql
sudo systemctl status mysql --no-pager

Check the installed version:

mysql --version

An Important Note About Debian 12

The official Debian 12 repositories do not ship a mysql-server package. The default alternative is MariaDB, which is compatible with MySQL for common commands and queries:

sudo apt install -y mariadb-server
sudo systemctl enable --now mariadb

If you specifically need Oracle's MySQL on Debian, you can add the official MySQL APT repository from the MySQL website. The rest of this guide applies to both, and we will point out the small differences along the way.

Securing the Installation with mysql_secure_installation

Right after installing, run the hardening script:

sudo mysql_secure_installation

On MariaDB, use sudo mariadb-secure-installation instead. The script asks a few questions; here are the recommended answers:

  • VALIDATE PASSWORD component: enable it and choose the medium level if you want to enforce strong passwords.
  • Remove anonymous users: yes.
  • Disallow remote root login: yes, always.
  • Remove the test database: yes.
  • Reload privilege tables: yes.

On Ubuntu, the MySQL root user authenticates with auth_socket by default, which means it can log in without a password only when you are root on the system. So log in like this:

sudo mysql

This behavior is secure and sensible, so leave it alone unless you have a clear reason to change it. Your applications should never use root anyway; they get a dedicated user, as you will see next.

Creating a Database and a Least-Privilege User

The golden rule: every application gets its own database and its own user, and that user has privileges on that database only. Log in with sudo mysql and run:

CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'shop_user'@'localhost' IDENTIFIED BY 'put-a-strong-password-here';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, INDEX, DROP, REFERENCES ON shop.* TO 'shop_user'@'localhost';
SHOW GRANTS FOR 'shop_user'@'localhost';
EXIT;

A few things worth noting:

  • The utf8mb4 character set is essential for correctly storing non-Latin scripts such as Arabic, as well as emoji.
  • The @'localhost' part means this user can only connect from the server itself, which is exactly what you want when the application and the database live on the same VPS.
  • We avoided GRANT ALL PRIVILEGES ON *.* because it gives the user control over every database and over user management, a real risk if your application's connection details ever leak.
  • If the application does not need to change its table structure after installation, you can later revoke CREATE, ALTER and DROP.

Test the connection with the new user:

mysql -u shop_user -p shop

Accessing MySQL Remotely, Safely

The Best Option: an SSH Tunnel

If you only want to manage the database from your own computer with a tool like DBeaver or MySQL Workbench, do not open port 3306 to the world. Use an SSH tunnel instead:

ssh -N -L 3307:127.0.0.1:3306 [email protected]

Now point your tool at 127.0.0.1 on port 3307, and the connection travels encrypted over SSH without touching the MySQL configuration. Most database tools can also set up an SSH tunnel for you from their connection settings.

When Another Server Needs a Direct Connection

For example, an application on a separate web server connecting to a database on a dedicated database server. Here are the steps:

  1. Make MySQL listen on all interfaces. On Ubuntu, edit /etc/mysql/mysql.conf.d/mysqld.cnf; on MariaDB in Debian, edit /etc/mysql/mariadb.conf.d/50-server.cnf. Change this line:
bind-address = 0.0.0.0
  1. Restart the service:
sudo systemctl restart mysql
  1. Create a user restricted to the application server's IP address only:
CREATE USER 'shop_user'@'198.51.100.20' IDENTIFIED BY 'another-strong-password';
GRANT SELECT, INSERT, UPDATE, DELETE ON shop.* TO 'shop_user'@'198.51.100.20';
  1. Open the port in the firewall for that address only, using ufw:
sudo ufw allow OpenSSH
sudo ufw allow from 198.51.100.20 to any port 3306 proto tcp
sudo ufw enable
sudo ufw status numbered

Make sure SSH is allowed before enabling ufw so you don't lock yourself out. You can add a second layer with the cloud firewall in the NileVPS panel, so that no connection on port 3306 ever reaches the server unless it comes from the allowed address.

MethodSecurityWhen to use it
SSH tunnelVery highManaging the database from your own computer
Port open to one specific IPGood, with a firewallConnections between two servers with fixed IPs
Port open to everyoneWeakNever recommended

Backups with mysqldump

Manual Backup

sudo mysqldump --single-transaction --routines --triggers shop | gzip > shop-$(date +%F).sql.gz

The --single-transaction option takes a consistent snapshot of InnoDB tables without locking the database while your application keeps running.

Restoring

gunzip < shop-2026-11-22.sql.gz | sudo mysql shop

Automatic Daily Backups

Create a script at /usr/local/bin/mysql-backup.sh:

#!/bin/bash
set -euo pipefail
BACKUP_DIR=/var/backups/mysql
mkdir -p "$BACKUP_DIR"
mysqldump --single-transaction --routines --triggers shop | gzip > "$BACKUP_DIR/shop-$(date +%F).sql.gz"
find "$BACKUP_DIR" -name '*.sql.gz' -mtime +7 -delete

Then make it executable and schedule it as a root cron job:

sudo chmod 700 /usr/local/bin/mysql-backup.sh
echo '30 2 * * * root /usr/local/bin/mysql-backup.sh' | sudo tee /etc/cron.d/mysql-backup

This keeps the last seven days of backups. Remember that a backup stored on the same server won't help if the server itself fails, so copy it off the server regularly, and take a snapshot from the control panel before any major update as an extra safety net.

Basic Performance Tuning

The default settings are very conservative. Instead of editing the main configuration file, create a separate file for your changes: /etc/mysql/mysql.conf.d/zz-tuning.cnf on Ubuntu, or /etc/mysql/mariadb.conf.d/99-tuning.cnf on MariaDB:

[mysqld]
innodb_buffer_pool_size = 1G
max_connections = 100
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
  • innodb_buffer_pool_size: by far the most important setting. On a server dedicated to the database, set it to 50% to 70% of RAM. If the same server also runs a web server and PHP, stick to roughly 25% to 40%.
  • max_connections: don't raise it without a reason, because every connection consumes memory.
  • Slow query log: reveals the queries that need indexes. Fixing a single index is often worth more than any hardware upgrade.

After saving, restart the service and confirm the new value took effect:

sudo systemctl restart mysql
sudo mysql -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';"

FAQ

Should I use MySQL or MariaDB?

Both work well for most applications, including WordPress and Laravel. On Ubuntu, MySQL is readily available from the repositories, while Debian 12 ships MariaDB by default. Go with whatever your application recommends; if it has no preference, use the one in your distribution's repositories, since it receives security updates along with the rest of the system.

How much RAM do I need to run MySQL?

MySQL runs on 1 GB for small sites, but 2 GB or more leaves room for a larger buffer pool and noticeably better performance. If you see the server constantly using swap, that is a sign you need a bigger plan.

Why can't I connect to the database from my computer when the password is correct?

The most common causes are that bind-address is still set to 127.0.0.1, the user was created for localhost only, or port 3306 is closed in ufw or in the cloud firewall. In most cases the simplest and safest fix is an SSH tunnel rather than opening the port.

Are snapshots enough, or do I still need mysqldump?

A snapshot saves the entire server and is excellent before updates, but mysqldump gives you a copy of a specific database that you can restore on any other server, and you can keep several daily copies. The best approach is to use both.

Related guides