MySQL server has gone away
Production Risk
HIGH — causes query failures in long-running or pooled applications.
Client error 2006 is returned when the server closes the connection while the client still has it open. This is typically caused by the connection being idle longer than wait_timeout, a query result larger than max_allowed_packet, or the server being restarted.
- 1Connection was idle longer than wait_timeout (default 8 hours) and the server dropped it
- 2Query or result set exceeds max_allowed_packet — server closes connection on oversized packet
- 3Server was restarted while the connection was open
- 4Network interruption between client and server
- 5Application is reusing a connection from a pool that was silently closed by the server
Executing a query on a connection that the server has already closed.
-- After a long idle period: SELECT * FROM large_table;
expected output
ERROR 2006 (HY000): MySQL server has gone away
Fix 1
Increase wait_timeout and interactive_timeout
WHEN When connections are legitimately idle for extended periods.
SET GLOBAL wait_timeout = 28800; -- 8 hours SET GLOBAL interactive_timeout = 28800; -- Permanent setting in my.cnf: -- wait_timeout = 28800
Why this works
wait_timeout controls how long the server keeps an idle non-interactive connection alive. Raising it reduces the frequency of disconnects for long-running application processes.
Fix 2
Enable connection pool reconnection / ping
WHEN In applications using a connection pool (most frameworks).
-- PDO example — enable persistent connections with ping: $pdo = new PDO($dsn, $user, $pass, [ PDO::ATTR_PERSISTENT => true, PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', ]); -- Most ORMs have a reconnect option; enable it.
Why this works
Connection pools should validate (ping) connections before reuse. MariaDB Connector/J, MySQL Connector/J, and most ORMs support automatic reconnection.
Fix 3
Increase max_allowed_packet for large queries
WHEN When the error occurs with large INSERT or BLOB operations.
SET GLOBAL max_allowed_packet = 67108864; -- 64 MB -- Permanent in my.cnf: -- max_allowed_packet = 64M
Why this works
max_allowed_packet limits the maximum size of one network packet. Queries or results larger than this cause the server to drop the connection.
✕ Set wait_timeout to a very high value to mask connection leaks
Idle connections consume server resources and thread slots; fix pool configuration instead.
MySQL Client error 2006 / CR_SERVER_GONE_ERROR
MariaDB wait_timeout ↗MariaDB max_allowed_packet ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev