`
beyondhjjyt
  • 浏览: 39748 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

MySQL server has gone away问题解决方法

 
阅读更多

  1、应用程序(比如PHP)长时间的执行批量的MYSQL语句。最常见的就是采集或者新旧数据转化。

  解决方案:

  在my.cnf文件中添加或者修改以下两个变量:

wait_timeout=2880000
interactive_timeout = 2880000  
关于两个变量的具体说明可以google或者看官方手册。如果不能修改my.cnf,则可以在连接数据库的时候设置CLIENT_INTERACTIVE,比如:

sql = "set interactive_timeout=24*3600";
mysql_real_query(...)

  2、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。比如,图片数据的处理

  解决方案:

  在my.cnf文件中添加或者修改以下变量:

max_allowed_packet = 10M
(也可以设置自己需要的大小)

max_allowed_packet
参数的作用是,用来控制其通信缓冲区的最大长度

MySQL: 诡异的MySQL server has gone away及其解决
来自:http://fz9493.blog.sohu.com/38472203.html

jimmy | 15 三月, 2007 20:32

在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 becorrected.

A client application running on a different host does not have thenecessary 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 mayhelp solve the problem.

You have encountered a timeout on the server side and the automaticreconnection in the client is disabled (the reconnect flag in the MYSQLstructure is equal to 0).

You are using a Windows client and the server had dropped theconnection (probably because wait_timeout expired) before the commandwas issued.

The problem on Windows is that in some cases MySQL doesn't get anerror 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 theconnection.

In this case, even if the reconnect flag in the MYSQL structure isequal to 1, MySQL does not automatically reconnect and re-issue thequery as it doesn't know if the server did get the original query ornot.

The solution to this is to either do a mysql_ping on the connectionif there has been a long time since the last query (this is what MyODBCdoes) or set wait_timeout on the mysqld server so high that it inpractice never times out.

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

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

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

It is also possible to see this error if hostname lookups fail (forexample, if the DNS server on which your server or network relies goesdown). This is because MySQL is dependent on the host system for nameresolution, but has no way of knowing whether it is working — fromMySQL's point of view the problem is indistinguishable from any othernetwork 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 theMySQL port (default 3306) is blocked by your firewall, thus preventingany connections at all to the MySQL server.

You can also encounter this error with applications that fork childprocesses, all of which try to use the same connection to the MySQLserver. This can be avoided by using a separate connection for eachchild 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 toissue 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.aspx


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 amount oftext 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_packetvariable, so if you want to handle big packets, you must increase thisvariable both in the client and in the server.

If you are using the mysql client program, its defaultmax_allowed_packet variable is 16MB. To set a larger value, start mysqllike 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 increasethis if the server needs to handle big queries (for example, if you areworking with big BLOB columns). For example, to set the variable to16MB, start the server like this:

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

[mysqld]max_allowed_packet=16M

使用mysql做数据库还原的时候,由于有些数据很大,会出现这样的错误:The MySQL Server returned thisError:MySQL Error Nr.2006-MySQL server has goneaway。我的一个150mb的备份还原的时候就出现了这错误。解决的方法就是找到mysql安装目录,找到my.ini文件,在文件的最后添加:max_allowed_packet = 10M(也可以设置自己需要的大小)。 max_allowed_packet参数的作用是,用来控制其通信缓冲区的最大长度。

  1、应用程序(比如PHP)长时间的执行批量的MYSQL语句。最常见的就是采集或者新旧数据转化。

  解决方案:

  在my.cnf文件中添加或者修改以下两个变量:

wait_timeout=2880000
interactive_timeout = 2880000  
关于两个变量的具体说明可以google或者看官方手册。如果不能修改my.cnf,则可以在连接数据库的时候设置CLIENT_INTERACTIVE,比如:

sql = "set interactive_timeout=24*3600";
mysql_real_query(...)

  2、执行一个SQL,但SQL语句过大或者语句中含有BLOB或者longblob字段。比如,图片数据的处理

  解决方案:

  在my.cnf文件中添加或者修改以下变量:

max_allowed_packet = 10M
(也可以设置自己需要的大小)

max_allowed_packet
参数的作用是,用来控制其通信缓冲区的最大长度

MySQL: 诡异的MySQL server has gone away及其解决
来自:http://fz9493.blog.sohu.com/38472203.html

jimmy | 15 三月, 2007 20:32

在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 becorrected.

A client application running on a different host does not have thenecessary 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 mayhelp solve the problem.

You have encountered a timeout on the server side and the automaticreconnection in the client is disabled (the reconnect flag in the MYSQLstructure is equal to 0).

You are using a Windows client and the server had dropped theconnection (probably because wait_timeout expired) before the commandwas issued.

The problem on Windows is that in some cases MySQL doesn't get anerror 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 theconnection.

In this case, even if the reconnect flag in the MYSQL structure isequal to 1, MySQL does not automatically reconnect and re-issue thequery as it doesn't know if the server did get the original query ornot.

The solution to this is to either do a mysql_ping on the connectionif there has been a long time since the last query (this is what MyODBCdoes) or set wait_timeout on the mysqld server so high that it inpractice never times out.

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

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

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

It is also possible to see this error if hostname lookups fail (forexample, if the DNS server on which your server or network relies goesdown). This is because MySQL is dependent on the host system for nameresolution, but has no way of knowing whether it is working — fromMySQL's point of view the problem is indistinguishable from any othernetwork 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 theMySQL port (default 3306) is blocked by your firewall, thus preventingany connections at all to the MySQL server.

You can also encounter this error with applications that fork childprocesses, all of which try to use the same connection to the MySQLserver. This can be avoided by using a separate connection for eachchild 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 toissue 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.aspx


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 amount oftext 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_packetvariable, so if you want to handle big packets, you must increase thisvariable both in the client and in the server.

If you are using the mysql client program, its defaultmax_allowed_packet variable is 16MB. To set a larger value, start mysqllike 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 increasethis if the server needs to handle big queries (for example, if you areworking with big BLOB columns). For example, to set the variable to16MB, start the server like this:

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

[mysqld]max_allowed_packet=16M

使用mysql做数据库还原的时候,由于有些数据很大,会出现这样的错误:The MySQL Server returned thisError:MySQL Error Nr.2006-MySQL server has goneaway。我的一个150mb的备份还原的时候就出现了这错误。解决的方法就是找到mysql安装目录,找到my.ini文件,在文件的最后添加:max_allowed_packet = 10M(也可以设置自己需要的大小)。 max_allowed_packet参数的作用是,用来控制其通信缓冲区的最大长度。

分享到:
评论

相关推荐

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

    "MySQL server has gone away 问题的解决方法" MySQL server has gone away 是一个常见的 MySQL 错误提示,出现这种错误的原因可能是多种多样的,今天我们将从不同的角度来讨论这个问题,并提供一些实用的解决方法...

    MySQL server has gone away错误提示解决方法

    其次,超时问题也是导致MySQL Server has gone away错误的常见原因。MySQL服务器有两组超时设置,`wait_timeout`和`interactive_timeout`。前者针对非交互式连接(如Web应用),后者针对交互式连接(如命令行客户端...

    MySQL导入sql脚本错误:2006 解决方法

    MySQL导入sql脚本错误:2006 – MySQL server has gone away 到如一些小脚本很少报错,但最近导入一个10+M的SQL脚本,却重复报错: Error occured at:2014-03-24 11:42:24 Line no.:85 Error Code: 2006 - MySQL ...

    gearman中worker常驻后台,导致MySQL server has gone away的解决方法

    本文实例讲述了gearman中worker常驻后台,导致MySQL server has gone away的解决方法。分享给大家供大家参考,具体如下: 产生这个原因主要有如下几点: 1、mysql服务宕机了 2、长时间没有操作,超过了wait_timeout...

    解决MySQL server has gone away错误的方案

    在我们使用mysql导入大文件sql时可能会报MySQL server has gone away错误,该问题是max_allowed_packet配置的默认值设置太小,只需要相应调大该项的值之后再次导入便能成功。该项的作用是限制mysql服务端接收到的包...

    sql错误解决错误号:40error: 40

    在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 ...

    mysql提示got timeout reading communication packets的解决方法

    MYSQL server has gone away 引起这个原因是不可怕的.原因是更改了系统的断开时间. mysql>show gloable variables like “%timeout%”; 进行查看 interactive_timeout 的黓认值为28800 wait_timeout 的默认值这:...

    101个MySQL的调节和优化方法

    以下是对“101个MySQL的调节和优化方法”这一主题的深入解析,涵盖从硬件配置到软件参数设置,以及查询优化等多个层面的知识点。 ### 硬件配置优化 1. **InnoDB缓存池大小调整**:根据服务器内存资源,合理设定`...

    使用MySQL时的一些常见错误.docx

    本文将详细讨论两个常见的错误:“MySQL server has gone away”和“Can't connect to [local] MySQL server”。 首先,让我们关注“MySQL server has gone away”错误。这个错误通常意味着服务器在执行查询时超时...

    全国手机号码段归属地数据库(记录条数共415284条记录)

    若导入时报错,ERROR 2006 (HY000):MySQL server has gone away 。我们可以通过语句查看一下允许的最大包大小:show global variables like 'max_allowed_packet'; MySQL使用最大数据包站站点进行服务器和客户端...

    Qt5.4下连接Mysql,QSqlDatabase: QMYSQL driver not loaded but available-附件资源

    Qt5.4下连接Mysql,QSqlDatabase: QMYSQL driver not loaded but available-附件资源

    Django数据库连接丢失问题的解决方法

    OperationalError: (2006, ‘MySQL server has gone away’) OperationalError: (2013, ‘Lost connection to MySQL server during query’) 查询mysql全局变量SHOW GLOBAL VARIABLES;可以看到wait_timeout,此变量...

    关于MySQL的wait-timeout连接超时问题报错解决方案.docx

    然而,如果将其设置的太小,可能会遭遇到“MySQL has gone away” 之类的问题。 show variables 命令的误解 在使用 show variables 命令时,需要注意的是,这个命令实际上是查询的是会话变量,而不是全局变量。这...

Global site tag (gtag.js) - Google Analytics