写一个输出mysql的qps等性能指标的shell脚本
时间: 2024-05-01 20:22:08 浏览: 100
如何使用mysqladmin获取一个mysql实例当前的TPS和QPS
以下是一个简单的输出 MySQL QPS 和性能指标的 shell 脚本:
```bash
#!/bin/bash
# MySQL 登录信息
USER="username"
PASSWORD="password"
HOST="localhost"
PORT="3306"
DB="database_name"
# 获取当前时间戳
TIMESTAMP=$(date +%s)
# 执行 show global status 命令并保存结果到文件
mysql -u $USER -p$PASSWORD -h $HOST -P $PORT -e "show global status" > status_${TIMESTAMP}.txt
# 从结果文件中解析出 QPS 和其他性能指标
COM_COMMIT=$(cat status_${TIMESTAMP}.txt | grep -w "Com_commit" | awk '{print $2}')
COM_ROLLBACK=$(cat status_${TIMESTAMP}.txt | grep -w "Com_rollback" | awk '{print $2}')
INNODB_ROW_LOCK_TIME_AVG=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_row_lock_time_avg" | awk '{print $2}')
INNODB_ROW_LOCK_TIME_MAX=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_row_lock_time_max" | awk '{print $2}')
INNODB_ROW_LOCK_TIME_MIN=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_row_lock_time_min" | awk '{print $2}')
INNODB_ROW_LOCK_TIME=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_row_lock_time" | awk '{print $2}')
INNODB_ROWS_READ=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_rows_read" | awk '{print $2}')
INNODB_ROWS_UPDATED=$(cat status_${TIMESTAMP}.txt | grep -w "Innodb_rows_updated" | awk '{print $2}')
UPTIME=$(cat status_${TIMESTAMP}.txt | grep -w "Uptime" | awk '{print $2}')
# 计算 QPS
TOTAL_QUERIES=$((COM_COMMIT + COM_ROLLBACK))
QPS=$(echo "scale=2; $TOTAL_QUERIES / $UPTIME" | bc)
# 输出结果
echo "QPS: $QPS"
echo "InnoDB Row Lock Time: avg=$INNODB_ROW_LOCK_TIME_AVG, max=$INNODB_ROW_LOCK_TIME_MAX, min=$INNODB_ROW_LOCK_TIME_MIN, total=$INNODB_ROW_LOCK_TIME"
echo "InnoDB Rows Read: $INNODB_ROWS_READ"
echo "InnoDB Rows Updated: $INNODB_ROWS_UPDATED"
# 删除结果文件
rm status_${TIMESTAMP}.txt
```
你可以将上述代码保存为一个 `mysql_perf.sh` 文件,然后在终端运行 `bash mysql_perf.sh` 来执行脚本。请注意,你需要将脚本中的 MySQL 登录信息和数据库名称替换为你自己的信息。
阅读全文