前言:
有時(shí)候,連接MySQL的會(huì)話經(jīng)常會(huì)異常退出,錯(cuò)誤日志里會(huì)看到"Got an error reading communication packets"類型的告警。本篇文章我們一起來(lái)討論下該錯(cuò)誤可能的原因以及如何來(lái)規(guī)避。
1.狀態(tài)變量Aborted_clients和Aborted_connects
首先我們來(lái)了解下Aborted_clients和Aborted_connects這兩個(gè)狀態(tài)變量的含義,當(dāng)出現(xiàn)會(huì)話異常退出時(shí),這兩個(gè)狀態(tài)值會(huì)有變化。根據(jù)官方文檔描述,總結(jié)如下:
![](http://img.jbzj.com/file_images/article/202008/2020828104718844.jpeg?2020728104729)
造成Aborted_connects狀態(tài)變量增加的可能原因:
- 客戶端試圖訪問(wèn)數(shù)據(jù)庫(kù),但沒(méi)有數(shù)據(jù)庫(kù)的權(quán)限。
- 客戶端使用了錯(cuò)誤的密碼。
- 連接包不包含正確的信息。
- 獲取一個(gè)連接包需要的時(shí)間超過(guò)connect_timeout秒。
![](http://img.jbzj.com/file_images/article/202008/2020828104815466.jpeg?2020728104824)
造成Aborted_clients狀態(tài)變量增加的可能原因:
- 程序退出前,客戶機(jī)程序沒(méi)有調(diào)用mysql_close()。
- 客戶端睡眠時(shí)間超過(guò)了wait_timeout或interactive_timeout參數(shù)的秒數(shù)。
- 客戶端程序在數(shù)據(jù)傳輸過(guò)程中突然終止。
簡(jiǎn)單來(lái)說(shuō)即:數(shù)據(jù)庫(kù)會(huì)話未能正常連接到數(shù)據(jù)庫(kù),會(huì)造成Aborted_connects變量增加。數(shù)據(jù)庫(kù)會(huì)話已正常連接到數(shù)據(jù)庫(kù)但未能正常退出,會(huì)造成Aborted_clients變量增加。
2.Got an error reading communication packets原因分析
哪種情況會(huì)導(dǎo)致error log中出現(xiàn)“Aborted connection xxxx to db: 'db' user: 'dbuser' host: 'hostname' (Got an error reading communication packets)”類似告警呢?下面我們根據(jù)上面可能的原因來(lái)做下具體測(cè)試。每次測(cè)試要注意狀態(tài)變量Aborted_clients和Aborted_connects的變化及錯(cuò)誤日志記錄。
- 測(cè)試一:錯(cuò)誤密碼,錯(cuò)誤用戶
1.測(cè)試前查看狀態(tài)變量值mysql> show global status like 'abort%';+------------------+-------+| Variable_name | Value |+------------------+-------+| Aborted_clients | 0 || Aborted_connects | 0 |+------------------+-------+
2.測(cè)試過(guò)程# mysql -uroot -pwrongpassmysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)# mysql -uroot1 -pwrongpassmysql: [Warning] Using a password on the command line interface can be insecure.ERROR 1045 (28000): Access denied for user 'root1'@'localhost' (using password: YES)
3.查看狀態(tài)變化及錯(cuò)誤日志mysql> show global status like 'abort%';+------------------+-------+| Variable_name | Value |+------------------+-------+| Aborted_clients | 0 || Aborted_connects | 2 |+------------------+-------+錯(cuò)誤日志記錄:2020-03-16T17:58:35.318819+08:00 6 [Note] Access denied for user 'root'@'localhost' (using password: YES)2020-03-16T17:59:04.153753+08:00 7 [Note] Access denied for user 'root1'@'localhost' (using password: YES)
結(jié)果:Aborted_connects有增加 error log無(wú)Aborted connection相關(guān)記錄
- 測(cè)試二:睡眠時(shí)間超時(shí)或手動(dòng)殺會(huì)話
1.測(cè)試前查看狀態(tài)變量值mysql> show global status like 'abort%';+------------------+-------+| Variable_name | Value |+------------------+-------+| Aborted_clients | 0 || Aborted_connects | 2 |+------------------+-------+
2.手動(dòng)殺會(huì)話測(cè)試mysql> show processlist;+----+------+-----------+------+---------+------+----------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+-----------+------+---------+------+----------+------------------+| 9 | root | localhost | NULL | Query | 0 | starting | show processlist || 10 | root | localhost | NULL | Sleep | 7 | | NULL |+----+------+-----------+------+---------+------+----------+------------------+2 rows in set (0.00 sec)mysql> kill 10;Query OK, 0 rows affected (0.00 sec)
3.查看狀態(tài)變化及錯(cuò)誤日志mysql> show global status like 'abort%';+------------------+-------+| Variable_name | Value |+------------------+-------+| Aborted_clients | 1 || Aborted_connects | 2 |+------------------+-------+
結(jié)果:Aborted_clients有增加 error log無(wú)記錄 ,類似的,睡眠時(shí)間超時(shí)后Aborted_clients有增加 error log中有Aborted connection相關(guān)記錄。
會(huì)話異常退出一般會(huì)造成Aborted connection告警,即我們可以通過(guò)Aborted_clients狀態(tài)變量的變化來(lái)反映出是否存在異常會(huì)話,那么出現(xiàn)“Got an error reading communication packets” 類似告警的原因就很明了了,查詢相關(guān)資料,總結(jié)出造成Aborted connection告警的可能原因如下:
- 會(huì)話鏈接未正常關(guān)閉,程序沒(méi)有調(diào)用mysql_close()。
- 睡眠時(shí)間超過(guò)wait_timeout或interactive_timeout參數(shù)的秒數(shù)。
- 查詢數(shù)據(jù)包大小超過(guò)max_allowed_packet數(shù)值,造成鏈接中斷。
- 其他網(wǎng)絡(luò)或者硬件層面的問(wèn)題。
3.問(wèn)題避免與總結(jié)
其實(shí)Aborted connection告警是很難避免的,error log里或多或少會(huì)有少量Aborted connection信息,這種情況是可以忽略的,但是當(dāng)你的error log里頻繁出現(xiàn)Aborted connection告警,這時(shí)候就應(yīng)該注意了,可能會(huì)對(duì)業(yè)務(wù)產(chǎn)生較大的影響。下面列舉出幾點(diǎn)避免錯(cuò)誤的建議,希望對(duì)你有所幫助。
- 建議業(yè)務(wù)操作結(jié)束后,應(yīng)用程序邏輯會(huì)正確關(guān)閉連接,以短連接替代長(zhǎng)連接。
- 檢查以確保max_allowed_packet的值足夠高,并且客戶端沒(méi)有收到“數(shù)據(jù)包太大”消息。
- 確??蛻舳藨?yīng)用程序不中止連接,例如,如果PHP設(shè)置了max_execution_time為5秒,增加connect_timeout并不會(huì)起到作用,因?yàn)镻HP會(huì)kill腳本。其他程序語(yǔ)言和環(huán)境也有類似的安全選項(xiàng)。
- 確保事務(wù)提交(begin和commit)都正確提交以保證一旦應(yīng)用程序完成以后留下的連接是處于干凈的狀態(tài)。
- 檢查是否啟用了skip-name-resolve,檢查主機(jī)根據(jù)其IP地址而不是其主機(jī)名進(jìn)行身份驗(yàn)證。
- 嘗試增加MySQL的net_read_timeout和net_write_timeout值,看看是否減少了錯(cuò)誤的數(shù)量。
以上就是MySQL Aborted connection告警日志的分析的詳細(xì)內(nèi)容,更多關(guān)于MySQL Aborted connection告警日志的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- MySQL系列之redo log、undo log和binlog詳解
- 詳解MySQL 重做日志(redo log)與回滾日志(undo logo)
- mysql中的7種日志小結(jié)
- MySQL使用binlog日志做數(shù)據(jù)恢復(fù)的實(shí)現(xiàn)
- MySQL 一則慢日志監(jiān)控誤報(bào)的問(wèn)題分析與解決
- MySQL慢查詢?nèi)罩镜淖饔煤烷_(kāi)啟
- MySQL 慢查詢?nèi)罩镜拈_(kāi)啟與配置
- MySQL中的undo日志
- 詳解監(jiān)聽(tīng)MySQL的binlog日志工具分析:Canal
- MYSQL SERVER收縮日志文件實(shí)現(xiàn)方法
- MySQL 撤銷日志與重做日志(Undo Log與Redo Log)相關(guān)總結(jié)