如何停止正在运行的 MySQL 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3787651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:15:22  来源:igfitidea点击:

How can I stop a running MySQL query?

mysqlkillprocesslist

提问by David B

I connect to mysqlfrom my Linux shell. Every now and then I run a SELECTquery that is too big. It prints and prints and I already know this is not what I meant. I would like to stop the query.

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

Hitting Ctrl+C(a couple of times) kills mysqlcompletely and takes me back to shell, so I have to reconnect.

击中Ctrl+C(几次)mysql完全杀死并将我带回外壳,因此我必须重新连接。

Is it possible to stop a query without killing mysqlitself?

是否可以在不自杀的情况下停止查询mysql

回答by baklarz2048

mysql>show processlist;

mysql> kill "number from first col";

回答by mtariq

Just to add

只是为了添加

KILL QUERY **Id**where Id is connection id from show processlist

KILL QUERY **Id**其中 Id 是来自的连接 ID show processlist

is more preferable if you are do not want to kill the connection usually when running from some application.

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

For more details you can read mysql doc here

有关更多详细信息,您可以在此处阅读 mysql 文档

回答by minhas23

Connect to mysql

连接到 mysql

mysql -uusername -p  -hhostname

show full processlist:

显示完整的进程列表:

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

Kill the specific query. Here id=9255451

终止特定查询。这里 id=9255451

mysql> kill 9255451;

If you get permission denied, try this SQL:

如果您的权限被拒绝,请尝试以下 SQL:

CALL mysql.rds_kill(9255451)

回答by minhas23

Use mysqladminto kill the runaway query:

用于mysqladmin终止失控的查询:

Run the following commands:

运行以下命令:

mysqladmin -uusername -ppassword pr

Then note down the process id.

然后记下进程ID。

mysqladmin -uusername -ppassword kill pid

The runaway query should no longer be consuming resources.

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

回答by Rudy Lattae

If you have mysqladminavailable, you may get the list of queries with:

如果您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 |
+-----+------+-----------------+--------+---------+------+--------------+------------------+

Then you may stop the mysql process that is hosting the long running query:

然后,您可以停止托管长时间运行的查询的 mysql 进程:

> mysqladmin -uUSERNAME -pPASSWORD kill 137

回答by Shivam Kubde

You need to run following command to kill the process.

您需要运行以下命令来终止进程。

> show processlist;  
> kill query processId;

Query parameter specifies that we need to kill query command process.

查询参数指定我们需要杀死查询命令进程。

The syntax for kill process as follows

杀死进程的语法如下

KILL [CONNECTION | QUERY] processlist_id

杀死[连接| QUERY] processlist_id

Please referthis link for more information.

参阅此链接以获取更多信息。

回答by Anthony Geoghegan

The author of this question mentions that it's usually only after MySQL prints its output that he realises that the the wrong query was executed. As noted, in this case, Ctrl-Cdoesn't help. However, I've noticed that it will abort the current query– if you catch it beforeany output is printed. For example:

这个问题的作者提到,通常只有在 MySQL 打印其输出后,他才意识到执行了错误的查询。如前所述,在这种情况下,Ctrl-C没有帮助。但是,我注意到它 会中止当前查询——如果您打印任何输出之前捕获它。例如:

mysql> select * from jos_users, jos_comprofiler;

MySQL gets busy generating the Cartesian Product of the above two tables and you soon notice that MySQL hasn't printed any output to screen (the process state is Sending data) so you type Ctrl-C:

MySQL 忙于生成上述两个表的笛卡尔积,您很快就会注意到 MySQL 没有将任何输出打印到屏幕(进程状态为Sending data),因此您键入Ctrl-C

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

Ctrl-Ccan similarly be used to stop an UPDATEquery.

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