解决Lost connection to MySQL server during query错误方法

昨天使用Navicat for MySQL导入MySQL数据库的时候,出现了一个严重的错误,Lost connection to MySQL server during query,字面意思就是在查询过程当中丢失链接到MySQL服务器。php

[Msg] Decompressing...
[Msg] Table Created: wp_wiki_copy
[Msg] Importing Data...
[Msg] 2013 - Lost connection to MySQL server during query
[Msg] Table Restored: wp_wiki_copy
[Msg] Finished - Stopped before completionmysql

个人数据量大概了5万条,备份的数据库文件大小有240M,这仍是压缩了的,确实有点大。sql

初步判断是MySQL可能挂掉了,在系统服务里面查看MySQL的进程并无中止。shell

最开始考虑是数据库结构不对,可是我是经过Navicat for MySQL的备份和恢复备份导入数据,应该表结构都在备份文件里面,应该不是数据库结构的问题。数据库

网络环境都是本地。也不多是网络连接和数据库服务器的问题。windows

最后在早上找到了解决方法,在my.ini配置文件 mysqld 节点下添加api

max_allowed_packet = 500M缓存

也就是配置MySQL容许的最大数据包大小,上面的500M你能够根据你的项目修改成你本身的值,只要比要导入的备份文件大就能够了。服务器

================================知识补充================================网络

 

mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的连接断开了。

形成这样的缘由通常是sql操做的时间过长,或者是传送的数据太大(例如使用insert ... values的语句过长, 这种状况能够经过修改max_allowed_packed的配置参数来避免,也能够在程序中将数据分批插入)。

产生这个问题的缘由有不少,总结下网上的分析:

缘由一. MySQL 服务宕了

判断是否属于这个缘由的方法很简单,进入mysql控制台,查看mysql的运行时长

mysql> show global status like 'uptime';
+---------------+---------+
| Variable_name | Value   |
+---------------+---------+
| Uptime        | 3414707 |
+---------------+---------+

1 row in set或者查看MySQL的报错日志,看看有没有重启的信息

若是uptime数值很大,代表mysql服务运行了好久了。说明最近服务没有重启过。
若是日志没有相关信息,也表名mysql服务最近没有重启过,能够继续检查下面几项内容。

缘由二. mysql链接超时

即某个mysql长链接好久没有新的请求发起,达到了server端的timeout,被server强行关闭。
此后再经过这个connection发起查询的时候,就会报错server has gone away
(大部分PHP脚本就是属于此类)

mysql> show global variables like '%timeout';
+----------------------------+----------+
| Variable_name              | Value    |
+----------------------------+----------+
| connect_timeout            | 10       |
| delayed_insert_timeout     | 300      |
| innodb_lock_wait_timeout   | 50       |
| innodb_rollback_on_timeout | OFF      |
| interactive_timeout        | 28800    |
| lock_wait_timeout          | 31536000 |
| net_read_timeout           | 30       |
| net_write_timeout          | 60       |
| slave_net_timeout          | 3600     |
| wait_timeout               | 28800    |
+----------------------------+----------+
10 rows in set

wait_timeout 是28800秒,即mysql连接在无操做28800秒后被自动关闭

缘由三. mysql请求连接进程被主动kill

这种状况和缘由二类似,只是一个是人为一个是MYSQL本身的动做

mysql> show global status like 'com_kill';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_kill      | 21    |
+---------------+-------+
1 row in set缘由四. Your SQL statement was too large.

当查询的结果集超过 max_allowed_packet 也会出现这样的报错。定位方法是打出相关报错的语句。

用select * into outfile 的方式导出到文件,查看文件大小是否超过 max_allowed_packet ,若是超过则须要调整参数,或者优化语句。

mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
1 row in set (0.00 sec)

修改参数:

mysql> set global max_allowed_packet=1024*1024*16;
mysql> show global variables like 'max_allowed_packet';
+--------------------+----------+
| Variable_name      | Value    |
+--------------------+----------+
| max_allowed_packet | 16777216 |
+--------------------+----------+
1 row in set (0.00 sec)

如下是补充:

应用程序(好比PHP)长时间的执行批量的MYSQL语句。执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。好比,图片数据的处理。都容易引发MySQL server has gone away。 

今天遇到相似的情景,MySQL只是冷冷的说:MySQL server has gone away。 

大概浏览了一下,主要多是由于如下几种缘由: 
一种多是发送的SQL语句太长,以至超过了max_allowed_packet的大小,若是是这种缘由,你只要修改my.cnf,加大max_allowed_packet的值便可。 

还有一种多是由于某些缘由致使超时,好比说程序中获取数据库链接时采用了Singleton的作法,虽然屡次链接数据库,但其实使用的都是同一个链接,并且程序中某两次操做数据库的间隔时间超过了wait_timeout(SHOW STATUS能看到此设置),那么就可能出现问题。最简单的处理方式就是把wait_timeout改大,固然你也能够在程序里时不时顺手mysql_ping()一下,这样MySQL就知道它不是一我的在战斗。 

解决MySQL server has gone away 

一、应用程序(好比PHP)长时间的执行批量的MYSQL语句。最多见的就是采集或者新旧数据转化。 
解决方案: 
在my.cnf文件中添加或者修改如下两个变量: 
wait_timeout=2880000 
interactive_timeout = 2880000 
关于两个变量的具体说明能够google或者看官方手册。若是不能修改my.cnf,则能够在链接数据库的时候设置CLIENT_INTERACTIVE,好比: 
sql = "set interactive_timeout=24*3600"; 
mysql_real_query(...) 


二、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。好比,图片数据的处理 
解决方案: 
在my.cnf文件中添加或者修改如下变量: 
max_allowed_packet = 10M(也能够设置本身须要的大小) 
max_allowed_packet 参数的做用是,用来控制其通讯缓冲区的最大长度。 


最近作网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页很是慢慢,超过了mysql的 wait-timeout时间,而后当网页内容被抓回来后,准备插入到MySQL的时候,发现MySQL的链接超时关闭了,因而就出现了“MySQL server has gone away”这样的错误提示,解决这个问题,个人经验有如下两点,或许对你们有用处: 
第 一种方法: 
固然是增长你的 wait-timeout值,这个参数是在my.cnf(在Windows下台下面是my.ini)中设置,个人数据库负荷稍微大一点,因此,我设置的值 为10,(这个值的单位是秒,意思是当一个数据库链接在10秒钟内没有任何操做的话,就会强行关闭,我使用的不是永久连接 (mysql_pconnect),用的是mysql_connect,关于这个wait-timeout的效果你能够在MySQL的进程列表中看到 (show processlist) ),你能够把这个wait-timeout设置成更大,好比300秒,呵呵,通常来说300秒足够用了,其实你也能够不用设置,MySQL默认是8个小 时。状况由你的服务器和站点来定。 
第二种方法: 
这也是我我的认为最好的方法,即检查 MySQL的连接状态,使其从新连接。 
可能你们都知道有mysql_ping这么一个函数,在不少资料中都说这个mysql_ping的 API会检查数据库是否连接,若是是断开的话会尝试从新链接,但在个人测试过程当中发现事实并非这样子的,是有条件的,必需要经过 mysql_options这个C API传递相关参数,让MYSQL有断开自动连接的选项(MySQL默认为不自动链接),但我测试中发现PHP的MySQL的API中并不带这个函数,你从新编辑MySQL吧,呵呵。但mysql_ping这个函数仍是终于能用得上的,只是要在其中有一个小小的操做技巧: 
这是个人的数据库操 做类中间的一个函数 

复制代码 代码以下:

function ping(){ 
if(!mysql_ping($this->link)){ 
mysql_close($this->link); //注意:必定要先执行数据库关闭,这是关键 
$this->connect(); 


我须要调用这个函数的代码多是这样子的 

复制代码 代码以下:

$str = file_get_contents('http://www.jb51.net'); 
$db->ping();//通过前面的网页抓取后,或者会致使数据库链接关闭,检查并从新链接 
$db->query('select * from table'); 


ping()这个函数先检测数据链接是否正常,若是被关闭,整个把当前脚本的MYSQL实例关闭,再从新链接。 
经 过这样处理后,能够很是有效的解决MySQL server has gone away这样的问题,并且不会对系统形成额外的开销。 
__________________________________________________________________________________________________ 
  今天遇到相似的情景,MySQL只是冷冷的说:MySQL server has gone away。 
  大概浏览了一下,主要多是由于如下几种缘由: 
  一种多是发送的SQL语句太长,以至超过了max_allowed_packet的大小,若是是这种缘由,你只要修改my.cnf,加大max_allowed_packet的值便可。
  还有一种多是由于某些缘由致使超时,好比说程序中获取数据库链接时采用了Singleton的作法,虽然屡次链接数据库,但其实使用的都是同一个链接,并且程序中某两次操做数据库的间隔时间超过了wait_timeout(SHOW STATUS能看到此设置),那么就可能出现问题。最简单的处理方式就是把wait_timeout改大,固然你也能够在程序里时不时顺手mysql_ping()一下,这样MySQL就知道它不是一我的在战斗。 
  解决MySQL server has gone away 
  一、应用程序(好比PHP)长时间的执行批量的MYSQL语句。最多见的就是采集或者新旧数据转化。 
  解决方案: 
  在my.cnf文件中添加或者修改如下两个变量: 
wait_timeout=2880000 
interactive_timeout = 2880000  关于两个变量的具体说明能够google或者看官方手册。若是不能修改my.cnf,则能够在链接数据库的时候设置CLIENT_INTERACTIVE,好比: 
sql = "set interactive_timeout=24*3600"; 
mysql_real_query(...) 
  二、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。好比,图片数据的处理 
  解决方案: 
  在my.cnf文件中添加或者修改如下变量: 
max_allowed_packet = 10M(也能够设置本身须要的大小) 
max_allowed_packet参数的做用是,用来控制其通讯缓冲区的最大长度。
一、应用程序(好比PHP)长时间的执行批量的MYSQL语句。
最多见的就是采集或者新旧数据转化。

 

解决方案:

在my.ini文件中添加或者修改如下两个变量:
wait_timeout=2880000
interactive_timeout = 2880000

关于两个变量的具体说明能够google或者看官方手册。
若是不能修改my.cnf,则能够在链接数据库的时候设置CLIENT_INTERACTIVE,好比:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)

二、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。
好比,图片数据的处理
解决方案

在my.cnf文件中添加或者修改如下变量:
max_allowed_packet = 10M (也能够设置本身须要的大小)

max_allowed_packet 参数的做用是,用来控制其通讯缓冲区的最大长度。

修改方法

1) 方法1
能够编辑my.cnf来修改(windows下my.ini),在[mysqld]段或者mysql的server配置段进行修改。

