0
0
MysqlDebug / FixBeginner · 4 min read

How to Fix MySQL 'Gone Away' Error Quickly

The MySQL 'gone away' error happens when the connection to the server is lost, often due to timeout or large queries. To fix it, increase the 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.

sql
mysql> SELECT * FROM large_table WHERE data = REPEAT('a', 20000000);
Output
ERROR 2006 (HY000): MySQL server has gone away
🔧

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.

ini
[mysqld]
wait_timeout=28800
max_allowed_packet=64M
Output
MySQL server restarted with new settings; large queries now succeed without error.
🛡️

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.

Key Takeaways

Increase wait_timeout and max_allowed_packet in MySQL config to fix connection drops.
Split very large queries to avoid exceeding packet size limits.
Restart MySQL server after changing configuration settings.
Monitor connection usage and close idle connections promptly.
Optimize queries to reduce load and prevent timeouts.