MySQL server has gone away報(bào)錯(cuò)原因分析

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

判斷是否屬于這個(gè)原因的方法很簡(jiǎn)單,執(zhí)行以下命令,查看mysql的運(yùn)行時(shí)長(zhǎng)

$ mysql -uroot -p -e "show global status like 'uptime';"

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| Uptime? ? ? ? | 68928 |

+---------------+-------+

1 row in set (0.04 sec)

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

$ 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數(shù)值很大,表明mysql服務(wù)運(yùn)行了很久了。說(shuō)明最近服務(wù)沒(méi)有重啟過(guò)。

如果日志沒(méi)有相關(guān)信息,也表名mysql服務(wù)最近沒(méi)有重啟過(guò),可以繼續(xù)檢查下面幾項(xiàng)內(nèi)容。

2. 連接超時(shí)

如果程序使用的是長(zhǎng)連接,則這種情況的可能性會(huì)比較大。

即,某個(gè)長(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

$ 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. 進(jìn)程在server端被主動(dòng)kill

這種情況和情況2相似,只是發(fā)起者是DBA或者其他job。發(fā)現(xiàn)有長(zhǎng)時(shí)間的慢查詢執(zhí)行kill xxx導(dǎo)致。

$ mysql -uroot -p -e "show global status like 'com_kill'"

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| Com_kill? ? ? | 0? ? |

+---------------+-------+

4. 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)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容