Skip to content
Log In Sign Up info@muvcloud.com

First: Do You Need It?

Ubuntu's MySQL and Debian's MariaDB listen on 127.0.0.1 only, and that is the safe default. A database port that the internet can reach receives password guessing all day, and one weak account exposes all its data. Never open 3306 to everyone.

  • The application runs on the same server: it connects to localhost. Nothing to change.
  • You want a database tool on your own computer: use an SSH tunnel. No port is opened.
  • Another server must connect: follow the steps below, limited to that one server.

The SSH Tunnel

ssh -L 3307:127.0.0.1:3306 user@your_server_ip

While the session is open, a client on your computer connects to 127.0.0.1 port 3307 and reaches MySQL through SSH, as an account at localhost or 127.0.0.1 on the server. Options and background are in SSH port forwarding.

Step 1: Bind to a Private Address

If both servers share a private network, use the database server's private address, here 10.0.0.2. On Ubuntu with MySQL, edit /etc/mysql/mysql.conf.d/mysqld.cnf; on Debian and Ubuntu with MariaDB, /etc/mysql/mariadb.conf.d/50-server.cnf. Under [mysqld]:

bind-address = 10.0.0.2

MySQL 8.0.13 and later, and MariaDB 10.11 and later, accept several addresses separated by commas, such as 127.0.0.1,10.0.0.2, so local TCP clients keep working. Local socket connections work either way. Avoid 0.0.0.0, which listens on every interface, including the public one. Then restart and check:

sudo systemctl restart mysql
sudo ss -tlnp | grep 3306

On MariaDB, restart mariadb instead.

Step 2: An Account for One Source Address

Inside the client as an administrator, with the application server's address, 10.0.0.5:

CREATE USER 'app_user'@'10.0.0.5' IDENTIFIED BY 'STRONG_PASSWORD' REQUIRE SSL;
GRANT SELECT, INSERT, UPDATE, DELETE ON app.* TO 'app_user'@'10.0.0.5';

Use the exact address rather than %, and rights on one database only, as explained in how to create a MySQL user. REQUIRE SSL refuses the login unless the connection is encrypted.

Step 3: Allow Only That Address in the Firewall

On Ubuntu and Debian with ufw:

sudo ufw allow proto tcp from 10.0.0.5 to any port 3306

On AlmaLinux and Rocky Linux with firewalld:

sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.5" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload

If your provider's control panel has a firewall in front of the server, limit the rule there to the same address.

Step 4: Check TLS

MySQL 8.0 and later create a self-signed certificate in the data directory at first start, so TLS is available without setup. MariaDB 11.4 and later, as shipped with Debian 13 and Ubuntu 26.04, also enable TLS by default. On older MariaDB releases, check with SHOW VARIABLES LIKE 'have_ssl';; if it says DISABLED, set ssl_cert and ssl_key in the server configuration first. From the application server:

mysql -h 10.0.0.2 -u app_user -p --ssl-mode=REQUIRED

--ssl-mode is an option of the MySQL client; the MariaDB client uses --ssl and, from 11.4, checks the server certificate by default. Inside the session, SHOW SESSION STATUS LIKE 'Ssl_cipher'; shows a cipher name when the connection is encrypted. For certificate checks against your own certificate authority, configure the CA file on the client.

Troubleshooting

  • Connection timed out: a firewall drops the packets. Test the port with nc: nc -zv 10.0.0.2 3306.
  • Connection refused: MySQL does not listen on that address; check bind-address and the ss output.
  • Access denied: the account's host does not match the address the server sees; see MySQL access denied for user.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations