針對(duì)mysql的連接參數(shù)和狀態(tài)值,本文做些介紹和對(duì)比
一、MYSQL連接參數(shù)變量
1、常用連接數(shù)限制參數(shù)
show variables like '%connect%';
| max_connect_errors | 999999999 | ##允許單用戶連接錯(cuò)誤最大值,超過(guò)后在不刷新?tīng)顟B(tài)的情況下,禁止該用戶新連接
| max_connections | 6000 | ##實(shí)例最大連接數(shù)限制
| max_user_connections | 0 | ##但用戶連接最大限制,默認(rèn)0表示無(wú)限制,遵守總連接數(shù)小于等于max_connections
| connect_timeout | 10 | ##用戶連接超時(shí)限制,超過(guò)10秒,如果依舊無(wú)法連接到mysql,則終止連接
2、超時(shí)參數(shù)
mysql -e "show variables like '%timeout%'"
+-----------------------------+----------+
| connect_timeout | 10 | ##連接超時(shí),10秒
| delayed_insert_timeout | 300 | ##延遲插入超時(shí)時(shí)間,300秒
| have_statement_timeout | YES | ##
| innodb_flush_log_at_timeout | 1 | ##刷新redo log buffer超時(shí)時(shí)間,1秒
| innodb_lock_wait_timeout | 120 | ##事務(wù)等待獲取資源等待的最長(zhǎng)時(shí)間,超過(guò)這個(gè)時(shí)間還未分配到資源則會(huì)返回應(yīng)用失敗,120秒
| innodb_rollback_on_timeout | ON |
| interactive_timeout | 28800 | ##mysql客戶端交互連接超時(shí)時(shí)間,默認(rèn)8小時(shí),用于控制sleep超時(shí)
| lock_wait_timeout | 31536000 | ##主要針對(duì)DDL產(chǎn)生的metadata locks超時(shí)時(shí)間
| net_read_timeout | 60 | ##網(wǎng)絡(luò)讀取數(shù)據(jù)超時(shí)時(shí)間,60秒
| net_write_timeout | 60 | ##為網(wǎng)絡(luò)寫(xiě)入數(shù)據(jù)超時(shí)間60秒
| rpl_stop_slave_timeout | 31536000 | ##停止從庫(kù)服務(wù)超時(shí)時(shí)間
| slave_net_timeout | 60 | ##slave網(wǎng)絡(luò)超時(shí)時(shí)間
| thread_pool_idle_timeout | 60 |
| wait_timeout | 28800 | ##jdbc/odbc連接超時(shí)時(shí)間,默認(rèn)8小時(shí),用于控制sleep超時(shí)
+-----------------------------+----------+
二、MySQL連接狀態(tài)變量
1、常用鏈接狀態(tài)變量
show global status like '%connect%';
+-----------------------------------------------+----------+
| Connection_errors_max_connections | 1906160 | ##用戶最大錯(cuò)誤連接數(shù)
| Connections | 87341259 | ##歷史用戶連接累積值
| Max_used_connections | 10242 | ##歷史最大連接用戶數(shù),processlist看到的連接數(shù)
| Threads_connected | 298 | ##當(dāng)前用戶連接數(shù),當(dāng)前processlist數(shù)
show global status like '%Threads_running%';
| Threads_running | 2 | ##當(dāng)前用戶連接處于running狀態(tài)的數(shù)目,非sleep
show global status like '%abort%';
+------------------+----------+
| Aborted_clients | 85050948 | ##非法終止客戶端連接的狀態(tài)統(tǒng)計(jì),例如通過(guò)kill終止連接
| Aborted_connects | 1905528 | ##非法連接操作狀態(tài)統(tǒng)計(jì),例如用戶密碼錯(cuò)誤
三、常見(jiàn)問(wèn)題分析
1、最大連接數(shù)超出限制
動(dòng)態(tài)調(diào)整最大連接參數(shù)變量,然后通過(guò)連接嘗試
mysql> set global max_connections=2;
[root@localhost ~]# mysql
ERROR 1040 (HY000): Too many connections
##此時(shí)連接超出的嘗試,不會(huì)經(jīng)過(guò)權(quán)限驗(yàn)證,因此不會(huì)記錄Aborted_connects變量值
如遇到此問(wèn)題即表示用戶連接超出了最大連接限制,需要增大連接數(shù)
mysql> set global max_connections=350;
##動(dòng)態(tài)調(diào)整后,需要將/etc/my.cnf配置參數(shù)中的max_connections=350也要做相應(yīng)調(diào)整,這個(gè)參數(shù)可以根據(jù)狀態(tài)量值Max_used_connections 評(píng)估
2、用戶連接錯(cuò)誤數(shù)超出限制
動(dòng)態(tài)調(diào)整最大連接錯(cuò)誤數(shù)變量限制,然后通過(guò)嘗試失敗登錄,主機(jī)對(duì)應(yīng)的普通用戶會(huì)被阻塞,超級(jí)用戶不限
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.
手動(dòng)調(diào)整參數(shù)測(cè)試(telnet 10.1.0.1 3306 五次斷鏈,用戶會(huì)被鎖)
mysql> set global max_connect_errors=5;
通過(guò)測(cè)試發(fā)現(xiàn)用戶直接錯(cuò)誤密碼連接,不會(huì)導(dǎo)致用戶被鎖,主要通過(guò)performance_schema下的host_cache表查看相關(guān)阻塞信息
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 |
+----------------+-------+----------------+--------------------+------------------------+---------------------------+
哪些連接會(huì)導(dǎo)致用戶被鎖?導(dǎo)致必須通過(guò)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連接狀態(tài)值主要有兩個(gè),分別是 Aborted_clients和Aborted_connects,其中兩個(gè)變量值含義如下:
Aborted_clients:The 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
下面的狀態(tài)變量主要是用戶最大連接超過(guò)后,有多少嘗試連接的錯(cuò)誤統(tǒng)計(jì):
Connection_errors_max_connections:The number of connections refused because the server max_connections limit was reached
Aborted_clients和Aborted_connects參數(shù)對(duì)比,兩個(gè)狀態(tài)值在什么情況下會(huì)變化?
Aborted_clients主要是因?yàn)榭蛻舳朔钦jP(guān)閉,次值就會(huì)不斷增加,具體場(chǎng)景如下:
(1)手動(dòng)殺死連接,kill的方式
(2)mysql客戶端超時(shí)interactive_timeout,結(jié)束sleep
(3)程序超時(shí)wait_timeout,異常結(jié)束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失敗的次數(shù),具體場(chǎng)景如下:
(1)用戶名密碼錯(cuò)誤
(2)登陸權(quán)限不足
(3)連接超時(shí),connect_timeout
(4)
max_allowed_packet
variable value is too small or queries require more memory than you have allocated for mysqld
針對(duì)aborted連接非常高,首先判斷是aborted_clients還是aborted_connects,然后具體情況具體分析,如果是aborted_connects可以通過(guò)tcpdump和error log日志判斷具體連接失敗、導(dǎo)致abort的主機(jī)、用戶等信息。
tcpdump -s 1500 -w tcp.out port 3306
tcpdump參數(shù)選項(xiàng)如下:
(1)tcp: ip icmp arp rarp 和 tcp、udp、icmp這些選項(xiàng)等都要放到第一個(gè)參數(shù)的位置,用來(lái)過(guò)濾數(shù)據(jù)報(bào)的類型
(2)-i eth1 : 只抓經(jīng)過(guò)接口eth1的包
(3)-t : 不顯示時(shí)間戳
(4)-s 0 : 抓取數(shù)據(jù)包時(shí)默認(rèn)抓取長(zhǎng)度為68字節(jié)。加上-S 0 后可以抓到完整的數(shù)據(jù)包
(5)-c 100 : 只抓取100個(gè)數(shù)據(jù)包
(6)dst port ! 22 : 不抓取目標(biāo)端口是22的數(shù)據(jù)包
(7)src net 192.168.1.0/24 : 數(shù)據(jù)包的源網(wǎng)絡(luò)地址為192.168.1.0/24
(8)-w ./target.cap : 保存成cap文件,方便用ethereal(即wireshark)分析
4、連接數(shù)不斷增加
mysql連接數(shù)有時(shí)候會(huì)不斷增加,這種情況一般由以下幾種原因
(1)長(zhǎng)連接,processlist不斷遞增
(2)連接池初始化設(shè)置過(guò)大,程序不關(guān)閉連接,只增加連接
解決方案:調(diào)整sleep合理超時(shí)時(shí)間;檢查程序代碼,確定是否關(guān)閉數(shù)據(jù)庫(kù)連接操作;檢查連接池配置,調(diào)整合適的初始化連接值和最大連接值
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。