Port 3306 (MySQL and MariaDB)

Port 3306 is the default port of the MySQL and MariaDB databases, which applications and admin tools use to connect to the database server, over TCP.

Key facts

Port
3306
Protocols
TCP
Range
Registered port, 1024-49151
IANA service name
mysql

IANA assigns ports in this range to services on request, under RFC 6335.

About port 3306

Port 3306 is the default port of the MySQL and MariaDB databases, which applications and admin tools use to connect to the database server, over TCP. Sites built with WordPress, Joomla, and Magento store their content in a database like this.

IANA registers port 3306 for the mysql service. MySQL also uses port 33060 by default for the X Protocol.

What port 3306 is used for

  • Site and database on the same server: the application connects to the database through localhost or a local socket file, with no need for a port open to the outside.
  • Separate database server: application servers connect to port 3306 on the database server, usually over a private network.
  • Admin tools: database management tools connect to port 3306, directly or through an SSH tunnel.
  • Replication: secondary database servers connect to the primary server on port 3306 to receive changes.

Port 3306 in the firewall

In most cases, there's no need to open port 3306 to the internet. When the site and the database are on the same server, keep the port closed in the firewall, and you can also set the database to listen only on 127.0.0.1 (the bind-address setting).

When outside access is needed, allow it only from specific IP addresses:

  • UFW: ufw allow from 203.0.113.10 to any port 3306 proto tcp
  • firewalld: firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'

For occasional work from your computer, an SSH tunnel lets you connect to the database without opening port 3306 at all.

Risks and security

A database open to the whole internet gets automated login attempts, just like any other exposed service. Limiting access in the firewall is the most important protection, along with a few simple habits:

  • Create a separate user for each site or application, with privileges only on its own database.
  • Define users by source address, for example 'app'@'10.0.0.5', and don't allow root to log in from any address.
  • When connecting from another computer over the internet, encrypt the connection with TLS or route it through an SSH tunnel.
  • Back up your databases regularly, and keep the backups off the server.

Commands to check and configure

  • ss -tlnp | grep ':3306 '

    Shows which address the database is listening on: 127.0.0.1 means local access only, and 0.0.0.0 or * means all addresses.

  • nc -zv db.example.com 3306

    Checks whether port 3306 on the database server is reachable from your computer.

  • mysql -h db.example.com -P 3306 -u appuser -p

    Connects to a remote database server as a specific user. The password is prompted for separately.

  • mysql -e "SHOW VARIABLES LIKE 'port';"

    Shows the port the database is set to listen on.

  • ssh -L 3307:127.0.0.1:3306 user@server.example.com

    Opens an SSH tunnel: the admin tool on your computer connects to 127.0.0.1 on port 3307, and the connection reaches the database on the server.

  • ufw allow from 203.0.113.10 to any port 3306 proto tcp

    Allows access to port 3306 only from the given IP address (UFW).

  • firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'

    Allows access to port 3306 in firewalld only from the given IP address. Then run firewall-cmd --reload.

Port 3306 in the IANA registry

Port Protocol Service IANA registry description
3306 TCP mysql MySQL
3306 UDP mysql MySQL

Data source: IANA Service Name and Transport Protocol Port Number Registry (opens in a new tab)

Port 3306 FAQ

Do I need to open port 3306 for my site to work?

No. When the site and the database are on the same server, the site connects to the database through localhost, and the port can stay closed in the firewall. Open port 3306 only when an application on another server needs to connect to the database, and only to that server's IP addresses.

How do I connect securely to the database on my server from my computer?

The recommended way is an SSH tunnel. Popular database tools support connecting over SSH, so the connection travels encrypted over port 22 and port 3306 stays closed to the internet. Another option is to allow access to port 3306 in the firewall only from your IP address, and encrypt the connection with TLS.

Do MySQL and MariaDB use the same port?

Yes. Both listen on port 3306 by default, and in most cases their clients and tools are also compatible with each other. You can change the port with the port setting in the database server's configuration file.

What is port 33060?

Port 33060 is the default port of the MySQL X Protocol, used by MySQL Shell and for working with documents (Document Store). Regular websites and applications use port 3306, so there's usually no need to open port 33060 to the internet.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement