由於經常恢復系統,一直使用綠色版本的MySQL,近來出現一個問題,用客戶端鏈接後,經常出先一個錯誤:
MySQL Server has gone away
在網上查了寫資料,改了一下配置就好了。
我的改動是:
.\MySQL5\my.ini文件中改了一句話
原來的是:wait_timeout=10
改後的是:wait_timeout=2880000
改後直接保存,就行了,服務不用重啟,修改完直接保存就行了。
==========華===麗===的===分===割===線============
也許其他人遇到這個問題,不一定是這兒的原因,那麼,就把我在網上找到比較全面的分析放到下面:
有兩篇,第一篇比較直觀,第二篇比較深奧。
解決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 參數的作用是,用來控制其通信緩沖區的最大長度。