Port 3306
MySQL / MariaDB Database
Port 3306: MySQL Database
| Protocol | TCP |
| Service | MySQL / MariaDB |
| Encrypted | Optional (SSL/TLS) |
| IANA Status | Official |
What is Port 3306?
Port 3306 is the default port for MySQL and MariaDB database servers. Clients connect to this port to execute SQL queries and manage databases.
Connection Examples
# MySQL command line
mysql -h hostname -P 3306 -u username -p
# Connection string
mysql://user:password@localhost:3306/database
# Node.js
const connection = mysql.createConnection({
host: 'localhost',
port: 3306,
user: 'root',
password: 'secret',
database: 'myapp'
});
# Python
connection = mysql.connector.connect(
host="localhost",
port=3306,
user="root",
password="secret"
)
Security Best Practices
Never expose MySQL to the internet
Keep port 3306 firewalled. Connect via SSH tunnel or private network.
# /etc/mysql/mysql.conf.d/mysqld.cnf
# Bind to localhost only (recommended)
bind-address = 127.0.0.1
# Or specific private IP
bind-address = 10.0.0.5
# Restart MySQL
sudo systemctl restart mysql
SSH Tunnel for Remote Access
# Create SSH tunnel
ssh -L 3307:localhost:3306 user@server
# Connect via tunnel (uses local port 3307)
mysql -h 127.0.0.1 -P 3307 -u dbuser -p
# Keep tunnel open in background
ssh -fN -L 3307:localhost:3306 user@server
Troubleshooting
# Check if MySQL is running
systemctl status mysql
sudo ss -tlnp | grep 3306
# Check bind address
mysql -u root -p -e "SHOW VARIABLES LIKE 'bind_address';"
# Test connection
nc -zv localhost 3306
mysql -h hostname -P 3306 -u user -p
# Check MySQL logs
sudo tail -f /var/log/mysql/error.log
Common Issues
- Access denied: Check username, password, and host permissions
- Connection refused: MySQL not running or bound to wrong interface
- Too many connections: Increase max_connections or close leaked connections
- Host blocked: Too many connection errors, flush hosts
Grant Remote Access
# Only if you need remote access (not recommended)
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'%';
FLUSH PRIVILEGES;
# Better: limit to specific IP
CREATE USER 'username'@'10.0.0.5' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON database.* TO 'username'@'10.0.0.5';
FLUSH PRIVILEGES;
How to Check if Port 3306 is Open
Use these commands to verify MySQL connectivity from both client and server perspectives.
# From a remote machine
nc -zv db-server.example.com 3306
nmap -p 3306 db-server.example.com
mysql -h db-server.example.com -P 3306 -u user -p
# On the server — check if MySQL is listening
sudo ss -tlnp | grep 3306
sudo lsof -i :3306
sudo netstat -tlnp | grep 3306
# Check MySQL bind address
mysql -u root -p -e "SHOW VARIABLES LIKE 'bind_address';"
# Check firewall
sudo ufw status | grep 3306
sudo iptables -L -n | grep 3306
MySQL vs PostgreSQL Ports
Choosing between MySQL (port 3306) and PostgreSQL (port 5432) depends on your application requirements.
| Feature | MySQL (3306) | PostgreSQL (5432) |
|---|---|---|
| Default port | 3306 | 5432 |
| Wire protocol | MySQL protocol | PostgreSQL protocol |
| Auth config | User grants (SQL) | pg_hba.conf + user roles |
| SSL support | Optional | Optional (sslmode) |
Monitoring MySQL with UptimeSignal
A MySQL outage on port 3306 can cascade into 500 Internal Server Errors across your application. UptimeSignal can monitor your application endpoints that depend on MySQL, alerting you the moment database issues affect your users. Pair application monitoring with TCP port checks for complete visibility into your PostgreSQL and Redis infrastructure.
Frequently Asked Questions
What runs on port 3306?
Should I expose MySQL port 3306 to the internet?
127.0.0.1 in mysqld.cnf and use SSH tunnels, VPNs, or private networking for remote access. If you must allow remote connections, restrict to specific IPs in both MySQL grants and firewall rules.
How do I connect to MySQL via SSH tunnel?
ssh -L 3307:localhost:3306 user@server, which maps your local port 3307 to the remote MySQL port 3306. Then connect via mysql -h 127.0.0.1 -P 3307 -u dbuser -p. Add -fN flags to run the tunnel in the background.
Why is my MySQL connection on port 3306 being refused?
systemctl status mysql), it's bound to 127.0.0.1 and you're connecting remotely, the firewall is blocking port 3306, or the max_connections limit is reached. Check /var/log/mysql/error.log for details. See our connection refused guide.
What is the MySQL "Too many connections" error?
max_connections is 151. Fix by: increasing max_connections in mysqld.cnf, closing leaked connections in your application, using connection pooling (like PgBouncer for PostgreSQL or ProxySQL for MySQL), or identifying long-running queries with SHOW PROCESSLIST.