#!/bin/bash

# mysql 5.6 version
# Author : thinklxr
# Time : 2020.07.10

Host="$1"
User="$2"
Pass="$3"
Port="$4"


# 管理员账户执行

if [ -z "$Host" ]
then
echo " Variable null HostName"
break
elif [ -z "$User" ]
then
echo " Variable null UserName"
break
elif [ -z "$Pass" ]
then
echo " Variable null PassWord"
break
elif [ -z "$Port" ]
then
echo " Variable null Port"
break
else
read -p '输入要查询的用户: ' USERNAME

mysql -h $Host -u $User -p$Pass -e "show processlist;"|grep $USERNAME >> $Host-`date +%Y%m%d-%H%M%S`.log

for Kid in $(mysql -h $Host -u $User -p$Pass -e "show processlist;"|grep $USERNAME|grep -E "Sleep|Query|locked"|grep -vE "information_schema|performance_schema"|awk '{print $1}')
do

echo -e "\033[33mkill $Kid\033[0m"
mysqladmin $mysql -h $Host -u $User -p$Pass kill ${Kid};
done
fi