批量殺死MySQL連線的幾種方法詳解

lhrbest發表於2017-08-27
批量殺死MySQL連線的幾種方法詳解


官網:https://dev.mysql.com/doc/refman/5.6/en/kill.html

13.7.6.4 KILL Syntax

	
	
	
	
KILL [CONNECTION | QUERY] processlist_id

Each connection to mysqld runs in a separate thread. You can kill a thread with the KILL processlist_id statement.

Thread processlist identifiers can be determined from the ID column of the INFORMATION_SCHEMA.PROCESSLIST table, the Id column of SHOW PROCESSLIST output, and the PROCESSLIST_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命令解決死鎖問題,殺死某條正在執行的sql語句

2013-08-15 10:28:21   來源:   評論:0 點選:4655

在使用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中各個執行緒狀態。

[sql] view plain copy
 print?
  1. mysql> show processlist;  
  2. +------+------+----------------------+----------------+---------+-------+-----------+---------------------   
  3. | Id   | User | Host                 | db             | Command | Time  | State     | Info  
  4. +------+------+----------------------+----------------+---------+-------+-----------+---------------------   
  5. | 7028 | root | ucap-devgroup:53396  | platform       | Sleep   | 19553 |           | NULL   
  6. | 8352 | root | ucap-devgroup:54794  | platform       | Sleep   |  4245 |           | NULL  
  7. | 8353 | root | ucap-devgroup:54795  | platform       | Sleep   |     3 |           | NULL  
  8. | 8358 | root | ucap-devgroup:62605  | platform       | query   |  4156 | updating  | update t_shop set  |  

以上顯示出當前正在執行的sql語句列表,找到消耗資源最大的那條語句對應的id.

然後執行kill命令,命令格式如下:

[sql] view plain copy
 print?
  1. kill id;  
  2. - 示例:  
  3. kill 8358  

殺掉即可。


很多時候由於異常或程式錯誤會導致個別程式佔用大量系統資源,需要結束這些程式,通常可以使用以下命令Kill程式:

mysql中kill掉所有鎖表的程式
2009-05-12 14:03

轉載請保留如下作者資訊

作者 : jesse

部落格 : http://hi.baidu.com/leechl

3點鐘剛睡下, 4點多, 同事打電話告訴我使用者資料庫掛掉了. 我起床看一下程式列表.

mysql>show processlist;

出來嘩啦啦好幾螢幕的, 沒有一千也有幾百條, 查詢語句把表鎖住了, 趕緊找出第一個Locked的thread_id, 在MySQL的shell裡面執行.

mysql>kill thread_id;

kill掉第一個鎖表的程式, 依然沒有改善. 既然不改善, 我們們就想辦法將所有鎖表的程式kill掉吧, 簡單的指令碼如下.

#!/bin/bash
mysql -u root -e "show processlist" | grep -i "Locked" >> locked_log.txt

for line in `cat locked_log.txt | awk '{print $1}'`
do 
   
echo "kill $line;" >> kill_thread_id.sql
done

現在kill_thread_id.sql的內容像這個樣子

kill 66402982;
kill 66402983;
kill 66402986;
kill 66402991;
.....

好了, 我們在mysql的shell中執行, 就可以把所有鎖表的程式殺死了.

mysql>source kill_thread_id.sql

當然了, 也可以一行搞定
for id in `mysqladmin processlist | grep -i locked | awk '{print $1}'`
do
   
mysqladmin kill ${id}
done


以下內容來自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

.............................................................................................................................................

● 本文整理自網路

● 本文在itpub(http://blog.itpub.net/26736162/abstract/1/)、部落格園(http://www.cnblogs.com/lhrbest)和個人微信公眾號(xiaomaimiaolhr)上有同步更新

● 本文itpub地址:http://blog.itpub.net/26736162/abstract/1/

● 本文部落格園地址:http://www.cnblogs.com/lhrbest

● 本文pdf版、個人簡介及小麥苗雲盤地址:http://blog.itpub.net/26736162/viewspace-1624453/

● 資料庫筆試面試題庫及解答:http://blog.itpub.net/26736162/viewspace-2134706/

● DBA寶典今日頭條號地址:http://www.toutiao.com/c/user/6401772890/#mid=1564638659405826

.............................................................................................................................................

● QQ群號:230161599(滿)、618766405

● 微信群:可加我微信,我拉大家進群,非誠勿擾

● 聯絡我請加QQ好友646634621,註明新增緣由

● 於 2017-08-01 09:00 ~ 2017-08-31 22:00 在魔都完成

● 文章內容來源於小麥苗的學習筆記,部分整理自網路,若有侵權或不當之處還請諒解

● 版權所有,歡迎分享本文,轉載請保留出處

.............................................................................................................................................

小麥苗的微店https://weidian.com/s/793741433?wfr=c&ifr=shopdetail

小麥苗出版的資料庫類叢書http://blog.itpub.net/26736162/viewspace-2142121/

.............................................................................................................................................

使用微信客戶端掃描下面的二維碼來關注小麥苗的微信公眾號(xiaomaimiaolhr)及QQ群(DBA寶典),學習最實用的資料庫技術。

   小麥苗的微信公眾號      小麥苗的DBA寶典QQ群1     小麥苗的DBA寶典QQ群2        小麥苗的微店

.............................................................................................................................................

批量殺死MySQL連線的幾種方法詳解
DBA筆試面試講解群1
DBA筆試面試講解群2
歡迎與我聯絡



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-2144194/,如需轉載,請註明出處,否則將追究法律責任。

相關文章