每日shell脚本之mysql健康查询
#!/usr/bin/bash
#**** :M乔木
#Email:2776617348@qq.com
#解释器:这是一个shell脚本
cat<<EOF
----------------------------------------------------
输入1 [heartbeat] 获取mysql存活
输入2 [qps] 获取mysql的增删改查次数
输入3 [oos] 获取MySQL启动时间
输入4 [replication] 获取主从复制的状态 [返回状态码 正常:0 异常:1 无主从:2]
输入5 [process] 获取当前正在运行的sql数量
输入6 [time] 获取当前执行时间大于10秒的所有sql
输入q 退出
----------------------------------------------------
EOF
read -p "请输入你的操作:" num
case $num in
1)
ch=`mysql -p0 -e "SHOW STATUS LIKE 'Threads_connected';" 2>/dev/null | awk '/Threads_connected/{print $0}' | awk '{print $2}'`
if [ $ch -eq 1 ];then
echo "mysql还在活着!"
else
echo "mysql已死亡!"
fi
;;
2)
cm=`mysql -p0 -e "SHOW STATUS LIKE 'Com_insert';" 2>/dev/null | awk '/Com_insert/{print $2}'`
sm=`mysql -p0 -e "SHOW STATUS LIKE 'Com_delete';" 2>/dev/null | awk '/Com_delete/{print $2}'`
gm=`mysql -p0 -e "SHOW STATUS LIKE 'Com_update';" 2>/dev/null | awk '/Com_update/{print $2}'`
zm=`mysql -p0 -e "SHOW STATUS LIKE 'Com_select';" 2>/dev/null | awk '/Com_select/{print $2}'`
echo "mysql增删改查分别为:"
echo "插入[${cm}]删除[${sm}]更新[${gm}]查询[${zm}]"
;;
3)
mu=`mysql -p0 -e "SHOW GLOBAL STATUS LIKE 'Uptime';" 2>/dev/null | awk '/Uptime/{print $2}'`
echo "mysql已运行[${mu}秒]"
;;
4)
zz=`ssh 192.168.219.128 "mysql -p0 -e 'show slave status\G'" 2>/dev/null | awk '/Slave_IO_Running/{print $0}' | cut -d':' -f2 `
if [ $zz = "yes" ];then
echo "mysql状态:0"
else
echo "mysql状态:1"
fi
;;
5)
ym=`mysql -p0 -e "SHOW FULL PROCESSLIST;" 2>/dev/null | awk '/root/{print $1}'`
echo "mysql当前有${ym}个sql运行"
;;
6)
cat /var/lib/mysql/atguigu-slow.log
;;
q)
exit 0
esac