How to Diagnose and Fix MySQL Error 2006 (CR_SERVER_GONE_ERROR)

If you’re a MySQL user, you might have encountered the Error 2006 (CR_SERVER_GONE_ERROR), commonly phrased as “MySQL server has gone away.” This error indicates that the client could not send a question to the server or the connection to the server was lost during the query. Several factors can cause this, from server timeouts to packet size issues. Let’s explore how to diagnose and resolve this error with practical examples and sample code.

Understanding the Error

Error 2006 occurs when the connection between the MySQL client and the server is interrupted. This can happen for various reasons:

  • The server timed out and closed the connection.
  • The server was restarted.
  • The connection was killed with the KILL command.
  • A large packet was sent to the server exceeding the max_allowed_packet size.

Diagnosing the Problem

The first step in diagnosing this error is to check the MySQL server logs. These logs can provide insights into why the server has gone away. Look for any error messages or notices about server restarts or killed connections.

Next, review your MySQL configuration settings, particularly wait_timeout and max_allowed_packet, as these are often related to Error 2006.

To check the current values for these settings, you can use the following SQL queries:

SHOW VARIABLES LIKE 'wait_timeout';
SHOW VARIABLES LIKE 'max_allowed_packet';

Fixing the Error

1. Adjusting wait_timeout

If the server is timing out due to inactivity, you can increase the wait_timeout value. This is the number of seconds the server waits for activity on a non-interactive connection before closing it.

To set a new value for wait_timeout, add the following to your MySQL configuration file (usually my.cnf or my.ini):

wait_timeout = 28800

After making changes, restart the MySQL server for the new settings to take effect.

2. Increasing max_allowed_packet

If the error is related to packet size, you can increase the max_allowed_packet value. This setting controls the maximum size of one packet or any generated/intermediate string.

To change max_allowed_packet, add the following to your MySQL configuration file:

max_allowed_packet = 64M

Again, restart the MySQL server after saving the changes.

3. Handling Server Restarts

If the server was restarted, ensure that your application has proper reconnection logic. Here’s a simple example in PHP:


do {
    $reconnected = false;
    try {
        $mysqli = new mysqli('host', 'user', 'password', 'database');
    } catch (mysqli_sql_exception $e) {
        if ($e->getCode() == 2006) {
            // Sleep for a while before trying to reconnect
            $reconnected = true;
        } else {
            throw $e;
} while ($reconnected);

4. Checking for Large Queries

If you suspect that a large query is causing the error, review your SQL statements and ensure they do not exceed the max_allowed_packet size. If necessary, break down large queries into smaller ones.


Error 2006 in MySQL can be a frustrating issue to deal with, but by understanding the common causes and knowing how to adjust your server’s configuration, you can minimize its occurrence. Always remember to back up your configuration file before making changes and to apply updates in a controlled environment to prevent disrupting your production system. With careful attention to server settings and application logic, you can maintain a stable and reliable connection to your MySQL server.

Leave a Comment