我从我的Linux shell连接到mysql。我时不时地运行一个太大的SELECT查询。打印啊打印啊,我已经知道这不是我的意思。我想停止查询。

按Ctrl+C(几次)完全杀死mysql,带我回到shell,所以我必须重新连接。

是否有可能在不杀死mysql本身的情况下停止查询?


mysql> show processlist;
+----+------+-----------+-----+---------+------+---------------------+------------------------------+----------+
| Id | User | Host      | db  | Command | Time | State               | Info                         | Progress |
+----+------+-----------+-----+---------+------+---------------------+------------------------------+----------+
| 14 | usr1 | localhost | db1 | Query   |    0 | starting            | show processlist             |    0.000 |
| 16 | usr1 | localhost | db1 | Query   |   94 | Creating sort index | SELECT  `tbl1`.* FROM `tbl1` |    0.000 |
+----+------+-----------+-----+---------+------+---------------------+------------------------------+----------+
2 rows in set (0.000 sec)

mysql> kill 16;
Query OK, 0 rows affected (0.004 sec)

mysql> show processlist;
+----+------+-----------+-----+---------+------+----------+------------------+----------+
| Id | User | Host      | db  | Command | Time | State    | Info             | Progress |
+----+------+-----------+-----+---------+------+----------+------------------+----------+
| 14 | usr1 | localhost | db1 | Query   |    0 | starting | show processlist |    0.000 |
+----+------+-----------+-----+---------+------+----------+------------------+----------+
1 row in set (0.000 sec)

再补充一下

删除查询**Id** where Id是连接Id从显示processlist

如果您不想在运行某些应用程序时终止连接,则更可取。

更多细节你可以阅读mysql文档这里


如果你有mysqladmin可用,你可以得到查询的列表:

> mysqladmin -uUSERNAME -pPASSWORD pr

+-----+------+-----------------+--------+---------+------+--------------+------------------+
| Id  | User | Host            | db     | Command | Time | State        | Info             |
+-----+------+-----------------+--------+---------+------+--------------+------------------+
| 137 | beet | localhost:53535 | people | Query   | 292  | Sending data | DELETE FROM      |
| 145 | root | localhost:55745 |        | Query   | 0    |              | show processlist |
+-----+------+-----------------+--------+---------+------+--------------+------------------+

然后你可以停止运行这个长时间查询的mysql进程:

> mysqladmin -uUSERNAME -pPASSWORD kill 137

使用mysqladmin杀死失控查询:

执行如下命令:

mysqladmin -uusername -ppassword pr

然后记下进程id。

mysqladmin -uusername -ppassword kill pid

失控查询应该不再消耗资源。


连接mysql

mysql -uusername -p  -hhostname

显示完整的流程列表:

mysql> show full processlist;
+---------+--------+-------------------+---------+---------+------+-------+------------------+
| Id      | User   | Host              | db      | Command | Time | State | Info             |
+---------+--------+-------------------+---------+---------+------+-------+------------------+
| 9255451 | logreg | dmin001.ops:37651 | logdata | Query   |    0 | NULL  | show processlist |
+---------+--------+-------------------+---------+---------+------+-------+------------------+

终止特定的查询。id = 9255451

mysql> kill 9255451;

如果你的权限被拒绝,试试下面的SQL:

CALL mysql.rds_kill(9255451)

这个问题的作者提到,通常是在 MySQL打印它的输出,他意识到执行了错误的查询。 如上所述,在这种情况下,Ctrl-C不起作用。然而,我注意到它 将中止当前查询-如果你捕捉它之前任何输出是 打印出来。例如:

mysql> select * from jos_users, jos_comprofiler;

MySQL忙于生成上面两个表的笛卡尔积 你很快就会注意到MySQL并没有将任何输出输出到屏幕上(进程 状态是发送数据),所以你输入Ctrl-C:

Ctrl-C -- sending "KILL QUERY 113240" to server ...
Ctrl-C -- query aborted.
ERROR 1317 (70100): Query execution was interrupted

Ctrl-C可以类似地用于停止UPDATE查询。


您需要运行以下命令来终止进程。 找出您想要终止的进程的id

> show processlist;

从id列中获取值,然后执行下面的命令

kill query <processId>;

查询参数指定需要终止查询命令进程。

终止进程的语法如下

KILL [CONNECTION | QUERY] processlist_id

请参考此链接了解更多信息。