mysql的QPS和TPS采集脚本
查询次数:SHOW GLOBAL STATUS LIKE 'Questions';
事务的提交数:SHOW GLOBAL STATUS LIKE 'Com_commit';
回滚次数:SHOW GLOBAL STATUS LIKE 'Com_rollback';
bash
#!/bin/bash
# 连接MySQL数据库的参数
USER="user"
PASSWORD="passwd"
HOST="localhost"
PORT="3306"
# 获取当前状态
function get_mysql_status {
mysql -u$USER -p$PASSWORD -h$HOST -P$PORT -e "SHOW GLOBAL STATUS LIKE 'Questions';"
}
# 初始记录
start_status=$(get_mysql_status | awk 'NR==2 {print $2}')
sleep 10 # 等待10秒
# 第二次记录
end_status=$(get_mysql_status | awk 'NR==2 {print $2}')
# 计算QPS
queries=$((end_status - start_status))
qps=$((queries / 10)) # 除以等待的秒数
echo "QPS: $qps"
采集TPS指标只需要将 "Questions" (查询数)改为 "Com_commit" (事务提交次数) 即可