經過案例分析MySQL中使人頭疼的Aborted告警
轉載 2017-06-29 做者:dbapower
我要評論mysql
這篇文章經過案例跟你們分析了MySQL中使人頭疼的Aborted告警的相關資料,文中將Aborted告警介紹的很是詳細,對你們具備必定的參考學習價值,須要的朋友們下面來一塊兒看看吧。sql
本文主要給你們介紹的是關於MySQL中Aborted告警的相關內容,分享出來供你們參考學習,下面來一塊兒看看詳細的介紹:數據庫
實戰編程
Part1:寫在最前服務器
在MySQL的error log中,咱們會常常性看到一些各種的Aborted connection錯誤,本文中會針對這類錯誤進行一個初步分析,並瞭解一個問題產生後的基本排查思路和方法。掌握這種方法是相當重要的,而不是出現問題了,去猜,去試。數據庫出現問題的時候須要DBA在短期內快速解決問題,所以一個好與壞的DBA,區別也在於此。編程語言
Part2:種類工具
?學習
1spa 2日誌 3 4 5 6 |
[Warning] Aborted connection 305628 to db: 'db' user : 'dbuser' host: 'hostname' (Got an error reading communication packets) [Warning] Aborted connection 81 to db: 'unconnected' user : 'root' host: '127.0.0.1' (Got timeout reading communication packets) [Warning] Aborted connection 109 to db: 'helei1' user : 'sys_admin' host: '192.168.1.1' (Got an error writing communication packets) [Warning] Access denied for user 'root' @ '127.0.0.1' (using password : YES) [Warning] Got an error writing communication packets |
Part3:重點參數分析
wait_timeout
Command-Line Format |
--wait-timeout=# |
System Variable |
Name |
wait_timeout |
Variable Scope |
Global, Session |
Dynamic Variable |
Yes |
Permitted Values (Windows) |
Type |
integer |
Default |
28800 |
Min Value |
1 |
Max Value |
2147483 |
Permitted Values (Other) |
Type |
integer |
Default |
28800 |
Min Value |
1 |
Max Value |
31536000 |
這個參數指的是數據庫系統在關閉它以前,服務器等待非交互式鏈接上的活動的秒數。
interactive_timeout
Command-Line Format |
--interactive-timeout=# |
System Variable |
Name |
interactive_timeout |
Variable Scope |
Global, Session |
Dynamic Variable |
Yes |
Permitted Values |
Type |
integer |
Default |
28800 |
Min Value |
1 |
這個參數指的是在關閉交互式鏈接以前,服務器等待活動的秒數
Warning:警告這兩個參數建議一塊兒調節,可以避免一些坑。
本文的兩個參數值採用的是默認值
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
mysql> show global variables like '%timeout%' ; + ----------------------------+----------+ | Variable_name | Value | + ----------------------------+----------+ | connect_timeout | 10 | | 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 | + ----------------------------+----------+ 10 rows in set (0.01 sec) |
另外在數據庫中,咱們重點關注下這兩個參數,看看什麼狀況下Aborted_clients會提高,什麼狀況下Aborted_connects 會提高
?
1 2 3 4 5 6 7 8 |
mysql>show global status like 'aborted%' ; + ------------------+-------+ |Variable_name | Value | + ------------------+-------+ |Aborted_clients | 19 | |Aborted_connects | 0 | + ------------------+-------+ 2 rows inset (0.00 sec) |
Part4:案例1
這裏我故意輸入錯誤的密碼5次,來看下數據庫的error log和Aborted的哪一個參數記載了這一問題
?
1 2 3 4 5 6 7 8 9 10 |
[root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 ERROR 1045 (28000): Access denied for user 'root' @ '127.0.0.1' (using password : YES) [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 ERROR 1045 (28000): Access denied for user 'root' @ '127.0.0.1' (using password : YES) [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 ERROR 1045 (28000): Access denied for user 'root' @ '127.0.0.1' (using password : YES) [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 ERROR 1045 (28000): Access denied for user 'root' @ '127.0.0.1' (using password : YES) [root@HE3~]# mysql -uroot -pwrongpass -h127.0.0.1 ERROR 1045 (28000): Access denied for user 'root' @ '127.0.0.1' (using password : YES) |
能夠看出,這裏的Aborted_connects 記錄了密碼錯誤的這一問題
?
1 2 3 4 5 6 7 8 |
mysql>show global status like 'aborted%' ; + ------------------+-------+ |Variable_name | Value | + ------------------+-------+ |Aborted_clients | 19 | |Aborted_connects | 5 | + ------------------+-------+ 2 rows inset (0.00 sec) |
error log中,也記載了這類密碼輸錯的信息
?
1 2 3 4 5 |
[Warning] Access denied for user 'root' @ '127.0.0.1' (using password : YES) [Warning] Access denied for user 'root' @ '127.0.0.1' (using password :YES) [Warning] Access denied for user 'root' @ '127.0.0.1' (using password :YES) [Warning] Access denied for user 'root' @ '127.0.0.1' (using password :YES) [Warning] Access denied for user 'root' @ '127.0.0.1' (using password :YES) |
Part5:案例2
接下來咱們看下文章第三節提到的兩個重點參數對數據庫鏈接的行爲影響
這裏咱們將這兩個參數均配置爲10秒
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
mysql> set global wait_timeout=10; Query OK,0 rows affected (0.00 sec) mysql> set global interactive_timeout=10; Query OK,0 rows affected (0.00 sec) mysql>show processlist; ERROR 2006 (HY000): MySQL server has gone away No connection . Trying to reconnect... Connection id: 79 Current database : *** NONE *** + ----+------+-----------------+------+---------+------+-------+------------------+ | Id | User | Host | db | Command | Time | State | Info | + ----+------+-----------------+------+---------+------+-------+------------------+ | 79 |root | 127.0.0.1:42016 | NULL | Query | 0 | NULL | show processlist | + ----+------+-----------------+------+---------+------+-------+------------------+ 1 row in set (0.00 sec) |
這裏三次操做,能夠看到clients數上升,這是因爲timeout參數控制的,已經鏈接上數據的鏈接被殺掉。
?
1 2 3 4 5 6 7 8 9 10 11 |
mysql>show global status like 'aborted%' ; ERROR 2006 (HY000): MySQL server has gone away No connection . Trying to reconnect... Connection id: 81 Current database : *** NONE *** + ------------------+-------+ |Variable_name | Value | + ------------------+-------+ |Aborted_clients | 22 | |Aborted_connects | 5 | + ------------------+-------+ 2 rows in set (0.01 sec) |
error log中記載的是
?
1 2 3 |
[Warning] Aborted connection 81 to db: 'unconnected' user : 'root' host: '127.0.0.1' (Got timeout reading communication packets) [Warning] Aborted connection 78 to db: 'unconnected' user : 'root' host: '127.0.0.1' (Got timeout reading communication packets) [Warning] Aborted connection 79 to db: 'unconnected' user : 'root' host: '127.0.0.1' (Got timeout reading communication packets) |
Part6:案例3
在這個案例中咱們看下最大鏈接數對數據庫鏈接的行爲影響
?
1 2 3 4 5 6 7 8 9 10 11 12 |
mysql>show global variables like 'max_conn%' ; + --------------------+-------+ |Variable_name | Value | + --------------------+-------+ |max_connect_errors | 1000 | |max_connections | 1024 | + --------------------+-------+ 2 rows in set (0.00 sec) mysql> set global max_connections=2; Query OK,0 rows affected (0.00 sec) |
這裏看到爆出了鏈接數過多的問題
?
1 2 |
[root@HE3~]# mysql -uroot -pMANAGER -h127.0.0.1 ERROR 1040 (HY000): Too many connections |
而錯誤日誌沒有任何記錄
Part7:案例4
第三方工具navicat select結果沒有出來的時候選擇中止則出現
clients上漲
?
1 2 3 4 5 6 7 8 |
mysql>show global status like 'aborted%' ; + ------------------+-------+ |Variable_name | Value | + ------------------+-------+ |Aborted_clients | 28 | |Aborted_connects | 10 | + ------------------+-------+ 2 rows in set (0.00 sec) |
error log日誌記錄
?
1 |
170626 16:26:56 [Warning] Aborted connection 109 to db: 'helei1' user : 'sys_admin' host: '192.168.1.1' (Got an error writing communication packets) |
Part8:緣由總結
- 在MySQL中sleep狀態數百秒的並且常常重複鏈接是應用程序在工做後沒有關閉鏈接的症狀之一,而是依靠數據庫wait_timeout來關閉它們。強烈建議在操做結束時更改應用程序邏輯以正確關閉鏈接;
- 檢查以確保max_allowed_packet的值足夠高,而且客戶端沒有收到「數據包太大」消息。 這種狀況他會停止鏈接,而不正確關閉它;
- 另外一種可能性是TIME_WAIT。建議您確認鏈接被妥善管理而且是在應用端正常關閉;
- 確保事務正確提交(開始和提交),以便一旦應用程序「完成」鏈接,它將處於「clean」的狀態;
- 您應該確保客戶端應用程序不停止鏈接。 例如,若是PHP的選項max_execution_time設置爲5秒,增長connect_timeout是沒用的,由於PHP會殺死腳本。 其餘編程語言和環境也有相似的選項;
- 鏈接延遲的另外一個緣由是DNS問題。 檢查是否啓用了skip-name-resolve,檢查主機根據其IP地址而不是其主機名進行身份驗證;
- 嘗試增長MySQL的net_read_timeout和net_write_timeout值,看看是否減小了錯誤的數量。
總結
經過這4個案例,咱們可以瞭解到,Aborted_clients、和Aborted_connects的區別,以及什麼狀況下會爆出什麼樣的錯誤日誌,文章第二節中的幾個Aborted錯誤是常見的錯誤,這類錯誤出現的時候腦海裏要有一個理論知識,知道什麼狀況下,會出現什麼樣的錯誤,以便快速定位問題。因爲筆者的水平有限,編寫時間也很倉促,文中不免會出現一些錯誤或者不許確的地方,不妥之處懇請讀者批評指正。
好了,以上就是這篇文章的所有內容了,但願本文的內容對你們的學習或者工做能帶來必定的幫助,若是有疑問你們能夠留言交流,謝謝你們對腳本之家的支持。