MySQL · 最佳实践 · 快速杀掉大量会话连接

kill.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# !/bin/sh

#############################################################################
# Kill all processlist According to status.
# -h: 主机IP
# -u: 用户名
# -p: 密码
#############################################################################

host = ''
user = ''
pwd = ''
status = ''

while :
do
n=`mysqladmin processlist -h$host -u$user -p$pwd | grep -i $status | wc -l`
date=`date +%Y%m%d\[%H:%M:%S]`
echo $n

if [ "$n" -gt 10 ]
then
for i in `mysqladmin processlist -h$host -u$user -p$pwd | grep -i $status | awk '{print $2}'`
do
mysqladmin processlist -h$host -u$user -p$pwd kill $i
done

echo "$status is too many need killed it " >> /tmp/kill.log
echo "$date : $n" >> /tmp/kill.log
fi
sleep 1
done