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
-- 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.
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.
Related reading
- MySQL "Lock wait timeout exceeded": how to fix it: Fix MySQL error 1205 Lock wait timeout exceeded: find the blocking transaction with the performance schema, shorten transactions and index your updates.
- 408 Request Timeout: causes and how to fix it: Understand 408 Request Timeout: slow client uploads, idle keep-alive connections and proxy settings, and how it differs from a 504 Gateway Timeout.
- 504 Gateway Timeout: causes and fixes: What causes 504 Gateway Timeout errors, how timeouts stack across CDN, load balancer and Nginx, and how to find and fix the slow request.
- 502 Bad Gateway: causes and how to fix it: What a 502 Bad Gateway means, the common causes behind Nginx, load balancers and CDNs, how to find yours and how to stop it happening again.
- Cloudflare error 521: web server is down: Fix Cloudflare error 521 "Web server is down": the origin refused the connection. Check the web server, firewall rules for Cloudflare IPs and DNS.
- Cloudflare error 520: web server returns an unknown error: Fix Cloudflare error 520 "Web server returns an unknown error": empty or reset origin responses, crashes, oversized headers and keep-alive mismatches.
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.