Bootstrap

关于mysql数据库在运行中出现MySQL server has gone away的问题解决

这段时间在进行数采软件开发时,搭建了mysql数据库,用于数据的存储,晚上系统联调后,软件运行了一个晚上。

第二天过来,再进行数据操作功能测试时,出现了MySQL server has gone away的错误和Lost connection to MySQL server during query错误。

通过百度查找及mysql官网查找相关原因,基本可以确定本次出现的问题是因为mysql数据库服务8小时不做数据处理操作后,mysql server连接自动断开,从而出现该问题。

mysql官网原文如下,链接为http://dev.mysql.com/doc/refman/5.6/en/gone-away.html

B.5.2.9 MySQL server has gone away

This section also covers the related Lost connection to server during query error.

The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection. In this case, you normally get one of the following error codes (which one you get is operating system-dependent).

Error Code Description
CR_SERVER_GONE_ERROR The client couldn't send a question to the server.
CR_SERVER_LOST The client didn't get an error when writing to the server, but it didn't get a full answer (or any answer) to the question.

By default, the server closes the connection after eight hours if nothing has happened. You can change the time limit by setting the wait_timeout variable when you start mysqld. See Section 5.1.4, “Server System Variables”.

If you have a script, you just have to issue the query again for the client to do an automatic reconnection. This assumes that you have automatic reconnection in the client enabled (which is the default for the mysql command-line client).

Some other common reasons for the MySQL server has gone away error are:

  • You (or the db administrator) has killed the running thread with a KILL statement or a mysqladmin kill command.

  • You tried to run a query after closing the connection to the server. This indicates a logic error in the application that should be corrected.

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

  • 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 may help solve the problem.

  • You have encountered a timeout on the server side and the automatic reconnection in the client is disabled (thereconnect flag in the MYSQL structure is equal to 0).

  • You are using a Windows client and the server had dropped the connection (probably because wait_timeoutexpired) before the command was issued.

    The problem on Windows is that in some cases MySQL does not 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.

    The solution to this is to either do a mysql_ping() on the connection if there has been a long time since the last query (this is what Connector/ODBC does) or set wait_timeout on the mysqld server so high that it in practice never times out.

  • 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 inSection B.5.2.10, “Packet Too Large”.

    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.

  • 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.

  • It is also possible to see this error if host name 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.

    You may also see the MySQL server has gone away error if MySQL is started with the --skip-networkingoption.

    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.

  • 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.

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

You can check whether the MySQL server died and restarted by executing mysqladmin version and examining the server's uptime. If the client connection was broken because mysqld crashed and restarted, you should concentrate on finding the reason for the crash. Start by checking whether issuing the query again kills the server again. See Section B.5.4.2, “What to Do If MySQL Keeps Crashing”.

You can get more information about the lost connections by starting mysqld with the --log-warnings=2 option. This logs some of the disconnected errors in the hostname.err file. See Section 5.2.2, “The Error Log”.

If you want to create a bug report regarding this problem, be sure that you include the following information:

See also Section B.5.2.11, “Communication Errors and Aborted Connections”, and Section 1.7, “How to Report Bugs or Problems”.

可以通过两种方式解决该问题。

1.修改配置文件my.ini文件

在[mysqld]项中,增加【wait_timeout】及【interactive_timeout】内容,将wait_timeout及interactive_timeout数值设定较大数值(如86400(24小时)),默认

这两项为28800秒(8小时),

wait_timeout=86400
interactive_timeout=86400

wait_timeout的最大值在windows操作系统上为2147483秒,interactive_timeout未查看到,各位可自行查找测试。

 wait_timeout

Command-Line Format --wait_timeout=#
System Variable Name wait_timeout
Variable Scope Global, Session
Dynamic Variable Yes
Permitted Values (Windows) Type integer
Default 28800
Min Value 1
Max Value 2147483
Permitted Values (Other) Type integer
Default 28800
Min Value 1
Max Value 31536000

注意修改完参数后需要重新启动mysql服务使设置有效。

然后可以通过命令查看是否更改,从而进行相关测试。本人测试时设置的数值为300秒,见下图



2.在软件中进行处理

判断数据库访问时的异常值(本人这里当前状况下数据库操作时的返回数值为-8),然后在软件中增加数据库重连代码中进行重连操作即可恢复正常。


;