MYSQL連線相關引數和狀態值詳解

Rangle發表於2018-01-08

針對mysql的連線引數和狀態值,本文做些介紹和對比

一、MYSQL連線引數變數

1、常用連線數限制引數

show variables like '%connect%';

| max_connect_errors | 999999999 |      ##允許單使用者連線錯誤最大值,超過後在不重新整理狀態的情況下,禁止該使用者新連線
| max_connections | 6000 |           ##例項最大連線數限制
| max_user_connections | 0 |          ##但使用者連線最大限制,預設0表示無限制,遵守總連線數小於等於max_connections
| connect_timeout  | 10     |         ##使用者連線超時限制,超過10秒,如果依舊無法連線到mysql,則終止連線

2、超時引數

mysql -e "show variables  like '%timeout%'" 

+-----------------------------+----------+
| connect_timeout             | 10       |    ##連線超時,10秒
| delayed_insert_timeout      | 300      |    ##延遲插入超時時間,300秒
| have_statement_timeout      | YES      |    ##
| innodb_flush_log_at_timeout | 1        |    ##重新整理redo log buffer超時時間,1秒
| innodb_lock_wait_timeout    | 120      |    ##事務等待獲取資源等待的最長時間,超過這個時間還未分配到資源則會返回應用失敗,120秒
| innodb_rollback_on_timeout  | ON       |
| interactive_timeout         | 28800    |    ##mysql客戶端互動連線超時時間,預設8小時,用於控制sleep超時
| lock_wait_timeout           | 31536000 |    ##主要針對DDL產生的metadata locks超時時間
| net_read_timeout            | 60       |    ##網路讀取資料超時時間,60秒
| net_write_timeout           | 60       |    ##為網路寫入資料超時間60秒
| rpl_stop_slave_timeout      | 31536000 |    ##停止從庫服務超時時間
| slave_net_timeout           | 60       |    ##slave網路超時時間
| thread_pool_idle_timeout    | 60       |
| wait_timeout                | 28800    |    ##jdbc/odbc連線超時時間,預設8小時,用於控制sleep超時
+-----------------------------+----------+

 

二、MySQL連線狀態變數

1、常用連結狀態變數

show global status like '%connect%';

+-----------------------------------------------+----------+
| Connection_errors_max_connections             | 1906160  |    ##使用者最大錯誤連線數
| Connections                                   | 87341259 |    ##歷史使用者連線累積值
| Max_used_connections                          | 10242    |    ##歷史最大連線使用者數,processlist看到的連線數
| Threads_connected                             | 298      |    ##當前使用者連線數,當前processlist數

show global status like '%Threads_running%'; 

| Threads_running | 2     |                        ##當前使用者連線處於running狀態的數目,非sleep

show global status like '%abort%';

+------------------+----------+
| Aborted_clients  | 85050948 |                      ##非法終止客戶端連線的狀態統計,例如通過kill終止連線
| Aborted_connects | 1905528  |                      ##非法連線操作狀態統計,例如使用者密碼錯誤

 

三、常見問題分析

1、最大連線數超出限制

 動態調整最大連線引數變數,然後通過連線嘗試

mysql> set global max_connections=2;
[root@localhost ~]# mysql
ERROR 1040 (HY000): Too many connections
##此時連線超出的嘗試,不會經過許可權驗證,因此不會記錄Aborted_connects變數值

如遇到此問題即表示使用者連線超出了最大連線限制,需要增大連線數

mysql> set global max_connections=350;
##動態調整後,需要將/etc/my.cnf配置引數中的max_connections=350也要做相應調整,這個引數可以根據狀態量值Max_used_connections 評估

2、使用者連線錯誤數超出限制

動態調整最大連線錯誤數變數限制,然後通過嘗試失敗登入,主機對應的普通使用者會被阻塞,超級使用者不限

max_connect_errors官方解釋如下:

If more than this many successive connection requests from a host are interrupted without a successful connection, 
the server blocks that host from further connections. You can unblock blocked hosts by flushing the host cache.

To do so, issue a FLUSH HOSTS statement or execute a mysqladmin flush-hosts command.

If a connection is established successfully within fewer than max_connect_errors attempts after a previous connection was interrupted,
the error count for the host is cleared to zero. However, once a host is blocked, flushing the host cache is the only way to unblock it.
The default is 100.

手動調整引數測試(telnet 10.1.0.1 3306 五次斷鏈,使用者會被鎖)

mysql> set global max_connect_errors=5;

 通過測試發現使用者直接錯誤密碼連線,不會導致使用者被鎖,主要通過performance_schema下的host_cache表檢視相關阻塞資訊

mysql> select IP,HOST,HOST_VALIDATED,SUM_CONNECT_ERRORS ,COUNT_HANDSHAKE_ERRORS,COUNT_HOST_BLOCKED_ERRORS   from host_cache;
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
| IP             | HOST  | HOST_VALIDATED | SUM_CONNECT_ERRORS | COUNT_HANDSHAKE_ERRORS | COUNT_HOST_BLOCKED_ERRORS |
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
| 10.1.0.1    | NULL  | NO             |                  0 |                      5 |                         0 |
| 10.1.0.100 | node1 | YES            |                  0 |                      0 |                         0 |
+----------------+-------+----------------+--------------------+------------------------+---------------------------+

哪些連線會導致使用者被鎖?導致必須通過flush hosts釋放host_cache,具體如下:

