杀死所有查询 - MySQL

JIS*_*one 17 mysql command-line

有时在 SNAFU 期间,我必须跑kill query xxxxxxx二十或三十次。任何形式的kill all命令我失踪?

因为我不喜欢打字。

Rol*_*DBA 16

从 Linux 命令行

for PROC_TO_KILL in `mysql -h... -u... -p... -A --skip-column-names -e"SHOW PROCESSLIST" | grep -v "system user" | awk '{print $1}'` ; do mysql -h... -u... -p... -A --skip-column-names -e"KILL QUERY ${PROC_TO_KILL}" ; done
Run Code Online (Sandbox Code Playgroud)

您可以更改 for 循环标题中的 grep 选项以在查询中定位特定用户或特定字符串。

如果您有 MySQL 5.1,其中 processlist 在 INFORMATION_SCHEMA 中,您可以执行此操作以从 mysql 客户端中批量生成 KILL QUERY 命令:

SELECT GROUP_CONCAT(CONCAT('KILL QUERY ',id,';') SEPARATOR ' ') KillQuery
FROM information_schema.processlist WHERE user<>'system user'\G
Run Code Online (Sandbox Code Playgroud)

您可以针对 INFO 字段执行 WHERE 子句以查找特定查询,针对长时间运行的查询执行 TIME 字段,或针对特定数据库执行 DB 字段。


小智 5

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)
Run Code Online (Sandbox Code Playgroud)

http://www.mysqlperformanceblog.com/2009/05/21/mass-killing-of-mysql-connections/