mysql報General error 2006 MySQL server has gone away...錯誤解決方案

來源:互聯網
上載者:User

一個定時指令碼select表的時候頻繁報錯,具體錯誤如:...... General error: 2006 MySQL server has gone away. The SQL statement executed was: SELECT * FROM ......,檢查了一下發現是由於這是一個迴圈裡面的select查詢,而兩次select的查詢間隔超過了資料庫定義的wait_timeout時間長度,即mysql長串連很久沒有新的請求發起,達到了server端的wait_timeout的值後,被server強行關閉,即MySQL連結逾時,經過一番測試,得出解決辦法如下:

使用 mysql_ping() 函數 Ping 一個伺服器串連,如果存在串連,則返回 true。如果失敗,則返回 false。這裡如果沒有串連則重新串連。

<?php
class MySQL(){
    private $link;
    ......
    function connect(){
        $this->link=mysql_connect('localhost','root','123456');
    }
    ......
    function ping(){
        if(!mysql_ping($this->link)){
            mysql_close($this->link);//注意:一定要先執行資料庫關閉,這是關鍵
            $this->connect();//串連MySQL,當然這個方法根據自己的情況進行修改
        }
    }
    ......
}

具體查看mysql伺服器相關timeout的設定可使用如下查詢命令:

    show global variables like '%timeout';

得到的結果可能如下:

+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 60       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         | 30       |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                | 30       |
+-----------------------------+----------+

當然可能還有其他的情況會引起MySQL報 MySQL server has gone away 這種錯誤了,這裡就不一一詳解了。


MySQL server has gone away 報錯原因分析及解決辦法

原因1. MySQL 服務宕了

判斷是否屬於這個原因的方法很簡單,執行以下命令,查看mysql的運行時間長度

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



或者查看MySQL的報錯日誌,看看有沒有重啟的資訊

$ 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服務最近沒有重啟過,可以繼續檢查下面幾項內容。

2. 連線逾時

如果程式使用的是長串連,則這種情況的可能性會比較大。

即,某個長串連很久沒有新的請求發起,達到了server端的timeout,被server強行關閉。

此後再通過這個connection發起查詢的時候,就會報錯server has gone away

$ 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 ,如果超過則需要調整參數,或者最佳化語句。

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 |
+--------------------+----------+

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.