How to troubleshoot the “MySQL server has gone away” error
The MySQL server has gone away error denotes that the connection was closed by the MySQL server (mysqld) after it timed out. If nothing happens for eight hours (28800 seconds), MySQL will automatically terminate all open connections. It’s possible that your web host, DBA, or app developer reduced this timeout setting in some circumstances, as will be covered in the section that follows.
When attempting to restore a database, you might encounter the “MySQL Server has gone away” error.
# mysql database_name < database_back.sql
ERROR 2006 (HY000) at line 1253: MySQL server has gone away
Here are some instances of the error “MySQL server has gone away” from error logs:
Error Code: 2013. Lost connection to MySQL server during query
Warning: Error while sending QUERY packet
PDOException: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away
The followings are the primary causes of these errors and their fixes:-
- The connection was terminated by the server because it ran out of time. Verify that the wait timeout MySQL variable in your my.cnf configuration file is large enough to handle the situation.
- The InnoDB log file size MySQL variable in your my.cnf configuration might also need to be increased, perhaps to 128MB or higher.
- When a packet was too big or incorrect, the server dropped it. When mysqld receives a packet that is too large or incorrect, it assumes that the client is at fault and cuts off the connection.
The packet size in the my.ini file needs to be increased as a fix. 1 MB is the default. Change it to a value like 24M.
[mysqld]
max_allowed_packet=24M
Add max allowed packet after [mysqld] if it doesn’t already exist. Remember to save the file. You must restart MySQL for the changes to take effect after changing (or adding) the max allowed packet.
That’s it 🙂