max_allowed_packet = 20M
若是找不到my.cnf能够经过

mysql --help | grep my.cnf
去寻找my.cnf文件。

2) 方法2
(很妥协,很纠结的办法)

进入mysql server

在mysql 命令行中运行

set global max_allowed_packet = 2*1024*1024*10
而后关闭掉这此mysql server连接,再进入。

show VARIABLES like '%max_allowed_packet%';
查看下max_allowed_packet是否编辑成功

------------ 如下是网络搜索的资料 -------------------

也许其余人遇到这个问题,不必定是这儿的缘由,那么,就把我在网上找到比较全面的分析放到下面:有两篇,第一篇比较直观,第二篇比较深奥。解决MySQL server has gone away 2009-01-09 16:23:22今天遇到相似的情景,MySQL只是冷冷的说:MySQL server has gone away。大概浏览了一下,主要多是由于如下几种缘由:一种多是发送的SQL语句太长,以至超过了max_allowed_packet的大小,若是是这种缘由,你只要修改my.cnf,加大max_allowed_packet的值便可。还有一种多是由于某些缘由致使超时,好比说程序中获取数据库链接时采用了Singleton的作法,虽然屡次链接数据库,但其实使用的都是同一个链接,并且程序中某两次操做数据库的间隔时间超过了wait_timeout(SHOW STATUS能看到此设置),那么就可能出现问题。最简单的处理方式就是把wait_timeout改大,固然你也能够在程序里时不时顺手mysql_ping()一下,这样MySQL就知道它不是一我的在战斗。解决MySQL server has gone away一、应用程序(好比PHP)长时间的执行批量的MYSQL语句。最多见的就是采集或者新旧数据转化。解决方案:在my.cnf文件中添加或者修改如下两个变量:wait_timeout=2880000interactive_timeout = 2880000  关于两个变量的具体说明能够google或者看官方手册。若是不能修改my.cnf,则能够在链接数据库的时候设置CLIENT_INTERACTIVE,好比:sql = "set interactive_timeout=24*3600";mysql_real_query(...)二、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。好比,图片数据的处理解决方案:在my.cnf文件中添加或者修改如下变量:max_allowed_packet = 10M(也能够设置本身须要的大小)max_allowed_packet参数的做用是,用来控制其通讯缓冲区的最大长度MySQL: 诡异的MySQL server has gone away及其解决在Mysql执行show status,一般更关注缓存效果、进程数等,每每忽略了两个值:Variable_name Value Aborted_clients 3792 Aborted_connects 376一般只占query的0.0x%,因此并不为人所重视。并且在传统Web应用上,query错误对用户而言影响并不大,只是从新刷新一下页面就OK了。最近的基础改造中,把不少应用做为service运行,没法提示用户从新刷新,这种状况下,可能就会影响到服务的品质。经过程序脚本的日志跟踪,主要报错信息为“MySQL server has gone away”。官方的解释是:The most common reason for the MySQL server has gone away error is that the server timed out and closed the connection.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 (the reconnect 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_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.In this case, 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.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 MyODBC 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 in Section A.1.2.9, “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 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.You may also see the MySQL server has gone away error if MySQL is started with the --skip-networking option.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.据此分析,可能缘由有3: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汇报中会有吧。~~呵呵~~本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/brightsnow/archive/2009/03/17/3997705.aspxdescription: 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 amount 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 参数的做用是,用来控制其通讯缓冲区的最大长度。

相关文章
相关标签/搜索