How to Fix MySQL Connection Timeout
In this tutorial, you'll learn about How to Fix MySQL Connection Timeout. We cover key concepts, practical examples, and best practices.
The Problem
Your application cannot connect to MySQL and gets:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.100:3306' (110: Connection timed out)
Or:
SQLSTATE[HY000] [2002] Connection timed out
The client can reach the server's network but the MySQL port 3306 is not responding. This typically means MySQL is bound to localhost only, a firewall is blocking the port, or the server is overloaded.
Quick Fix
Step 1: Check if MySQL is running
sudo systemctl status mysql
# or
sudo systemctl status mariadb
Expected:
Active: active (running)
Step 2: Check the bind address
sudo grep bind-address /etc/mysql/mysql.conf.d/mysqld.cnf
If it shows bind-address = 127.0.0.1, MySQL only accepts local connections. Change it to:
bind-address = 0.0.0.0
Restart MySQL:
sudo systemctl restart mysql
Step 3: Check the firewall
sudo ufw status | grep 3306
If the port is not allowed, open it:
sudo ufw allow 3306/tcp
Step 4: Test the connection
mysql -h 192.168.1.100 -u myuser -p -e "SHOW DATABASES;"
Step 5: Increase connection timeout
If the server is slow, increase timeouts in my.cnf:
connect_timeout = 60
wait_timeout = 600
max_allowed_packet = 64M
Alternative Solutions
Use SSH tunneling if you cannot open the port:
ssh -L 3306:localhost:3306 user@mysql-server
mysql -h localhost -u myuser -p
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
Prevention
- Set
bind-address = 0.0.0.0if remote connections are needed. - Configure firewalls to allow MySQL only from specific IP ranges.
- Use a connection pool in your application to avoid connection storms.
- Monitor MySQL connection count:
SHOW STATUS LIKE 'Threads_connected';.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro