Skip to content

Troubleshooting guide

MySQL server has gone away: causes and how to fix it

The client tried to use a connection the server has already closed. Timeouts and oversized packets explain most cases.

3 min read10 sectionsUpdated

On this page

Key points

  • wait_timeout closes idle connections after eight hours by default.
  • A statement bigger than max_allowed_packet gets the connection dropped.
  • Pools must validate or recycle connections.
  • A server crash gives the same message, so check uptime.

What it means

Errors 2006 and 2013 mean the client lost its connection. The error appears at the moment of use, possibly hours after the server closed it, which makes the cause hard to see from the application's side.

Common causes

  • Idle connection beyond wait_timeout, or killed sooner by a firewall or proxy.
  • Query or result larger than max_allowed_packet.
  • Server restart or crash.
  • Failover to a new primary.
  • A forking process sharing one connection between children.

Diagnose it

Check whether the server restarted, then what the limits are.

bash
-- Seconds since the server started
SHOW GLOBAL STATUS LIKE 'Uptime';

-- Relevant limits
SHOW VARIABLES WHERE Variable_name IN ('wait_timeout','interactive_timeout','max_allowed_packet');

-- Aborted connection counters
SHOW GLOBAL STATUS LIKE 'Aborted_%';

Fixes

Make the pool's maximum connection lifetime shorter than wait_timeout and any firewall idle limit: pool_recycle in SQLAlchemy and maxLifetime in HikariCP. Enable pre-use validation, such as pool_pre_ping. Raise max_allowed_packet if large statements are legitimate, on both client and server. Batch huge inserts. Open database connections after forking, never before.

Clues in the timing

Timing often reveals the cause. Errors on the first request after a quiet period point to idle timeouts, and they typically appear in the morning, or after a weekend. An error at exactly the same statement every time, such as a big import, points to packet size. Errors across every client at one moment mean a restart or failover: compare the time with the server's Uptime. Errors at regular intervals may come from a load balancer or NAT gateway, which often have idle timeouts of a few minutes, far shorter than MySQL's own.

Catch it automatically

Chart aborted connections and server uptime in MySQL monitoring: an uptime that resets is a restart you might otherwise miss. Log management can alert on the exact error text across services.

Frequently asked questions

Should I just raise wait_timeout?

Not as the main fix. Configure the pool to recycle connections instead.

What is the default max_allowed_packet?

It is 64 MB on MySQL 8, and it was much smaller on older versions.

Why does it happen after idle periods only?

A connection was closed while idle, and the application tried to reuse it.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.