Error
Error Code:
2006
MySQL Error 2006: Server Connection Lost
Description
Error 2006 indicates that the client lost its connection to the MySQL server, or the server terminated the connection unexpectedly. This often occurs due to timeouts, server restarts, network issues, or problems with large data transfers.
Error Message
MySQL server has gone away
Known Causes
4 known causesConnection Timeout
The client or server waited too long for a response, exceeding configured timeout values like `wait_timeout` or `net_read_timeout`.
Server Crash or Restart
The MySQL server process unexpectedly stopped or was restarted while the client had an active connection, causing the connection to be terminated.
Large Query or Packet
A query or result set was too large for the configured `max_allowed_packet` size, leading the server to drop the connection.
Network Instability
Intermittent network disruptions, firewalls, or routing issues between the client and the MySQL server caused the connection to be forcibly closed.
Solutions
4 solutions available1. Increase `wait_timeout` and `interactive_timeout` easy
Adjust server variables to prevent premature connection closure.
1
Connect to your MySQL server as a user with sufficient privileges (e.g., root).
2
Check the current values of `wait_timeout` and `interactive_timeout`.
SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'interactive_timeout';
3
If the values are low (e.g., 28800 seconds, which is 8 hours), consider increasing them. A common starting point for `wait_timeout` is 600 seconds (10 minutes) for web applications, or higher if you have long-running processes.
4
Dynamically set the values for the current session (this is a temporary fix and will revert on server restart).
SET GLOBAL wait_timeout = 600;
SET GLOBAL interactive_timeout = 600;
5
To make this change permanent, you need to modify the MySQL configuration file (e.g., `my.cnf` or `my.ini`). Locate the `[mysqld]` section and add or modify these lines.
[mysqld]
wait_timeout = 600
interactive_timeout = 600
6
Restart the MySQL server for the configuration changes to take effect.
2. Configure Client-Side Reconnection Logic medium
Implement automatic reconnection in your application code.
1
Identify the programming language and MySQL connector library your application uses.
2
Consult the documentation for your specific connector to find out how to implement automatic reconnection. Many libraries provide built-in options or require custom logic.
3
Example (conceptual, actual implementation varies by library): Wrap your database operations in a try-catch block. If a `MySQL server has gone away` error (or its equivalent in your language's exception hierarchy) occurs, attempt to reconnect to the database and retry the operation.
try {
// Perform database operation
} catch (MySQLGoneAwayException e) {
// Close existing connection
// Reconnect to database
// Retry database operation
}
4
Consider implementing a retry mechanism with a small delay to avoid overwhelming the server during transient network issues.
3. Adjust `max_allowed_packet` easy
Increase the maximum packet size if large queries are causing timeouts.
1
Connect to your MySQL server as a user with sufficient privileges.
2
Check the current value of `max_allowed_packet`.
SHOW VARIABLES LIKE 'max_allowed_packet';
3
If you are sending very large queries or receiving large results, this value might be too small. Increase it. A value of 64M (64 megabytes) is often a good starting point for larger datasets.
4
Dynamically set the value for the current session (temporary).
SET GLOBAL max_allowed_packet = 67108864; -- 64M in bytes
5
To make this change permanent, edit your MySQL configuration file (`my.cnf` or `my.ini`) in the `[mysqld]` section.
[mysqld]
max_allowed_packet = 64M
6
Restart the MySQL server.
4. Check Network Connectivity and Server Health medium
Diagnose underlying network or server issues.
1
Verify basic network connectivity between the client machine and the MySQL server. Use `ping` to check latency and packet loss.
ping <mysql_server_ip_or_hostname>
2
Check if the MySQL server process is running and healthy. On Linux, use `systemctl status mysql` or `service mysql status`. On Windows, check the Services console.
3
Examine MySQL error logs for any related issues. The log file location varies by operating system and installation, but is often found in `/var/log/mysql/error.log` on Linux or within the MySQL data directory on Windows.
4
Monitor server resources (CPU, memory, disk I/O). High resource utilization can lead to the server becoming unresponsive and dropping connections.
5
If using a proxy or load balancer (e.g., HAProxy, ProxySQL), check its configuration and health. Proxies can also have timeouts that cause connections to be closed.