博客很久没有更新了,原因是大多数时间都忙在研究技术上,却懒得腾时间出来把技术分享,最近在开源力量上开课《Mongodb管理与维护》,让屌丝们从0到精通,敬请关注。本博客技术原创更新滞后一些,找时间更新有关mysql,mongodb等内容,谢谢大家关注。
全部博文(75)
分类: LINUX
2010-01-07 12:58:36
一、错误代码
Message: Lost connection to MySQL server during query
Message: MySQL server has gone away
二、错误代码原因
错误代码 |
描述 |
客户端无法向服务器发送请求 | |
当向服务器写入时无法获取错误,但无法获取请求的完整应答。 |
主要原因和解决办法:
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.
默认服务器在8小时后关闭连接,可以通过参数wait_timeout,然后重启mysqld服务来修改默认值。
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:
引起MySQL server has gone away错误的其它常见原因:
如果你使用Windows客户端,命令发送前服务器就已经删除连接(可能是因为wait_timeout已经过期)
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.
在windows平台,一些情况是MySQL通过TCP/IP连接至服务器时无法从系统级获取错误,而是当从连接试图读取应答时获取错误。
Prior to MySQL 5.1.8, 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.
在5.18以前的版本,尽管重连标志在mysql架构是1,但mysql不能自动重连和重新执行查询,因为它不知道服务器获取的查询是否是原始发送的。
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.
这种情况的解决方案就是用mysql_ping()连接确认自上次以为是否有长时间查询(MyODBC原理)。或者设置wait_timeout参数为很高的值,使连接不可能超时。
向服务器发送不正确或者太大的查询也会导致这种错误。如果mysqld收到的包太大或者无序,它就认为客户端发生错误而断开连接。如果需要大查询,可以修改max_allowed_packet变量,该变量默认值是1M。在客户端也需要设置最大包的大小。
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.
INSERT or REPLACE语句插入大量的行也会引起这种错误。这些语句不考虑插入的行数而发送单一的请求至服务器。因此,减少插入的行数可以避免经常发生这种错误。
如果客户端是4.0.8以前的版本而服务器是4.0.8以上的版本,当你发送16M以上的包时也会断开连接
如果解析主机名失败也可能看到这种错误。这是因为mysql依赖于主机系统解析主机名,但无法知道它是否有效。以mysql观点来看,在任何网络超时中这种情况都是不易察觉的。
You may also see the MySQL server has gone away error if MySQL is started with the --skip-networking option.
可以尝试使用--skip-networking来启动mysql.
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.
其中网络问题也会导致这种情况,如果mysql 3306端口被防火墙阻塞,就阻止了连接到mysql的任何连接。
如果应用产生子进程,所有子进程都试图使用同样的连接至mysql服务器,也同样会出现这种错误。可以把不同的子进程用不同的连接来避免这种问题。
执行查询时,服务器进程已经死掉,这是遇到的bug.
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.
你可以检查mysql服务是否已经死掉,执行重启和检查服务器启动时间。如果客户端连接中断是因为mysqld崩溃和重启,可以集中查找mysql崩溃的原因。