← Debugging Guides

阅读中文版本

MySQL — MySQL server has gone away during a query

Fix "server has gone away" by checking max_allowed_packet, wait_timeout and connection health under load.

intermediatedebuggingmysqlnetworking

Symptoms

  • MySQL server has gone away (error 2006)
  • The failure happens on large inserts or long-running queries
  • An idle connection drops after a period of inactivity
  • It occurs after a load balancer recycles connections

Possible causes

  • The packet exceeded max_allowed_packet so the server closed the connection
  • The connection was idle beyond wait_timeout and the server closed it
  • A proxy or firewall dropped an idle TCP connection
  • The server restarted or crashed mid-query

Diagnosis steps

Check packet and timeout settings

mysql -e "show variables like 'max_allowed_packet'; show variables like 'wait_timeout'"

Interpretation

Small packet limits and short wait timeouts explain large-query and idle-connection failures respectively.

Size the payload

ls -lh big_dump.sql

Interpretation

Comparing the payload against max_allowed_packet shows whether it is too large to send in one statement.

Add connection validation

grep -rn "testOnBorrow\|pool_reset\|ping" src config

Interpretation

Pools that validate or revive connections before use avoid handing out a connection the server already closed.

Root cause

The connection was closed by the server or an intermediary, either because a packet exceeded the allowed size, the connection idled past its timeout, or the server restarted, so the client found the socket gone mid-query.

Fix

Raise max_allowed_packet or split large statements, increase or refresh idle connections before wait_timeout, and configure the pool to validate connections so stale ones are replaced.

Verify the fix

Rerun the large insert or idle scenario and confirm it completes, and confirm the pool replaces closed connections rather than reusing them.

Symptom walkthrough

A query or insert fails with MySQL server has gone away, and it tends to happen on large payloads or after periods of inactivity. The server or an intermediary closed the connection, so the client discovers the dead socket only when it tries to use it. Large statements exceeding max_allowed_packet and idle connections past wait_timeout are the usual causes, and proxies can drop idle TCP connections independently.

Fix in practice

Raise max_allowed_packet or split large statements into batches, keep connections alive with a shorter recycle time than wait_timeout, and configure the pool to validate or ping connections before handing them out. Retrying the query once after reconnecting handles the residual cases.

Verification

Rerun the large insert or idle scenario and confirm it completes, then confirm the pool now replaces closed connections instead of reusing them. Completion plus healthy pool behavior confirms the fix.

Diagnostic commands

Check the behavior with commands like these:

mysql -e "show variables like 'max_allowed_packet'; show variables like 'wait_timeout'"
ls -lh big_dump.sql
grep -rn "testOnBorrow\|pool_reset\|ping" src config