MySQL server has gone away問題得解決方案


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

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

產生這個問題的原因有很多,總結下網上的分析得到一個最重要得:

原因: Your SQL statement was too large.

當查詢的結果集超過 max_allowed_packet 也會出現這樣的報錯。定位方法是打出相關報錯的語句。

用select * into outfile 的方式導出到文件,查看文件大小是否超過 max_allowed_packet ,如果超過則需要調整參數,或者優化語句。

mysql> show global variables like 'max_allowed_packet';
+--------------------+---------+
| Variable_name      | Value   |
+--------------------+---------+
| max_allowed_packet | 1048576 |
+--------------------+---------+
1 row in set (0.00 sec)

修改參數: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)

------------ 以下是網絡搜索的資料 -------------------


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 參數的作用是,用來控制其通信緩沖區的最大長度。

 

轉載:http://www.jb51.net/article/23781.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM