PortGate

Resources · Port reference · port 3306

Port 3306 (MySQL and MariaDB): what it is and how to secure it

MySQL and MariaDB on port 3306 belong on localhost. A public 3306 invites password guessing against your database.

Check command

sudo ss -tlnp | grep 3306

Copied.

Port 3306 is MySQL and MariaDB. If your app runs on the same server, the database should only listen on 127.0.0.1. A public 3306 gets constant password guessing, and one weak or reused password exposes everything.

What port 3306 is for

MySQL and MariaDB accept connections on 3306 from apps such as WordPress, Laravel and Magento, and from tools such as phpMyAdmin, TablePlus and MySQL Workbench. Ubuntu and Debian packages listen on 127.0.0.1 by default.

Why a public 3306 is dangerous

  • Bots try common users (root, admin, wordpress) with leaked and default passwords all day.
  • Accounts created as 'user'@'%' accept logins from any address once the port is open.
  • With a valid login and the FILE privilege, an attacker can read files the database can read, and write files into some directories.
  • Old servers also carry known authentication bugs that only matter when the port is reachable.

Check yours

sudo ss -tlnp | grep 3306
sudo grep -rs "bind-address" /etc/mysql/
sudo mysql -e "SELECT user, host FROM mysql.user;"

bind-address = 127.0.0.1 and hosts of localhost or a private IP are what you want. Users with host % can log in from anywhere if the port is public. From another machine:

nc -zv YOUR_SERVER_IP 3306

"succeeded" or "open" means it is public.

Fix it

Set the bind address. For MySQL, edit /etc/mysql/mysql.conf.d/mysqld.cnf; for MariaDB, /etc/mysql/mariadb.conf.d/50-server.cnf:

[mysqld]
bind-address = 127.0.0.1
sudo systemctl restart mysql   # or: sudo systemctl restart mariadb

Then tighten the users. Remove % accounts you do not need, and give the app a user tied to localhost with access to its own database only:

CREATE USER 'app'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'localhost';
DROP USER 'olduser'@'%';

For Docker, publish on loopback, or do not publish if only containers use it:

services:
  db:
    image: mysql:8.4
    ports:
      - "127.0.0.1:3306:3306"

If you need remote access

Most database tools have an SSH tunnel option built in. From a terminal:

ssh -L 3306:127.0.0.1:3306 you@YOUR_SERVER_IP

For an app on another server, bind to the private network address, allow only that server in the firewall, and create the user as 'app'@'10.0.0.5' rather than '%'.

Common mistakes

  • bind-address = 0.0.0.0 so a desktop tool can connect. Use the tool's SSH tunnel setting instead.
  • phpMyAdmin on a public URL with no extra login. It is a second door to the same database.
  • Docker publishing 3306. UFW does not filter Docker-published ports.

FAQ

Is changing the port to something other than 3306 enough? No. Scanners check every port. Bind to localhost.

Does a strong root password make a public port safe? It reduces the risk, but every other user and every future bug still faces the internet.

How will I know if it opens again? PortGate's Server Guard alerts when 3306 listens on a public address, including through Docker.