Skip to content

How to Fix MySQL Connection Timeout Errors

DodaTech 2 min read

In this tutorial, you'll learn about How to Fix MySQL Connection Timeout Errors. We cover key concepts, practical examples, and best practices.

The Problem

Your application connects to MySQL but the connection drops after a period of inactivity or under load:

ERROR 2006: MySQL server has gone away
ERROR 2013: Lost connection to MySQL server during query

These timeouts happen when the server closes idle connections or the network drops packets during long-running queries.

Quick Fix

Step 1: Check current timeout settings

mysql -u root -p -e "SHOW VARIABLES LIKE '%timeout%';"

Look for wait_timeout, interactive_timeout, and net_read_timeout. Default wait_timeout is often 28800 seconds (8 hours).

Step 2: Increase wait_timeout

SET GLOBAL wait_timeout = 600;
SET GLOBAL interactive_timeout = 600;

This sets idle connection timeout to 10 minutes. Adjust based on your application's needs.

Step 3: Increase max_allowed_packet

SET GLOBAL max_allowed_packet = 67108864;

Large queries or blob data can trigger "MySQL server has gone away" if the packet size exceeds the limit. This sets it to 64 MB.

Step 4: Check max_connections

SHOW VARIABLES LIKE 'max_connections';

If connection pool exhaustion is the issue, increase the limit:

SET GLOBAL max_connections = 500;

Step 5: Make changes permanent

Edit /etc/mysql/my.cnf or /etc/my.cnf:

[mysqld]
wait_timeout = 600
interactive_timeout = 600
max_allowed_packet = 64M
max_connections = 500
sudo systemctl restart mysql

Step 6: Test the connection

mysql -u appuser -p -h dbhost -e "SELECT 1 AS test;"

Expected:

+------+
| test |
+------+
|    1 |
+------+

Step 7: Check for network timeouts

ping -c 5 dbhost

If packet loss or high latency appears, the network layer needs investigation — MySQL settings alone will not fix it.

Alternative Solutions

Use connection pool libraries (HikariCP, SQLAlchemy) with connection validation and idle timeout shorter than the server's wait_timeout.

Common Errors

Changes reset after restart: SET GLOBAL changes are lost on MySQL restart. Always add the values to my.cnf for permanent configuration.

Too many connections: If you see "Too many connections" error, increase max_connections, but also check for connection leaks in your application code.

Socket timeout vs connection timeout: Distinguish between connect_timeout (time to establish a TCP connection) and wait_timeout (idle time before closing). Both may need tuning.

Firewall blocking port 3306: If MySQL is remote, ensure the firewall allows traffic: sudo ufw allow 3306/tcp.

DNS resolution delays: MySQL does a reverse DNS lookup on each connection. If DNS is slow, add skip-name-resolve to my.cnf under [mysqld] to skip DNS lookups.

Connection pool exhaustion from long-running queries: Even with high max_connections, a query that runs for minutes holds one connection. Use max_execution_time to limit query duration.

Prevention

  • Set wait_timeout to match your connection pool's max lifetime.
  • Monitor Threads_connected and Aborted_connects via SHOW GLOBAL STATUS.
  • Use read replicas to distribute query load.
  • Enable slow query logging to find queries that cause long-running connections.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro