SQL开发知识:MYSQL如何查看进程和kill进程
如何查看进程和kill进程
有时候我们在执行一条SQL语句时,或者更改表结构时,由于这张表的数据量巨大,往往会在执行操作后就会卡住…然后这张表就会被锁住..这时,我们可以杀掉这个进程.
这里有两种方法查看和杀掉进程
第一种
SHOW PROCESSLIST;
执行上述命令,可以看到下图:
然后找到对应的ID:
KILL 41515;
就OK了.
第二种
需要借助工具,Navicat. 工具->服务器监控->MySQL
打开之后就可以看到下图:
然后还是找到对应的ID :
KILL 42736;
kill所有慢查询进程和锁表进程
1、kill所有慢查询进程
#!/bin/bash
mysql -uroot -pMy_Password -e “show processlist” | grep -i “Query” > slow_query.log
for query in `cat slow_query.log | awk ‘{print $1}’`
do
echo “kill $query;” > kill_slow_query.sql
done
mysql -uroot -pMy_Password -e “show processlist” | grep -i “Query” > slow_query.log
for query in `cat slow_query.log | awk ‘{print $1}’`
do
echo “kill $query;” > kill_slow_query.sql
done
登录mysql,执行:
mysql> source kill_slow_query.sql
或者:
#!/bin/bash
for query in `mysqladmin -uroot -pMy_Password processlist | grep -i ‘Query’ | awk ‘{print $2}’`
do
mysqladmin kill ${query}
done
for query in `mysqladmin -uroot -pMy_Password processlist | grep -i ‘Query’ | awk ‘{print $2}’`
do
mysqladmin kill ${query}
done
2、kill所有死锁进程
#!/bin/bash
mysql -uroot -pMy_Password -e “show processlist” | grep -i “Locked” > locked.log
for lock in `cat locked.log | awk ‘{print $1}’`
do
echo “kill $lock;” > kill_locked.sql
done
mysql -uroot -pMy_Password -e “show processlist” | grep -i “Locked” > locked.log
for lock in `cat locked.log | awk ‘{print $1}’`
do
echo “kill $lock;” > kill_locked.sql
done
登录mysql,执行:
mysql> source kill_locked.sql
或者:
#!/bin/bash
for lock in `mysqladmin -uroot -pMy_Password processlist | grep -i ‘Locked’ | awk ‘{print $2}’`
do
mysqladmin kill ${lock}
done
for lock in `mysqladmin -uroot -pMy_Password processlist | grep -i ‘Locked’ | awk ‘{print $2}’`
do
mysqladmin kill ${lock}
done
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。