MySQL Tip: MySQL Server Has Gone Away Or Lost connection to server during query Fix

By Angsuman Chakraborty, Gaea News Network
Tuesday, July 24, 2007

A much dreaded MySQL error message during queries is “MySQL server has gone away”. An alternative message is “Lost connection to server during query”. This is a strange problem which afflicts a wide variety of PHP software including but not limited to WordPress. There are several causes for it. Let’s look at the common and some rare causes and what you can do to fix it.

The most common causes are:
1. The server timed out and closed the connection. By default, the server closes the connection after 8 hours or 28800 seconds if nothing has happened. You can change the time limit by setting the wait_timeout variable when you start mysqld via your server’s /etc/my.cnf (on Linux; locate the file in installation directory on windows) as well. This mostly affects persistent connections; connections opened using mysql_pconnect() in PHP. It can also affect pooled connections from say any server side connection pooling.

2. Another common reason to receive the MySQL server has gone away error is because you have issued a “close” on your MySQL connection and then tried to run a query on the closed connection. This is a simple logic problem. Are you sharing the connection across multiple threads?

3. You got a timeout from the TCP/IP connection on the client side. This may happen if you have been using the commands: mysql_options(…, MYSQL_OPT_READ_TIMEOUT,…) or mysql_options(…, MYSQL_OPT_WRITE_TIMEOUT,…). In this case increasing the timeout, as described above, may help solve the problem.

4. You have encountered a timeout on the server side and the automatic reconnection in the client is disabled. Please refer to the article linked above for details and solution.

5. You can also get these errors if you send a query to the server that is incorrect or too large. If mysqld receives a packet that is too large or out of order, it assumes that something has gone wrong with the client and closes the connection. If you need big queries (for example, if you are working with big BLOB columns), you can increase the query limit by setting the server’s max_allowed_packet variable, which has a default value of 1MB. You may also need to increase the maximum packet size on the client end. More information on setting the packet size is given in Section B.1.2.9, “Packet too large”.

6. An INSERT or REPLACE statement that inserts a great many rows can also cause these sorts of errors. Either one of these statements sends a single request to the server irrespective of the number of rows to be inserted; thus, you can often avoid the error by reducing the number of rows sent per INSERT or REPLACE.

7. You also get a lost connection if you are sending a packet 16MB or larger if your client is older than 4.0.8 and your server is 4.0.8 and above, or the other way around.

Few rare causes are:
1. Rarely the db administrator may have killed the running thread with a KILL statement or a mysqladmin kill command.

2. A client application running on a different host does not have the necessary privileges to connect to the MySQL server from that host.

3. You are using a Windows client and the server had dropped the connection (probably because wait_timeout expired) before the command was issued. The problem on Windows is that in some cases MySQL doesn’t get an error from the OS when writing to the TCP/IP connection to the server, but instead gets the error when trying to read the answer from the connection.

4. Prior to MySQL 5.0.19, even if the reconnect flag in the MYSQL structure is equal to 1, MySQL does not automatically reconnect and re-issue the query as it doesn’t know if the server did get the original query or not.

5. It is also possible to see this error if hostname lookups fail (for example, if the DNS server on which your server or network relies goes down). This is because MySQL is dependent on the host system for name resolution, but has no way of knowing whether it is working — from MySQL’s point of view the problem is indistinguishable from any other network timeout.

6. You may also see the MySQL server has gone away error if MySQL is started with the –skip-networking option.

7. You can also encounter this error with applications that fork child processes, all of which try to use the same connection to the MySQL server. This can be avoided by using a separate connection for each child process.

8. Another networking issue that can cause this error occurs if the MySQL port (default 3306) is blocked by your firewall, thus preventing any connections at all to the MySQL server.

9. You have encountered a bug where the server died while executing the query. Source

It takes time and effort to identify and solve these problems. Speaking from experience it is often not what it looks in the first place. Don’t assume anything, diligently rule out all possibilities till you solve it.

Discussion

Greg
April 13, 2009: 5:23 am

Does opening a connection, making several separate calls to the db, writing the html page then closing the connection fall under any of the above categories?

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :