服務(wù)項(xiàng)目:網(wǎng)站建設(shè)、仿站、程序開(kāi)發(fā)、APP開(kāi)發(fā)設(shè)計(jì)、移動(dòng)網(wǎng)站開(kāi)發(fā)設(shè)計(jì)、企業(yè)網(wǎng)站設(shè)計(jì)、電子商務(wù)網(wǎng)站開(kāi)發(fā)、網(wǎng)站維護(hù)、網(wǎng)站推廣、UX/UI 、HTML5、CSS3、JS / Jquery ...
四川???萍加邢薰?></a></div>
                    <div   id=四川???萍加邢薰? title=
四川???萍加邢薰?(開(kāi)發(fā)設(shè)計(jì)官網(wǎng))TEL : 15308000360 / QQ : 38585404

您的位置:首頁(yè) > 技術(shù)經(jīng)驗(yàn) > 數(shù)據(jù)庫(kù) > 正文

MySQL server has gone away 問(wèn)題的解決方法
技術(shù)支持服務(wù)電話:15308000360 【7x24提供運(yùn)維服務(wù),解決各類系統(tǒng)/軟硬件疑難技術(shù)問(wèn)題】

MySQL server has gone away 問(wèn)題解決方法,需要的朋友可以參考下。

mysql出現(xiàn)ERROR : (2006, 'MySQL server has gone away') 的問(wèn)題意思就是指client和MySQL server之間的鏈接斷開(kāi)了。

造成這樣的原因一般是sql操作的時(shí)間過(guò)長(zhǎng),或者是傳送的數(shù)據(jù)太大(例如使用insert ... values的語(yǔ)句過(guò)長(zhǎng), 這種情況可以通過(guò)修改max_allowed_packed的配置參數(shù)來(lái)避免,也可以在程序中將數(shù)據(jù)分批插入)。

產(chǎn)生這個(gè)問(wèn)題的原因有很多,總結(jié)下網(wǎng)上的分析:

原因一. MySQL 服務(wù)宕了

判斷是否屬于這個(gè)原因的方法很簡(jiǎn)單,進(jìn)入mysql控制臺(tái),查看mysql的運(yùn)行時(shí)長(zhǎng)

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

1 row in set或者查看MySQL的報(bào)錯(cuò)日志,看看有沒(méi)有重啟的信息

如果uptime數(shù)值很大,表明mysql服務(wù)運(yùn)行了很久了。說(shuō)明最近服務(wù)沒(méi)有重啟過(guò)。
如果日志沒(méi)有相關(guān)信息,也表名mysql服務(wù)最近沒(méi)有重啟過(guò),可以繼續(xù)檢查下面幾項(xiàng)內(nèi)容。

原因二. mysql連接超時(shí)

即某個(gè)mysql長(zhǎng)連接很久沒(méi)有新的請(qǐng)求發(fā)起,達(dá)到了server端的timeout,被server強(qiáng)行關(guān)閉。
此后再通過(guò)這個(gè)connection發(fā)起查詢的時(shí)候,就會(huì)報(bào)錯(cuò)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鏈接在無(wú)操作28800秒后被自動(dòng)關(guān)閉

原因三. mysql請(qǐng)求鏈接進(jìn)程被主動(dòng)kill

這種情況和原因二相似,只是一個(gè)是人為一個(gè)是MYSQL自己的動(dòng)作

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

當(dāng)查詢的結(jié)果集超過(guò) max_allowed_packet 也會(huì)出現(xiàn)這樣的報(bào)錯(cuò)。定位方法是打出相關(guān)報(bào)錯(cuò)的語(yǔ)句。

用select * into outfile 的方式導(dǎo)出到文件,查看文件大小是否超過(guò) max_allowed_packet ,如果超過(guò)則需要調(diào)整參數(shù),或者優(yōu)化語(yǔ)句。

mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
1 row in set (0.00 sec)
修改參數(shù):

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)
以下是補(bǔ)充:
應(yīng)用程序(比如PHP)長(zhǎng)時(shí)間的執(zhí)行批量的MYSQL語(yǔ)句。執(zhí)行一個(gè)SQL,但SQL語(yǔ)句過(guò)大或者語(yǔ)句中含有BLOB或者longblob字段。比如,圖片數(shù)據(jù)的處理。都容易引起MySQL server has gone away。
今天遇到類似的情景,MySQL只是冷冷的說(shuō):MySQL server has gone away。
大概瀏覽了一下,主要可能是因?yàn)橐韵聨追N原因:
一種可能是發(fā)送的SQL語(yǔ)句太長(zhǎng),以致超過(guò)了max_allowed_packet的大小,如果是這種原因,你只要修改my.cnf,加大max_allowed_packet的值即可。
還有一種可能是因?yàn)槟承┰驅(qū)е鲁瑫r(shí),比如說(shuō)程序中獲取數(shù)據(jù)庫(kù)連接時(shí)采用了Singleton的做法,雖然多次連接數(shù)據(jù)庫(kù),但其實(shí)使用的都是同一個(gè)連接,而且程序中某兩次操作數(shù)據(jù)庫(kù)的間隔時(shí)間超過(guò)了wait_timeout(SHOW STATUS能看到此設(shè)置),那么就可能出現(xiàn)問(wèn)題。最簡(jiǎn)單的處理方式就是把wait_timeout改大,當(dāng)然你也可以在程序里時(shí)不時(shí)順手mysql_ping()一下,這樣MySQL就知道它不是一個(gè)人在戰(zhàn)斗。
解決MySQL server has gone away
1、應(yīng)用程序(比如PHP)長(zhǎng)時(shí)間的執(zhí)行批量的MYSQL語(yǔ)句。最常見(jiàn)的就是采集或者新舊數(shù)據(jù)轉(zhuǎn)化。
解決方案:
在my.cnf文件中添加或者修改以下兩個(gè)變量:
wait_timeout=2880000
interactive_timeout = 2880000
關(guān)于兩個(gè)變量的具體說(shuō)明可以google或者看官方手冊(cè)。如果不能修改my.cnf,則可以在連接數(shù)據(jù)庫(kù)的時(shí)候設(shè)置CLIENT_INTERACTIVE,比如:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)

2、執(zhí)行一個(gè)SQL,但SQL語(yǔ)句過(guò)大或者語(yǔ)句中含有BLOB或者longblob字段。比如,圖片數(shù)據(jù)的處理
解決方案:
在my.cnf文件中添加或者修改以下變量:
max_allowed_packet = 10M(也可以設(shè)置自己需要的大小)
max_allowed_packet 參數(shù)的作用是,用來(lái)控制其通信緩沖區(qū)的最大長(zhǎng)度。

最近做網(wǎng)站有一個(gè)站要用到WEB網(wǎng)頁(yè)采集器功能,當(dāng)一個(gè)PHP腳本在請(qǐng)求URL的時(shí)候,可能這個(gè)被請(qǐng)求的網(wǎng)頁(yè)非常慢慢,超過(guò)了mysql的 wait-timeout時(shí)間,然后當(dāng)網(wǎng)頁(yè)內(nèi)容被抓回來(lái)后,準(zhǔn)備插入到MySQL的時(shí)候,發(fā)現(xiàn)MySQL的連接超時(shí)關(guān)閉了,于是就出現(xiàn)了“MySQL server has gone away”這樣的錯(cuò)誤提示,解決這個(gè)問(wèn)題,我的經(jīng)驗(yàn)有以下兩點(diǎn),或許對(duì)大家有用處:
第 一種方法:
當(dāng)然是增加你的 wait-timeout值,這個(gè)參數(shù)是在my.cnf(在Windows下臺(tái)下面是my.ini)中設(shè)置,我的數(shù)據(jù)庫(kù)負(fù)荷稍微大一點(diǎn),所以,我設(shè)置的值 為10,(這個(gè)值的單位是秒,意思是當(dāng)一個(gè)數(shù)據(jù)庫(kù)連接在10秒鐘內(nèi)沒(méi)有任何操作的話,就會(huì)強(qiáng)行關(guān)閉,我使用的不是永久鏈接 (mysql_pconnect),用的是mysql_connect,關(guān)于這個(gè)wait-timeout的效果你可以在MySQL的進(jìn)程列表中看到 (show processlist) ),你可以把這個(gè)wait-timeout設(shè)置成更大,比如300秒,呵呵,一般來(lái)講300秒足夠用了,其實(shí)你也可以不用設(shè)置,MySQL默認(rèn)是8個(gè)小 時(shí)。情況由你的服務(wù)器和站點(diǎn)來(lái)定。
第二種方法:
這也是我個(gè)人認(rèn)為最好的方法,即檢查 MySQL的鏈接狀態(tài),使其重新鏈接。
可能大家都知道有mysql_ping這么一個(gè)函數(shù),在很多資料中都說(shuō)這個(gè)mysql_ping的 API會(huì)檢查數(shù)據(jù)庫(kù)是否鏈接,如果是斷開(kāi)的話會(huì)嘗試重新連接,但在我的測(cè)試過(guò)程中發(fā)現(xiàn)事實(shí)并不是這樣子的,是有條件的,必須要通過(guò) mysql_options這個(gè)C API傳遞相關(guān)參數(shù),讓MYSQL有斷開(kāi)自動(dòng)鏈接的選項(xiàng)(MySQL默認(rèn)為不自動(dòng)連接),但我測(cè)試中發(fā)現(xiàn)PHP的MySQL的API中并不帶這個(gè)函數(shù),你重新編輯MySQL吧,呵呵。但mysql_ping這個(gè)函數(shù)還是終于能用得上的,只是要在其中有一個(gè)小小的操作技巧:
這是我的的數(shù)據(jù)庫(kù)操 作類中間的一個(gè)函數(shù)

function ping(){
if(!mysql_ping($this->link)){
mysql_close($this->link); //注意:一定要先執(zhí)行數(shù)據(jù)庫(kù)關(guān)閉,這是關(guān)鍵
$this->connect();
}

}

我需要調(diào)用這個(gè)函數(shù)的代碼可能是這樣子的
$str = file_get_contents('http://www.xxx.com');

$db->ping();//經(jīng)過(guò)前面的網(wǎng)頁(yè)抓取后,或者會(huì)導(dǎo)致數(shù)據(jù)庫(kù)連接關(guān)閉,檢查并重新連接

$db->query('select * from table');

ping()這個(gè)函數(shù)先檢測(cè)數(shù)據(jù)連接是否正常,如果被關(guān)閉,整個(gè)把當(dāng)前腳本的MYSQL實(shí)例關(guān)閉,再重新連接。
經(jīng) 過(guò)這樣處理后,可以非常有效的解決MySQL server has gone away這樣的問(wèn)題,而且不會(huì)對(duì)系統(tǒng)造成額外的開(kāi)銷。
__________________________________________________________________________________________________
  今天遇到類似的情景,MySQL只是冷冷的說(shuō):MySQL server has gone away。
  大概瀏覽了一下,主要可能是因?yàn)橐韵聨追N原因:
  一種可能是發(fā)送的SQL語(yǔ)句太長(zhǎng),以致超過(guò)了max_allowed_packet的大小,如果是這種原因,你只要修改my.cnf,加大max_allowed_packet的值即可。
  還有一種可能是因?yàn)槟承┰驅(qū)е鲁瑫r(shí),比如說(shuō)程序中獲取數(shù)據(jù)庫(kù)連接時(shí)采用了Singleton的做法,雖然多次連接數(shù)據(jù)庫(kù),但其實(shí)使用的都是同一個(gè)連接,而且程序中某兩次操作數(shù)據(jù)庫(kù)的間隔時(shí)間超過(guò)了wait_timeout(SHOW STATUS能看到此設(shè)置),那么就可能出現(xiàn)問(wèn)題。最簡(jiǎn)單的處理方式就是把wait_timeout改大,當(dāng)然你也可以在程序里時(shí)不時(shí)順手mysql_ping()一下,這樣MySQL就知道它不是一個(gè)人在戰(zhàn)斗。
  解決MySQL server has gone away
  1、應(yīng)用程序(比如PHP)長(zhǎng)時(shí)間的執(zhí)行批量的MYSQL語(yǔ)句。最常見(jiàn)的就是采集或者新舊數(shù)據(jù)轉(zhuǎn)化。
  解決方案:
  在my.cnf文件中添加或者修改以下兩個(gè)變量:
wait_timeout=2880000
interactive_timeout = 2880000  關(guān)于兩個(gè)變量的具體說(shuō)明可以google或者看官方手冊(cè)。如果不能修改my.cnf,則可以在連接數(shù)據(jù)庫(kù)的時(shí)候設(shè)置CLIENT_INTERACTIVE,比如:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)
2、執(zhí)行一個(gè)SQL,但SQL語(yǔ)句過(guò)大或者語(yǔ)句中含有BLOB或者longblob字段。比如,圖片數(shù)據(jù)的處理
解決方案:
在my.cnf文件中添加或者修改以下變量:
max_allowed_packet = 10M(也可以設(shè)置自己需要的大小)
max_allowed_packet參數(shù)的作用是,用來(lái)控制其通信緩沖區(qū)的最大長(zhǎng)度。
1、應(yīng)用程序(比如PHP)長(zhǎng)時(shí)間的執(zhí)行批量的MYSQL語(yǔ)句。
最常見(jiàn)的就是采集或者新舊數(shù)據(jù)轉(zhuǎn)化。

解決方案:
my.ini文件中添加或者修改以下兩個(gè)變量:
wait_timeout=2880000
interactive_timeout = 2880000

關(guān)于兩個(gè)變量的具體說(shuō)明可以google或者看官方手冊(cè)。
如果不能修改my.cnf,則可以在連接數(shù)據(jù)庫(kù)的時(shí)候設(shè)置CLIENT_INTERACTIVE,比如:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)

2、執(zhí)行一個(gè)SQL,但SQL語(yǔ)句過(guò)大或者語(yǔ)句中含有BLOB或者longblob字段。
比如,圖片數(shù)據(jù)的處理

解決方案
my.cnf文件中添加或者修改以下變量:
max_allowed_packet = 10M (也可以設(shè)置自己需要的大小)
max_allowed_packet 參數(shù)的作用是,用來(lái)控制其通信緩沖區(qū)的最大長(zhǎng)度。

------------ 以下是網(wǎng)絡(luò)搜索的資料 -------------------


也許其他人遇到這個(gè)問(wèn)題,不一定是這兒的原因,那么,就把我在網(wǎng)上找到比較全面的分析放到下面:
有兩篇,第一篇比較直觀,第二篇比較深?yuàn)W。
解決MySQL server has gone away 2009-01-09 16:23:22
今天遇到類似的情景,MySQL只是冷冷的說(shuō):MySQL server has gone away。
大概瀏覽了一下,主要可能是因?yàn)橐韵聨追N原因:
一種可能是發(fā)送的SQL語(yǔ)句太長(zhǎng),以致超過(guò)了max_allowed_packet的大小,如果是這種原因,你只要修改my.cnf,加大max_allowed_packet的值即可。
還有一種可能是因?yàn)槟承┰驅(qū)е鲁瑫r(shí),比如說(shuō)程序中獲取數(shù)據(jù)庫(kù)連接時(shí)采用了Singleton的做法,雖然多次連接數(shù)據(jù)庫(kù),但其實(shí)使用的都是同一個(gè)連接,而且程序中某兩次操作數(shù)據(jù)庫(kù)的間隔時(shí)間超過(guò)了wait_timeout(SHOW STATUS能看到此設(shè)置),那么就可能出現(xiàn)問(wèn)題。最簡(jiǎn)單的處理方式就是把wait_timeout改大,當(dāng)然你也可以在程序里時(shí)不時(shí)順手mysql_ping()一下,這樣MySQL就知道它不是一個(gè)人在戰(zhàn)斗。
解決MySQL server has gone away
1、應(yīng)用程序(比如PHP)長(zhǎng)時(shí)間的執(zhí)行批量的MYSQL語(yǔ)句。最常見(jiàn)的就是采集或者新舊數(shù)據(jù)轉(zhuǎn)化。
解決方案:
在my.cnf文件中添加或者修改以下兩個(gè)變量:
wait_timeout=2880000
interactive_timeout = 2880000  
關(guān)于兩個(gè)變量的具體說(shuō)明可以google或者看官方手冊(cè)。如果不能修改my.cnf,則可以在連接數(shù)據(jù)庫(kù)的時(shí)候設(shè)置CLIENT_INTERACTIVE,比如:
sql = "set interactive_timeout=24*3600";
mysql_real_query(...)
2、執(zhí)行一個(gè)SQL,但SQL語(yǔ)句過(guò)大或者語(yǔ)句中含有BLOB或者longblob字段。比如,圖片數(shù)據(jù)的處理
解決方案:
my.cnf文件中添加或者修改以下變量:
max_allowed_packet = 10M
(也可以設(shè)置自己需要的大小)
max_allowed_packet
參數(shù)的作用是,用來(lái)控制其通信緩沖區(qū)的最大長(zhǎng)度
MySQL: 詭異的MySQL server has gone away及其解決
來(lái)自:http://fz9493.blog.sohu.com/38472203.html
jimmy | 15 三月, 2007 20:32
在Mysql執(zhí)行show status,通常更關(guān)注緩存效果、進(jìn)程數(shù)等,往往忽略了兩個(gè)值:
Variable_name Value
Aborted_clients 3792
Aborted_connects 376

通常只占query的0.0x%,所以并不為人所重視。而且在傳統(tǒng)Web應(yīng)用上,query錯(cuò)誤對(duì)用戶而言影響并不大,只是重新刷新一下頁(yè)面就OK了。最近的基礎(chǔ)改造中,把很多應(yīng)用作為service運(yùn)行,無(wú)法提示用戶重新刷新,這種情況下,可能就會(huì)影響到服務(wù)的品質(zhì)。
通過(guò)程序腳本的日志跟蹤,主要報(bào)錯(cuò)信息為“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.

據(jù)此分析,可能原因有3:
1,Mysql服務(wù)端與客戶端版本不匹配。
2,Mysql服務(wù)端配置有缺陷或者優(yōu)化不足
3,需要改進(jìn)程序腳本
通過(guò)更換多個(gè)服務(wù)端與客戶端版本,發(fā)現(xiàn)只能部分減少報(bào)錯(cuò),并不能完全解決。排除1。
對(duì)服務(wù)端進(jìn)行了徹底的優(yōu)化,也未能達(dá)到理想效果。在timeout的取值設(shè)置上,從經(jīng)驗(yàn)值的10,到PHP默認(rèn)的60,進(jìn)行了多次嘗試。而Mysql官方默認(rèn)值(8小時(shí))明顯是不可能的。從而對(duì)2也進(jìn)行了排除。(更多優(yōu)化的經(jīng)驗(yàn)分享,將在以后整理提供)
針對(duì)3對(duì)程序代碼進(jìn)行分析,發(fā)現(xiàn)程序中大量應(yīng)用了類似如下的代碼(為便于理解,用原始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. ]。在實(shí)際分析中發(fā)現(xiàn),if(!$conn)并不是可靠的,程序通過(guò)了if(!$conn)的檢驗(yàn)后,仍然會(huì)返回上述錯(cuò)誤。
對(duì)程序進(jìn)行了改寫:
if(!conn){ // connect ...}
elseif(!mysql_ping($conn)){ // reconnect ... }
mysql_query($sql, $conn);
經(jīng)實(shí)際觀測(cè),MySQL server has gone away的報(bào)錯(cuò)基本解決。
BTW: 附帶一個(gè)關(guān)于 reconnect 的疑問(wèn),
在php4x+client3x+mysql4x的舊環(huán)境下,reconnet的代碼:
$conn=mysql_connect(...) 可以正常工作。
但是,在php5x+client4x+mysql4x的新環(huán)境下,$conn=mysql_connect(...)返回的$conn有部分情況下不可用。需要書(shū)寫為:
mysql_close($conn);
$conn=mysql_connect(...);
返回的$conn才可以正常使用。原因未明。未做深入研究,也未見(jiàn)相關(guān)討論?;蛟Smysql官方的BUG匯報(bào)中會(huì)有吧。
~~呵呵~~
description:
remember that your MySQL "max_allowed_packet" configuration setting (default 1MB)
mysql 默認(rèn)最大能夠處理的是1MB
如果你在sql使用了大的text或者BLOB數(shù)據(jù),就會(huì)出現(xiàn)這個(gè)問(wèn)題。 php手冊(cè)上的注釋
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手冊(cè)上說(shuō)
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做數(shù)據(jù)庫(kù)還原的時(shí)候,由于有些數(shù)據(jù)很大,會(huì)出現(xiàn)這樣的錯(cuò)誤:The MySQL Server returned this Error:MySQL Error Nr.2006-MySQL server has gone away。我的一個(gè)150mb的備份還原的時(shí)候就出現(xiàn)了這錯(cuò)誤。解決的方法就是找到mysql安裝目錄,找到my.ini文件,在文件的最后添加:max_allowed_packet = 10M(也可以設(shè)置自己需要的大小)。 max_allowed_packet 參數(shù)的作用是,用來(lái)控制其通信緩沖區(qū)的最大長(zhǎng)度。



上一篇:MySQL數(shù)據(jù)類型和常用字段屬性總結(jié)
下一篇:windows下mysql忘記root密碼的解決方法

相關(guān)熱詞搜索:mysql