Four ways to kill MySQL connections in bulk

Source: Internet
Author: User
Tags what sql

Method One
Generate the statement temporary file for the MySQL connection that needs to be processed through the connection information in the Information_schema.processlist table, and then execute the instructions generated in the temporary file.

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)

Method Two
Kill all current MySQL connections

Mysqladmin-uroot-p processlist|awk-f "|" ' {print $} ' |xargs-n 1 mysqladmin-uroot-p Kill

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

Mysqladmin-uroot-p processlist|awk-f "|" ' {if ($ = = ' Mike ') print $ ' |xargs-n 1 mysqladmin-uroot-p Kill

Method Three
Implemented with Shel scripts

#杀掉锁定的MySQL连接
For ID in ' mysqladmin processlist|grep-i Locked|awk ' {print '} '
Do
Mysqladmin Kill ${id}
Done

Method Four
Through the Mk-kill command provided in the Maatkit toolset

#杀掉超过60秒的sql
Mk-kill-busy-time 60-kill
#如果你想先不杀, first look at what SQL is running for more than 60 seconds
Mk-kill-busy-time 60-print
#如果你想杀掉, and the output kills which processes
Mk-kill-busy-time 60-print–kill

Four ways to kill MySQL connections in bulk

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.