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

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

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

其他回答

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

这个问题的作者提到,通常是在 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** 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