获取MySQL活跃线程数量

2,611 total views, 1 views today

朋友问我,是否监控了MySQL的活跃thread。我想了想,是监控的,因为依据是MySQL官方文档关于mysqladmin status的一句话“Threads:The number of active threads (clients)”。

但是,仔细想想,mysqladmin status中Threads的值并不是我们常说的活跃thread。我们日常中常说的活跃thread,是指正在执行命令的thread。

而,mysqladmin status中Threads的值和通过show processlist的thread数量是一致的。其包含了很多Sleep状态的thread,所以mysqladmin status的值并不能说是活跃 thread。

show processlist命令,我们可以看结果中包含了ID、User、Host、db、Commad等列。我们需要关注的是Command列。

Command列是指thread正在行的命令类型,共有30余种类型。
这里面 Sleep是一种特殊类型,因为它是在等待客户端发送新命令给它(The thread is waiting for the client to send a new statement to it.)。

所以,活跃thread中不能包含Sleep类型,在添加zabbix监控脚本时切忌排除Sleep类型。

zabbix监控截图:

其中,MySQL threads conneted 是指所有节点到MySQL的threads数量;MySQL real active threas是至指活跃的threas(不包含Sleep类型)。

另外,特别说明一下zabbix监控使用的MySQL用户一定要赋予PROCESS权限,这样才能查询出MySQL服务级别的所有processlist, 否则只能看到当前用户的processlist。

以上是我在工作中遇到过的问题,写出来让大家引以为鉴。

发表评论

必填项已用*标注