MySQL Server Has Gone Away
Understand and resolve MySQL connection losses.
The "MySQL server has gone away" error occurs when the connection between your application and MySQL is cut. This can happen for several reasons, each with different solutions.
Symptoms
- "MySQL server has gone away" error
- Sporadic errors on certain queries
- Timeout on long operations
- Connections that work then fail
Common Causes
- wait_timeout exceeded: MySQL closes inactive connections after wait_timeout seconds.
- max_allowed_packet too small: A query too large exceeds the allowed limit.
- MySQL restarted: A MySQL restart cuts all existing connections.
Diagnostic Steps
- Check if MySQL is running (systemctl status mysql)
- Examine MySQL logs (/var/log/mysql/error.log)
- Check wait_timeout and max_allowed_packet
- Test with a simple query to isolate the problem
Automate with MoniTao
MoniTao helps detect these issues:
- Monitoring of endpoints that use the DB
- Detection of connection-related 500 errors
- History to identify patterns
Best Practices
- Use persistent connections with auto-reconnect
- Increase max_allowed_packet if needed
- Implement a health check on the DB
- Use a connection pool with validation
FAQ
What value for wait_timeout?
Default 28800s (8h). Reduce to 300s if you have many connections.
How to enable auto-reconnect?
In PHP with PDO, use ATTR_PERSISTENT or catch the error to reconnect.
max_allowed_packet, what value?
Minimum 16M for standard apps. 64M or more if you store blobs.
How does MoniTao help?
By monitoring endpoints, you detect when the DB has problems.
Useful Links
Ready to Sleep Soundly?
Start free, no credit card required.