13.7.6.4 KILL Syntax
<span style="color:#000000">
KILL [CONNECTION | QUERY] <em>processlist_id</em></span>
Each connection to mysqld runs in a separate thread. You can kill a thread with the KILLprocesslist_id statement.
Thread processlist identifiers can be determined from the ID column of theINFORMATION_SCHEMA.PROCESSLIST table, the Id column of SHOW PROCESSLIST output, and thePROCESSLIST_ID column of the Performance Schema threads table. The value for the current thread is returned by the CONNECTION_ID() function.
KILL permits an optional CONNECTION or QUERY modifier:
-
KILL CONNECTION is the same as KILL with no modifier: It terminates the connection associated with the given processlist_id, after terminating any statement the connection is executing.
-
KILL QUERY terminates the statement the connection is currently executing, but leaves the connection itself intact.
If you have the PROCESS privilege, you can see all threads. If you have the SUPER privilege, you can kill all threads and statements. Otherwise, you can see and kill only your own threads and statements.
You can also use the mysqladmin processlist and mysqladmin kill commands to examine and kill threads.
Note
You cannot use KILL with the Embedded MySQL Server library because the embedded server merely runs inside the threads of the host application. It does not create any connection threads of its own.
When you use KILL, a thread-specific kill flag is set for the thread. In most cases, it might take some time for the thread to die because the kill flag is checked only at specific intervals:
-
During SELECT operations, for ORDER BY and GROUP BY loops, the flag is checked after reading a block of rows. If the kill flag is set, the statement is aborted.
-
ALTER TABLE operations that make a table copy check the kill flag periodically for each few copied rows read from the original table. If the kill flag was set, the statement is aborted and the temporary table is deleted.
The KILL statement returns without waiting for confirmation, but the kill flag check aborts the operation within a reasonably small amount of time. Aborting the operation to perform any necessary cleanup also takes some time.
-
During UPDATE or DELETE operations, the kill flag is checked after each block read and after each updated or deleted row. If the kill flag is set, the statement is aborted. If you are not using transactions, the changes are not rolled back.
-
GET_LOCK() aborts and returns NULL.
-
An INSERT DELAYED thread quickly flushes (inserts) all rows it has in memory and then terminates.
-
If the thread is in the table lock handler (state: Locked), the table lock is quickly aborted.
-
If the thread is waiting for free disk space in a write call, the write is aborted with a “disk full” error message.
Warning
Killing a REPAIR TABLE or OPTIMIZE TABLE operation on a MyISAM table results in a table that is corrupted and unusable. Any reads or writes to such a table fail until you optimize or repair it again (without interruption).
方法一
通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令。
mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
+------------------------+
| concat('KILL ',id,';') |
+------------------------+
| KILL 3101; |
| KILL 2946; |
+------------------------+
2 rows in set (0.00 sec)
mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt';
Query OK, 2 rows affected (0.00 sec)
mysql>source /tmp/a.txt;
Query OK, 0 rows affected (0.00 sec)
方法二
杀掉当前所有的MySQL连接
mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill
杀掉指定用户运行的连接,这里为Mike
mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill
方法三
通过SHEL脚本实现
#杀掉锁定的MySQL连接
for id in `mysqladmin processlist|grep -i locked|awk '{print $1}'`
do
mysqladmin kill ${id}
done
方法四
通过Maatkit工具集中提供的mk-kill命令进行
#杀掉超过60秒的sql
mk-kill -busy-time 60 -kill
#如果你想先不杀,先看看有哪些sql运行超过60秒
mk-kill -busy-time 60 -print
#如果你想杀掉,同时输出杀掉了哪些进程
mk-kill -busy-time 60 -print –kill
mk-kill更多用法可参考:
http://www.maatkit.org/doc/mk-kill.html
在使用mysql运行某些语句时,会因数据量太大而导致死锁,没有反映。这个时候,就需要kill掉某个正在消耗资源的query语句即可, KILL命令的语法格式如下:
KILL [CONNECTION | QUERY] thread_id
每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运行,并使用KILL thread_id语句终止一个线程。
KILL允许自选的CONNECTION或QUERY修改符:KILL CONNECTION与不含修改符的KILL一样:它会终止与给定的thread_id有关的连接。KILL QUERY会终止连接当前正在执行的语句,但是会保持连接的原状。
如果您拥有PROCESS权限,则您可以查看所有线程。如果您拥有超级管理员权限,您可以终止所有线程和语句。否则,您只能查看和终止您自己的线程和语句。您也可以使用mysqladmin processlist和mysqladmin kill命令来检查和终止线程。
首先登录mysql,然后使用: show processlist; 查看当前mysql中各个线程状态。
- mysql> show processlist;
- +------+------+----------------------+----------------+---------+-------+-----------+---------------------
- | Id | User | Host | db | Command | Time | State | Info
- +------+------+----------------------+----------------+---------+-------+-----------+---------------------
- | 7028 | root | ucap-devgroup:53396 | platform | Sleep | 19553 | | NULL
- | 8352 | root | ucap-devgroup:54794 | platform | Sleep | 4245 | | NULL
- | 8353 | root | ucap-devgroup:54795 | platform | Sleep | 3 | | NULL
- | 8358 | root | ucap-devgroup:62605 | platform | query | 4156 | updating | update t_shop set |
以上显示出当前正在执行的sql语句列表,找到消耗资源最大的那条语句对应的id.
然后运行kill命令,命令格式如下:
- kill id;
- - 示例:
- kill 8358
杀掉即可。
很多时候由于异常或程序错误会导致个别进程占用大量系统资源,需要结束这些进程,通常可以使用以下命令Kill进程:
mysql中kill掉所有锁表的进程
3点钟刚睡下, 4点多, 同事打电话告诉我用户数据库挂掉了. 我起床看一下进程列表. mysql>show processlist; 出来哗啦啦好几屏幕的, 没有一千也有几百条, 查询语句把表锁住了, 赶紧找出第一个Locked的thread_id, 在MySQL的shell里面执行. mysql>kill thread_id; kill掉第一个锁表的进程, 依然没有改善. 既然不改善, 咱们就想办法将所有锁表的进程kill掉吧, 简单的脚本如下. #!/bin/bash 现在kill_thread_id.sql的内容像这个样子 kill 66402982; 好了, 我们在mysql的shell中执行, 就可以把所有锁表的进程杀死了. mysql>source kill_thread_id.sql当然了, 也可以一行搞定 for id in `mysqladmin processlist | grep -i locked | awk '{print $1}'` |
以下内容来自mysql手册:
13.5.5.3. KILL语法
KILL [CONNECTION | QUERY] thread_id
每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运行,并使用KILL thread_id语句终止一个线程。
KILL允许自选的CONNECTION或QUERY修改符:
· KILL CONNECTION与不含修改符的KILL一样:它会终止与给定的thread_id有关的连接。
· KILL QUERY会终止连接当前正在执行的语句,但是会保持连接的原状。
如果您拥有PROCESS权限,则您可以查看所有线程。如果您拥有SUPER权限,您可以终止所有线程和语句。否则,您只能查看和终止您自己的线程和语句。
您也可以使用mysqladmin processlist和mysqladmin kill命令来检查和终止线程。
注释:您不能同时使用KILL和Embedded MySQL Server库,因为内植的服务器只运行主机应用程序的线程。它不能创建任何自身的连接线程。
当您进行一个KILL时,对线程设置一个特有的终止标记。在多数情况下,线程终止可能要花一些时间,这是因为终止标记只会在在特定的间隔被检查:
· 在SELECT, ORDER BY和GROUP BY循环中,在读取一组行后检查标记。如果设置了终止标记,则该语句被放弃。
· 在ALTER TABLE过程中,在每组行从原来的表中被读取前,检查终止标记。如果设置了终止标记,则语句被放弃,临时表被删除。
· 在UPDATE或DELETE运行期间,在每个组读取之后以及每个已更行或已删除的行之后,检查终止标记。如果终止标记被设置,则该语句被放弃。注意,如果您正在使用事务,则变更不会被 回滚。
· GET_LOCK()会放弃和返回NULL。
· INSERT DELAYED线程会快速地刷新(插入)它在存储器中的所有的行,然后终止。
· 如果线程在表锁定管理程序中(状态:锁定),则表锁定被快速地放弃。
· 如果在写入调用中,线程正在等待空闲的磁盘空间,则写入被放弃,并伴随"disk full"错误消息。
· 警告:对MyISAM表终止一个REPAIR TABLE或OPTIMIZE TABLE操作会导致出现一个被损坏的没有用的表。对这样的表的任何读取或写入都会失败,直到您再次优化或修复它(不中断)。
1、通过information_schema.processlist表中的连接信息生成需要处理掉的MySQL连接的语句临时文件,然后执行临时文件中生成的指令
mysql> select concat('KILL ',id,';') from information_schema.processlist where user='root';
+------------------------+
| concat('KILL ',id,';')
+------------------------+
| KILL 3101;
| KILL 2946;
+------------------------+
2 rows in set (0.00 sec) mysql>select concat('KILL ',id,';') from information_schema.processlist where user='root' into outfile '/tmp/a.txt'; Query OK, 2 rows affected (0.00 sec) mysql>source /tmp/a.txt; Query OK, 0 rows affected (0.00 sec)
2、杀掉当前所有的MySQL连接
mysqladmin -uroot -p processlist|awk -F "|" '{print $2}'|xargs -n 1 mysqladmin -uroot -p kill
杀掉指定用户运行的连接,这里为Mike
mysqladmin -uroot -p processlist|awk -F "|" '{if($3 == "Mike")print $2}'|xargs -n 1 mysqladmin -uroot -p kill
3、通过SHEL脚本实现
#杀掉锁定的MySQL连接 for id in `mysqladmin processlist|grep -i locked|awk '{print $1}'` do mysqladmin kill ${id} done
4、通过Maatkit工具集中提供的mk-kill命令进行
#杀掉超过60秒的sql mk-kill -busy-time 60 -kill #如果你想先不杀,先看看有哪些sql运行超过60秒 mk-kill -busy-time 60 -print #如果你想杀掉,同时输出杀掉了哪些进程 mk-kill -busy-time 60 -print –kill
mk-kill更多用法可参考:
http://www.maatkit.org/doc/mk-kill.html
http://www.sbear.cn/archives/426
Maatkit工具集的其它用法可参考:
http://code.google.com/p/maatkit/wiki/TableOfContents?tm=6
参考文档:
http://www.google.com
http://www.orczhou.com/index.php/2010/10/kill-mysql-connectio-in-batch/
http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/
About Me
.............................................................................................................................................
● 本文整理自网络
●
● QQ群号:230161599(满)、618766405
● 微信群:可加我微信,我拉大家进群,非诚勿扰
● 联系我请加QQ好友(646634621),注明添加缘由
● 于 2017-08-01 09:00 ~ 2017-08-31 22:00 在魔都完成
● 文章内容来源于小麦苗的学习笔记,部分整理自网络,若有侵权或不当之处还请谅解
● 版权所有,欢迎分享本文,转载请保留出处
.............................................................................................................................................
.............................................................................................................................................
使用微信客户端扫描下面的二维码来关注小麦苗的微信公众号(xiaomaimiaolhr)及QQ群(DBA宝典),学习最实用的数据库技术。
小麦苗的微信公众号 小麦苗的DBA宝典QQ群1 小麦苗的DBA宝典QQ群2 小麦苗的微店
.............................................................................................................................................