【转载】MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法

时间:2021-05-10 21:30:14

某个初级dba误删index,mysql漫山遍野全是10S以上的慢sql,mysql服务被hang住了,而且慢sql不断在增加中,如果手动在mysql界面kill的话,根本来不及,这个时候,shell就必须要出手了,如下的shell脚本,大家可以参考:

#It is used to kill processlist of mysql sleep
#!/bin/sh
while : do
n=`mysqladmin processlist -uadmin -pxxxxx|grep -i sleep |wc -l`
date=`date +%Y%m%d\[%H:%M:%S]`
echo $n if [ "$n" -gt 10 ]
then
for i in `mysqladmin processlist -uadmin -pxxxxxx|grep -i sleep |awk '{print $2}'`
do
mysqladmin -uadmin -pxxxxxx kill $i
done
echo "sleep is too many I killed it " >> /tmp/sleep.log
echo "$date : $n" >> /tmp/sleep.log
fi
sleep 1
done

最后,赶紧添加上合适的索引!