数据库
首页 > 数据库> > 定时统计数据库连接数并记录到表中

定时统计数据库连接数并记录到表中

作者:互联网

压测时想监控数据库连接数变化,自己写个shell脚本在crontab里定时查询并输出到日志。

shell脚本如下:

#!/bin/bash
for a in {1..12}
do
 mysql -h 192.168.128.136 -u root -pYourRealPassword -e "select now() logdate,substring_index(host, ':', 1) host, count(*) cnt from information_schema.processlist where db like '%YourDbName%'   and host like '192.168.129.141%' GROUP BY substring_index(host, ':', 1)"
 sleep 5
done

crontab -e编辑增加每分钟执行一次

*/1 * * * * nohup /root/cntConnect.sh>>/root/cntConnection.log

日志不方便统计,改写成写入到表中,先建表

create table zxc_connect  as
select now() logdate,substring_index(host, ':', 1) host, count(*) cnt from information_schema.processlist

然后修改脚本为insert

#!/bin/bash
for a in {1..12}
do
 mysql -h 192.168.128.136 -D yourDbName -u root -pYourPassword -e "insert into zxc_connect select now(),substring_index(host, ':', 1), count(*) from information_schema.processlist where db like '%yourDbName%'   and host like '192.168.129.141%' GROUP BY substring_index(host, ':', 1);commit;"
sleep 5
done

标签:index,like,到表中,数据库,连接数,substring,host,192.168,root
来源: https://blog.csdn.net/shy_snow/article/details/121116192