针对MySQL的连接参数和状态值,本文做些介绍和对比。
一、MYSQL连接参数变量
1、常用连接数限制参数
show variables like '%connect%';
2、超时参数
mysql -e "show variables like '%timeout%'"
二、MySQL连接状态变量
1、常用链接状态变量
show global status like '%connect%';
show global status like '%Threads_running%';
show global status like '%abort%';
三、常见问题分析
1、最大连接数超出限制
动态调整最大连接参数变量,然后通过连接尝试
如遇到此问题即表示用户连接超出了最大连接限制,需要增大连接数
2、用户连接错误数超出限制
动态调整最大连接错误数变量限制,然后通过尝试失败登录,主机对应的普通用户会被阻塞,超级用户不限
max_connect_errors官方解释如下:
手动调整参数测试(telnet 10.1.0.1 3306 五次断链,用户会被锁)
通过测试发现用户直接错误密码连接,不会导致用户被锁,主要通过performance_schema下的host_cache表查看相关阻塞信息
哪些连接会导致用户被锁?导致必须通过flush hosts释放host_cache,具体如下:
解决方法就是:flush hosts或mysqladmin flush-hosts
3、aborted连接值非常高
abort连接状态值主要有两个,分别是 Aborted_clients和Aborted_connects,其中两个变量值含义如下:
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
下面的状态变量主要是用户最大连接超过后,有多少尝试连接的错误统计:
Connection_errors_max_connections
:The number of connections refused because the server max_connections limit was reached
Aborted_clients和Aborted_connects参数对比,两个状态值在什么情况下会变化?
Aborted_clients主要是因为客户端非正常关闭,次值就会不断增加,具体场景如下:
Aborted_connects主要是连接Mysql失败的次数,具体场景如下:
针对aborted连接非常高,首先判断是aborted_clients还是aborted_connects,然后具体情况具体分析,如果是aborted_connects可以通过tcpdump和error log日志判断具体连接失败、导致abort的主机、用户等信息。
tcpdump -s 1500 -w tcp.out port 3306
4、连接数不断增加
mysql连接数有时候会不断增加,这种情况一般由以下几种原因
解决方案:调整sleep合理超时时间;检查程序代码,确定是否关闭数据库连接操作;检查连接池配置,调整合适的初始化连接值和最大连接值。