Back to Error Center

MySQL server has gone away

Error Message

SQLSTATE[HY000] [2006] MySQL server has gone away

Cause

The MySQL connection was lost, usually due to the wait_timeout being exceeded. Common with long-running processes or idle connections.

Solution

Reconnect to the database by re-running the query. Increase wait_timeout in MySQL config. Use try-catch to handle disconnections gracefully.

Code Example

Fix
// Handle disconnection gracefully
try {
    $results = Post::all();
} catch (QueryException $e) {
    if (str_contains($e->getMessage(), 'gone away')) {
        DB::reconnect();
        $results = Post::all();
    } else {
        throw $e;
    }
}

// Increase timeout in my.cnf
wait_timeout = 28800
interactive_timeout = 28800

// Use reconnect in queue workers
public function handle()
{
    DB::reconnect();
    // Process job...
}

Debugging Steps

  1. Check MySQL wait_timeout setting
  2. Review for long-running queries
  3. Check queue worker connection handling
  4. Monitor connection lifecycle

Prevention

Use DB::reconnect() in long-running processes. Increase wait_timeout for applications with idle connections. Implement retry logic for transient failures.