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

压测时想监控数据库连接数变化,自己写个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

/etc/init.d/crond restart 重启crontab

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值