| COUNT_NAMEINFO_TRANSIENT_ERRORS            | bigint(20)       | NO   |     | NULL                |       |
| COUNT_NAMEINFO_PERMANENT_ERRORS            | bigint(20)       | NO   |     | NULL                |       |
| COUNT_FORMAT_ERRORS                        | bigint(20)       | NO   |     | NULL                |       |
| COUNT_ADDRINFO_TRANSIENT_ERRORS            | bigint(20)       | NO   |     | NULL                |       |
| COUNT_ADDRINFO_PERMANENT_ERRORS            | bigint(20)       | NO   |     | NULL                |       |
| COUNT_FCRDNS_ERRORS                        | bigint(20)       | NO   |     | NULL                |       |
| COUNT_HOST_ACL_ERRORS                      | bigint(20)       | NO   |     | NULL                |       |
| COUNT_NO_AUTH_PLUGIN_ERRORS                | bigint(20)       | NO   |     | NULL                |       |
| COUNT_AUTH_PLUGIN_ERRORS                   | bigint(20)       | NO   |     | NULL                |       |
| COUNT_HANDSHAKE_ERRORS                     | bigint(20)       | NO   |     | NULL                |       |
| COUNT_PROXY_USER_ERRORS                    | bigint(20)       | NO   |     | NULL                |       |
| COUNT_PROXY_USER_ACL_ERRORS                | bigint(20)       | NO   |     | NULL                |       |
| COUNT_AUTHENTICATION_ERRORS                | bigint(20)       | NO   |     | NULL                |       |
| COUNT_SSL_ERRORS                           | bigint(20)       | NO   |     | NULL                |       |
| COUNT_MAX_USER_CONNECTIONS_ERRORS          | bigint(20)       | NO   |     | NULL                |       |
| COUNT_MAX_USER_CONNECTIONS_PER_HOUR_ERRORS | bigint(20)       | NO   |     | NULL                |       |
| COUNT_DEFAULT_DATABASE_ERRORS              | bigint(20)       | NO   |     | NULL                |       |
| COUNT_INIT_CONNECT_ERRORS                  | bigint(20)       | NO   |     | NULL                |       |
| COUNT_LOCAL_ERRORS                         | bigint(20)       | NO   |     | NULL                |       |
| COUNT_UNKNOWN_ERRORS                       | bigint(20)       | NO   |     | NULL                |       |

解決方法就是:flush hosts或mysqladmin flush-hosts 

mysql> select ip,host,HOST_VALIDATED,SUM_CONNECT_ERRORS ,COUNT_HANDSHAKE_ERRORS,COUNT_HOST_BLOCKED_ERRORS   from host_cache;
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
| ip             | host  | HOST_VALIDATED | SUM_CONNECT_ERRORS | COUNT_HANDSHAKE_ERRORS | COUNT_HOST_BLOCKED_ERRORS |
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
| 10.1.0.1    | NULL  | NO             |                  0 |                      5 |                         0 |
| 10.1.0.100 | node1 | YES            |                  0 |                      0 |                         0 |
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
2 rows in set (0.00 sec)

mysql> flush hosts;
Query OK, 0 rows affected (0.00 sec)

mysql> select ip,host,HOST_VALIDATED,SUM_CONNECT_ERRORS ,COUNT_HANDSHAKE_ERRORS,COUNT_HOST_BLOCKED_ERRORS   from host_cache;
Empty set (0.00 sec)

3、aborted連線值非常高

abort連線狀態值主要有兩個,分別是 Aborted_clients和Aborted_connects,其中兩個變數值含義如下:

Aborted_clientsThe number of connections that were aborted because the client died without closing the connection properly. 

Aborted_connects:The number of failed attempts to connect to the MySQL server

下面的狀態變數主要是使用者最大連線超過後,有多少嘗試連線的錯誤統計:

Connection_errors_max_connections:The number of connections refused because the server max_connections limit was reached

Aborted_clients和Aborted_connects引數對比,兩個狀態值在什麼情況下會變化?

Aborted_clients主要是因為客戶端非正常關閉,次值就會不斷增加,具體場景如下:

(1)手動殺死連線,kill的方式
(2)mysql客戶端超時interactive_timeout,結束sleep
(3)程式超時wait_timeout,異常結束sleep
(4)The client program did not call mysql_close() before exiting.
(5)The client program ended abruptly in the middle of a data transfer.

Aborted_connects主要是連線Mysql失敗的次數,具體場景如下:

(1)使用者名稱密碼錯誤
(2)登陸許可權不足
(3)連線超時,connect_timeout
(4) max_allowed_packet variable value is too small or queries require more memory than you have allocated for mysqld

針對aborted連線非常高,首先判斷是aborted_clients還是aborted_connects,然後具體情況具體分析,如果是aborted_connects可以通過tcpdump和error log日誌判斷具體連線失敗、導致abort的主機、使用者等資訊。

tcpdump -s 1500 -w tcp.out port 3306

tcpdump引數選項如下:
(1)tcp: ip icmp arp rarp 和 tcp、udp、icmp這些選項等都要放到第一個引數的位置,用來過濾資料包的型別
(2)-i eth1 : 只抓經過介面eth1的包
(3)-t : 不顯示時間戳
(4)-s 0 : 抓取資料包時預設抓取長度為68位元組。加上-S 0 後可以抓到完整的資料包
(5)-c 100 : 只抓取100個資料包
(6)dst port ! 22 : 不抓取目標埠是22的資料包
(7)src net 192.168.1.0/24 : 資料包的源網路地址為192.168.1.0/24
(8)-w ./target.cap : 儲存成cap檔案,方便用ethereal(即wireshark)分析

4、連線數不斷增加

 mysql連線數有時候會不斷增加,這種情況一般由以下幾種原因

(1)長連線,processlist不斷遞增
(2)連線池初始化設定過大,程式不關閉連線,只增加連線

解決方案:調整sleep合理超時時間;檢查程式程式碼,確定是否關閉資料庫連線操作;檢查連線池配置,調整合適的初始化連線值和最大連線值

 

相關文章