MySQL server has gone away(在執行sql的時候,莫名的報錯)

原文:https://cenalulu.github.io/mysql/mysql-has-gone-away/

MySQL Server has gone away報錯緣由彙總分析

本文將總結和彙總MySQL Server has gone away這類報錯發生的緣由mysql

背景:在平時和開發的交流 以及 在論壇回答問題的或稱中會發現這個問題被問及的頻率很是高。
程序中報錯: MySQL server has gone away 是什麼意思? 如何避免?
所以,感受有必要總結一下發生這個問題的緣由。今天正好看到一篇外文blog總結的比較好,就翻譯過來了
原文:http://ronaldbradford.com/blog/sqlstatehy000-general-error-2006-mysql-server-has-gone-away-2013-01-02/git


緣由1. MySQL 服務宕了

判斷是否屬於這個緣由的方法很簡單,執行如下命令,查看mysql的運行時長github

$ mysql -uroot -p -e "show global status like 'uptime';" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Uptime | 68928 | +---------------+-------+ 1 row in set (0.04 sec)

或者查看MySQL的報錯日誌,看看有沒有重啓的信息sql

$ tail /var/log/mysql/error.log 130101 22:22:30 InnoDB: Initializing buffer pool, size = 256.0M 130101 22:22:30 InnoDB: Completed initialization of buffer pool 130101 22:22:30 InnoDB: highest supported file format is Barracuda. 130101 22:22:30 InnoDB: 1.1.8 started; log sequence number 63444325509 130101 22:22:30 [Note] Server hostname (bind-address): '127.0.0.1'; port: 3306 130101 22:22:30 [Note] - '127.0.0.1' resolves to '127.0.0.1'; 130101 22:22:30 [Note] Server socket created on IP: '127.0.0.1'. 130101 22:22:30 [Note] Event Scheduler: Loaded 0 events 130101 22:22:30 [Note] /usr/sbin/mysqld: ready for connections. Version: '5.5.28-cll' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)

若是uptime數值很大,代表mysql服務運行了好久了。說明最近服務沒有重啓過。 若是日誌沒有相關信息,也說明mysql服務最近沒有重啓過,能夠繼續檢查下面幾項內容。bash


緣由2. 鏈接超時

若是程序使用的是長鏈接,則這種狀況的可能性會比較大。 即,某個長鏈接好久沒有新的請求發起,達到了server端的timeout,被server強行關閉。 此後再經過這個connection發起查詢的時候,就會報錯server has gone awaysocket

$ mysql -uroot -p -e "show global variables like '%timeout';" +----------------------------+----------+ | Variable_name | Value | +----------------------------+----------+ | connect_timeout | 30 | | 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 | +----------------------------+----------+ mysql> SET SESSION wait_timeout=5; # Wait 10 seconds mysql> SELECT NOW(); ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 132361 Current database: *** NONE *** +---------------------+ | NOW() | +---------------------+ | 2013-01-02 11:31:15 | +---------------------+ 1 row in set (0.00 sec)

緣由3. 進程在server端被主動kill

這種狀況和狀況2類似,只是發起者是DBA或者其餘job。發現有長時間的慢查詢執行kill xxx致使。優化

$ mysql -uroot -p -e "show global status like 'com_kill'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_kill | 0 | +---------------+-------+

緣由4. Your SQL statement was too large.

當查詢的結果集超過 max_allowed_packet 也會出現這樣的報錯。定位方法是打出相關報錯的語句。 用select * into outfile 的方式導出到文件,查看文件大小是否超過max_allowed_packet ,若是超過則須要調整參數,或者優化語句。spa

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)
相關文章
相關標籤/搜索