MySQL server has gone away问题的解决办法

发布时间:2020-07-20编辑:脚本学堂
本文介绍下,在mysql数据库中,遇到MySQL server has gone away问题,这里分享下具体的解决办法,有需要的朋友参考下吧。

通过程序脚本的日志跟踪,主要报错信息为“MySQL server has gone away”。

可能原因有三:
1,Mysql服务端与客户端版本不匹配。
2,Mysql服务端配置有缺陷或者优化不足
3,需要改进程序脚本

通过更换多个服务端与客户端版本,发现只能部分减少报错,并不能完全解决。排除1。

对服务端进行了彻底的优化,也未能达到理想效果。
在timeout的取值设置上,从经验值的10,到PHP默认的60,进行了多次尝试。
而Mysql官方默认值(8小时)明显是不可能的。
从而对2也进行了排除。

针对3对程序代码进行分析,发现程序中大量应用了类似如下的代码(为便于理解,用原始api描述):
 

复制代码 代码示例:
$conn=mysql_connect( ... ... );
... .. ... ...
if(!$conn){ //reconnect
$conn=mysql_connect( ... ... );
}
mysql_query($sql, $conn);

这段代码的含义,与Mysql官方建议的方法思路相符[ If you have a script, you just have to issue the query again for the client to do an automatic reconnection. ]。在实际分析中发现,if(!$conn)并不是可靠的,程序通过了if(!$conn)的检验后,仍然会返回上述错误。

对程序进行了改写:
 

复制代码 代码示例:
if(!conn){ // connect ...}
elseif(!mysql_ping($conn)){ // reconnect ... }
mysql_query($sql, $conn);

经实际观测,MySQL server has gone away的报错基本解决。
BTW: 附带一个关于 reconnect 的疑问,
在php4x+client3x+mysql4x的旧环境下,reconnet的代码:

$conn=mysql_connect(...) 可以正常工作。
但是,在php5x+client4x+mysql4x的新环境下,$conn=mysql_connect(...)返回的$conn有部分情况下不可用。需要书写为:
 

复制代码 代码示例:
mysql_close($conn);
$conn=mysql_connect(...);

返回的$conn才可以正常使用。原因未明。
或许mysql官方的BUG汇报中会有吧。

description:
remember that your MySQL "max_allowed_packet" configuration setting (default 1MB)
mysql 默认最大能够处理的是1MB
如果你在sql使用了大的text或者BLOB数据,就会出现这个问题。 php手册上的注释

When trying to INSERT or UPDATE and trying to put a large alinuxjishu/9952.html target=_blank class=infotextkey>mount of text or data (blob) into a mysql table you might run into problems.
In mysql.err you might see:
Packet too large (73904)
To fix you just have to start up mysql with the option -O max_allowed_packet=maxsize
You would just replace maxsize with the max size you want to insert, the default is 65536

mysql手册上的介绍:
Both the client and the server have their own max_allowed_packet variable, so if you want to handle big packets, you must increase this variable both in the client and in the server.

If you are using the mysql client program, its default max_allowed_packet variable is 16MB. To set a larger value, start mysql like this:

shell> mysql --max_allowed_packet=32M That sets the packet size to 32MB.

The server's default max_allowed_packet value is 1MB. You can increase this if the server needs to handle big queries (for example, if you are working with big BLOB columns). For example, to set the variable to 16MB, start the server like this:

shell> mysqld --max_allowed_packet=16M You can also use an option file to set max_allowed_packet. For example, to set the size for the server to 16MB, add the following lines in an option file:

[mysqld]max_allowed_packet=16M

使用mysql做数据库还原时,由于有些数据很大,会出现这样的错误:The MySQL Server returned this Error:MySQL Error Nr.2006-MySQL server has gone away。

我的一个150mb的备份还原时就出现了这错误。

解决方法:
找到mysql安装目录,找到my.ini文件,在文件的最后添加:max_allowed_packet = 10M(也可以设置自己需要的大小)。
max_allowed_packet 参数的作用:控制其通信缓冲区的最大长度。

>>> 您可能感兴趣的文章:
有关mysql server has gone away报错的原因分析
mysql server has gone away报错原因的分析