Long-running SQL in bulk kill MySQL

Source: Internet
Author: User
Tags modifiers mysql manual terminates what sql

The following is from the MySQL manual:

13.5.5.3. Kill syntax
KILL [CONNECTION | QUERY] thread_id
Each connection to the mysqld is run in a separate thread, and you can use the show Processlist statement to see which threads are running and terminate a thread with the kill thread_id statement.

Kill allows optional connection or query modifiers:

· Kill connection is the same as a kill without modifiers: it terminates the connection associated with a given thread_id.

· Kill query Terminates the statement that the connection is currently executing, but keeps the connection intact.

If you have process permissions, you can view all the threads. If you have super privileges, you can terminate all threads and statements. Otherwise, you can only view and terminate your own threads and statements.

You can also use the Mysqladmin processlist and mysqladmin kill commands to check and terminate threads.

Note: You cannot use both the kill and embedded MySQL server libraries because the embedded server only runs the thread of the host application. It cannot create any of its own connection threads.

When you make a kill, a unique termination token is set on the thread. In most cases, thread termination can take some time, because the terminating token is only checked at a specific interval:

· In the Select, ORDER by and group by loops, the tag is checked after reading a set of rows. If a termination token is set, the statement is discarded.

· In the ALTER TABLE procedure, the terminating token is checked before each set of rows is read from the original table. If a termination token is set, the statement is discarded and the temporary table is deleted.

· During the update or delete run, after each group has been read and after each row that has been more or deleted, the termination token is checked. If the termination token is set, the statement is discarded. Note that if you are using transactions, the changes are not rolled back.

· Get_lock () discards and returns NULL.

· The insert delayed thread quickly refreshes (inserts) all of its rows in memory and then terminates.

· If the thread is in the table lock Manager (state: Locked), the table lock is quickly discarded.

· If the thread is waiting for free disk space in the write call, the write is discarded and accompanies the "Disk full" error message.

· Warning: Terminating a repair table or optimize table operation on MyISAM tables results in a corrupted, unused table. Any read or write to such a table will fail until you optimize or repair it again (without interruption).

1. Generate statement temporary files for MySQL connections that need to be processed through the connection information in the Information_schema.processlist table, then execute the instructions generated in the temp file

Mysql>Select Concat (‘KILL', ID,‘;‘)From Information_schema.processlistwhereUser=‘Root‘;+------------------------+| Concat‘KILL', ID,‘;‘)+------------------------+|KILL3101;|KILL2946;+------------------------+2 rowsInchSet (0.00SEC) MySQL>Select Concat (‘KILL', ID,‘;‘)From Information_schema.processlistwhere user= ' 

2.

Kill all current MySQL connections

Mysqladmin-uroot-p processlist|  '|' {print $}' |  Kill     

Kill the connection run by the specified user, this is Mike

Mysqladmin-uroot-p processlist|  '|' {if ($ = = "Mike") print $}' |  Kill     

 

3, through the Shel script implementation

#杀掉锁定的MySQL连接 in   ' mysqladmin processlist|  Grep-i locked| '{print '}'dokill ${ID}       Done 

4. Through the Mk-kill command provided in the Maatkit tool set

  #杀掉超过60秒的sqlmk-kill-busy- time 60-kill #如果你想先不杀, first look at what SQL is running for more than 60 seconds Mk-kill-busy-time 60-print# if you want to kill, and output what processes are killed Mk-kill-busy-time 60-print–kill     

Mk-kill For more usage, refer to:
Http://www.maatkit.org/doc/mk-kill.html
http://www.sbear.cn/archives/426
Other uses of the Maatkit toolset can be consulted:
Http://code.google.com/p/maatkit/wiki/TableOfContents?tm=6
Reference Documentation:
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/

Long-running SQL in bulk kill MySQL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.