Port 3306 is the default port for MySQL and MariaDB. Applications connect to it to read and write data. It should not be reachable from the public internet.
- Port
- 3306
- Protocol
- TCP
- Service
- MySQL / MariaDB
- Expose to internet?
- No
- Also used by
- MySQL, MariaDB, Percona Server, TiDB (MySQL protocol)
Is port 3306 safe to open?
Public MySQL servers receive constant password-guessing against root and common usernames. A successful login exposes all data, and ransom gangs have wiped exposed databases and left a ransom note table. Distribution packages usually listen on localhost only, so a public 3306 is usually a deliberate configuration change.
How to secure MySQL / MariaDB
- Set
bind-address = 127.0.0.1(or a private IP) in my.cnf unless remote clients need access. - Restrict users to specific hosts (
'app'@'10.0.0.5') instead of'%'. - Remove anonymous users and the test database (
mysql_secure_installation). - Require TLS for remote connections with
require_secure_transport = ON. - Reach remote databases through an SSH tunnel or private network.
Check if port 3306 is open
Linux: what is listening on this port
sudo ss -tlpn | grep ':3306 'Windows (PowerShell): what is listening
Get-NetTCPConnection -LocalPort 3306 -State Listen | Select-Object LocalAddress, OwningProcess, @{n='Process';e={(Get-Process -Id $_.OwningProcess).Name}}From another machine: is it reachable
nc -zv HOST 3306
# or
nmap -p 3306 HOST
# Windows
Test-NetConnection HOST -Port 3306Test the MySQL / MariaDB service itself
mysql -h HOST -P 3306 -u USER -p -e "SELECT VERSION();"Restrict port 3306 in the firewall
Replace 10.0.0.0/8 with the network or address that needs access. Docker publishes container ports through its own iptables rules, which bypass ufw; bind published ports to 127.0.0.1 (-p 127.0.0.1:3306:3306) instead of relying on the host firewall.
ufw: allow only your private network, deny everyone else
sudo ufw allow from 10.0.0.0/8 to any port 3306 proto tcp
sudo ufw deny 3306/tcpfirewalld: allow one source network only
sudo firewall-cmd --permanent --remove-port=3306/tcp
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/8" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reloadiptables
sudo iptables -A INPUT -p tcp --dport 3306 -s 10.0.0.0/8 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3306 -j DROPWindows Firewall (PowerShell, as Administrator)
New-NetFirewallRule -DisplayName "Block 3306 from internet" -Direction Inbound -Protocol TCP -LocalPort 3306 -RemoteAddress Internet -Action BlockMonitor port 3306
Run a TCP port monitor against your public IP on port 3306: here, a failed connection is the healthy result, so you know the firewall is still doing its job. On Linux, the Statance agent also reports every listening port on the server. TCP port monitoring.
Related ports
FAQ
Should port 3306 be open to the internet?
No. Allow it only from your application servers or over a private network. For admin access use an SSH tunnel: ssh -L 3306:127.0.0.1:3306 user@host.
What is port 3306 used for?
Port 3306 is the default port for MySQL and MariaDB. Applications connect to it to read and write data. It is also used by MySQL, MariaDB, Percona Server, TiDB (MySQL protocol).
Is port 3306 TCP or UDP?
MySQL / MariaDB uses TCP on port 3306.
How do I check if port 3306 is open?
On the server, run sudo ss -tulpn | grep ':3306 ' (Linux) or Get-NetTCPConnection -LocalPort 3306 (Windows) to see what is listening. From another machine, run nc -zv HOST 3306 or Test-NetConnection HOST -Port 3306.
Know the moment a port stops answering.
TCP, HTTP, DNS and SSL checks from six regions, plus server metrics from a one-line agent.