How to Fix MySQL 'Gone Away' Error Quickly
wait_timeout and max_allowed_packet settings in your MySQL configuration and ensure your queries are optimized.Why This Happens
The 'MySQL server has gone away' error occurs when the client tries to communicate with the server but the connection is closed or lost. This can happen if the server times out idle connections, if a query is too large, or if the server restarts unexpectedly.
mysql> SELECT * FROM large_table WHERE data = REPEAT('a', 20000000);
The Fix
To fix this error, increase the wait_timeout to allow longer idle connections and raise max_allowed_packet to support bigger queries. Restart MySQL after changing these settings. Also, split large queries if possible.
[mysqld] wait_timeout=28800 max_allowed_packet=64M
Prevention
To avoid this error in the future, keep your queries efficient and avoid sending very large packets at once. Regularly monitor and adjust wait_timeout and max_allowed_packet based on your workload. Use persistent connections carefully and close unused connections promptly.
Related Errors
Similar errors include Lost connection to MySQL server during query, which also relates to connection drops. Fixes often overlap by tuning timeouts and packet sizes. Another related error is MySQL server has gone away during sleep, caused by long idle times.