MySQL server has gone away error resolution

  • 2020-05-07 20:32:24
  • OfStack

I have browsed about 1, mainly for the following reasons:
One possibility is that the SQL statement is so long that it exceeds the size of max_allowed_packet. If this is the case, you simply change my.cnf and increase the value of max_allowed_packet.
And one may be because of some reasons lead to the timeout, such as adopt Singleton when access to the database connection in the program, while connecting to the database for many times, but in fact are used with a connection, and the time interval of a twice in the program database operation over wait_timeout (SHOW STATUS can see this setting), then the problem may come up. The easiest way to do this is to make wait_timeout larger, but you can also drop mysql_ping()1 from time to time in the program, so MySQL knows it's not a person fighting.
addresses MySQL server has gone away
1. Applications (such as PHP) execute bulk MYSQL statements for long periods of time. The most common is the collection or conversion of old and new data.
Solutions:
Add or modify the following two variables in the my.cnf file:
wait_timeout=2880000
interactive_timeout = 2880000
Details of the two variables can be found in google or in the official manual. If you cannot modify my.cnf, you can set CLIENT_INTERACTIVE when you connect to the database, for example:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)
2. Execute 1 SQL, but SQL statement is too large or contains BLOB or longblob fields. For example, image data processing
Solutions:
Add or modify the following variables in the my.cnf file:
max_allowed_packet = 10M
(you can also set the size you want.)
max_allowed_packet
The parameter is used to control the maximum length of its communication buffer.

Related articles: