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

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

是否有可能在不杀死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

其他回答

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

> show processlist;

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

kill query <processId>;

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

终止进程的语法如下

KILL [CONNECTION | QUERY] processlist_id

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

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)

使用mysqladmin杀死失控查询:

执行如下命令:

mysqladmin -uusername -ppassword pr

然后记下进程id。

mysqladmin -uusername -ppassword kill pid

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

再补充一下

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

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

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

连接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)