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

解决方案:

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

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 server has gone away 2009-01-09 16:23:22

来自:http://www.webjx.com/database/mysql-8817.html

今天遇到类似的情景,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

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 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.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 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 2006_#2006 - MySQL server has gone away 问题解决方法相关推荐

  1. linux安装mysql遇到的问题_Linux下安装MySQL5.7及遇到的问题解决方法

    一.下载地址 本文安装的版本: 或者使用wget下载: [root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7 ...

  2. (2006, 'MySQL server has gone away') 错误解决 - dba007的空间 - 51CTO技术博客

    (2006, 'MySQL server has gone away') 错误解决 - dba007的空间 - 51CTO技术博客 (2006, 'MySQL server has gone away ...

  3. #2006 - MySQL server has gone away 问题解决方法 (全) (转)

    #2006 - MySQL server has gone away 问题解决方法 (全) (转) 参考文章: (1)#2006 - MySQL server has gone away 问题解决方法 ...

  4. Yii2 解决2006 MySQL server has gone away问题

    Yii2 解决2006 MySQL server has gone away问题 参考文章: (1)Yii2 解决2006 MySQL server has gone away问题 (2)https: ...

  5. django mysql 2006_Django (2006, 'MySQL server has gone away') 本地重现与解决

    最近我们的Django项目供Java Sofa应用进行tr调用时, 经常会出现一个异常: django.db.utils.OperationalError: (2006, 'MySQL server ...

  6. Yii 数据库重连告别General error: 2006 MySQL server has gone away

    General error: 2006 MySQL server has gone away 错误原因 制造错误 解决办法 最新办法 错误原因 Mysql has gone away MySQL 服务 ...

  7. peewee mysql自动断开_flask+mako+peewee(下)(解决了Error 2006: MySQL server has gone away)

    这篇主要介绍在这次项目中使用的peewee 首先我们要初始化一个数据库连接对象.这里我使用了peewee提供的链接池.当然你也可以直接指定连接例如: db = SqliteDatabase('base ...

  8. pymysql.err.OperationalError: (2006, “MySQL server has gone away (BrokenPipe

    目录 第一种情况:真的是连接数据库超时导致,比较常见 第二种情况:MySQL插入内容超过4M 在使用python+Django写项目时,需要用到定时任务apscheduler,但服务在长时间运行时,定 ...

  9. (2006, ‘MySQL server has gone away‘) 原因和解决方案

    mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 首选分析给出可能出现的 ...

最新文章

  1. c语言中如何设计和编写一个应用系统?
  2. ITK:创建Image
  3. Redis的文件事件与时间事件处理
  4. JobDataMap 更新_04
  5. 作一个真正合格的飞秋局域网聊天
  6. 数据结构基础(9) --单链表的设计与实现(2)之高级操作
  7. 3蛋白wb_WB常见问题原因分析及解决办法
  8. 设置ngxin服务器虚拟主机,详解Nginx 虚拟主机配置的三种方式(基于端口)
  9. Java 8 新特性 Stream类的collect方法
  10. python 实现SOM:代码注释与应用示例
  11. 思科交换机端口模式配置 端口安全配置 思科模拟器
  12. 常微分方程和偏微分方程
  13. 厦门大学计算机科学学院,厦门大学张俊松
  14. apple 密码 seeion has time out 无法再本机上重设密码
  15. Swift不深入只浅出入门教程-孟祥月-专题视频课程
  16. 罗晨:梦想照进现实,一个独立开发者的田园诗
  17. Win10的用户账户设置怎么取消?
  18. RK3399 Debian 制作xxx.deb
  19. 域策略怎么分发计算机软件,AD域中如何布置软件自动分发
  20. 「 工业缺陷检测深度学习方法」最新2022研究综述

热门文章

  1. 统计学习概论与ROC曲线
  2. 江苏省计算机二级vb知识点,2020年全国计算机二级VB复习知识点:数据类型
  3. 「学习总结-Haskell-1」Haskell 基础知识
  4. 土木类专业毕业设计全流程讲解,看完再也不担心毕业~
  5. 旋流式沉砂池计算_沉砂池计算
  6. 【hadoop】Archive命令使用
  7. Django MTV
  8. 制作一个圣诞快乐的卡片(python实现)
  9. pandas.pivot()函数的使用
  10. 信用卡葵花宝典 阅读笔记(三)