如何在不退出 mysql 的情况下中断 mysql 命令行工具中的长查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/498344/
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 12:43:46  来源:igfitidea点击:

How do I Interrupt a long query in the mysql command line tool without quitting mysql?

mysqlcommand-line

提问by rwired

While debugging SQL statements, if I accidentally execute a query in using the mysql command line that outputs at lot of results (even if the query itself executes in reasonable time), the only way I know of to stop the endless stream of output is CTRL-C.

在调试 SQL 语句时,如果我在使用 mysql 命令行时不小心执行了一个输出大量结果的查询(即使查询本身在合理的时间内执行),我知道停止无休止的输出流的唯一方法是 CTRL -C。

Unfortunately this puts me back in the shell, forcing me to login and select the database again.

不幸的是,这让我回到了 shell,迫使我再次登录并选择数据库。

To avoid this I've started running mysql with the --sigint-ignoreoption so that CTRL-C is ignored.

为了避免这种情况,我开始使用--signt-ignore选项运行 mysql,以便忽略CTRL-C。

Now I'd like a way to interrupt the output of those long queries.

现在我想要一种方法来中断那些长查询的输出。

Is there a keyboard shortcut that will do this?

有没有键盘快捷键可以做到这一点?

采纳答案by Schwern

You can use --pagerto have your output passed to a pager such as lesswhich will give you control over the output. Not just killing it, but also paging, searching and even storing the output better than your terminal window gives you.

您可以使用--pager将输出传递给寻呼机,例如less它可以让您控制输出。不仅仅是杀死它,而且比终端窗口给你的更好的分页、搜索甚至存储输出。

There's also the --safe-updatesor -Uswitch aka --i-am-a-dummywhich protects you from clauseless updates and deletes and also auto limits selects to 1000 (modifyable with select_limit).

还有--safe-updatesor-U开关 aka--i-am-a-dummy可以保护您免受无子句updates 和deletes 的影响,并且自动将选择限制为 1000(可使用 select_limit 修改)。

All of this can be set by default in ~/.my.cnf.

所有这些都可以在~/.my.cnf.

[mysql]
pager
safe-updates

回答by Bill Karwin

Not a keyboard shortcut.

不是键盘快捷键。

The only choice is to open another session, use SHOW PROCESSLISTand then KILL QUERYthe one you want to terminate.

唯一的选择是打开另一个会话,使用SHOW PROCESSLIST然后KILL QUERY要终止的会话。

You can also use the mysqladmincommand-line tool to issue these commands.

您还可以使用mysqladmin命令行工具来发出这些命令。

Either way, it requires you to login. So it's not much of an advantage over just hitting Ctrl-C.

无论哪种方式,它都需要您登录。因此,与仅按 Ctrl-C 相比,这并没有太大的优势。

回答by rwired

From the current mysql docs:

从当前的 mysql 文档:

As of MySQL 5.1.10, typing Control-C causes mysql to attempt to kill the current statement. If this cannot be done, or Control-C is typed again before the statement is killed, mysql exits. Previously, Control-C caused mysql to exit in all cases.

从 MySQL 5.1.10 开始,键入 Control-C 会导致 mysql 尝试终止当前语句。如果这不能完成,或者在语句被终止之前再次输入 Control-C,mysql 将退出。以前,Control-C 在所有情况下都会导致 mysql 退出。

Since I was using version 5.0.67 seems that updating mysql would be the best fix. However I have accepted Schwern's answer because it was quick to implement and works like a dream.

由于我使用的是 5.0.67 版,因此更新 mysql 似乎是最好的解决方法。但是,我接受了 Schwern 的回答,因为它实施起来很快,而且效果很好。

回答by JesusIniesta

Bit late, but maybe my answer will help someone.

有点晚了,但也许我的回答会对某人有所帮助。

A way to kill a concrete mysql query through the command line is:

通过命令行终止具体的 mysql 查询的一种方法是:

  1. Identify the process id of that query by the command: mysqladmin processlist
  2. Assuming that the Id is the 5736748, to kill the query you have to type: mysqladmin kill 5736748
  1. 通过以下命令识别该查询的进程 ID:mysqladmin processlist
  2. 假设 Id 是 5736748,要终止查询,您必须键入:mysqladmin kill 5736748

回答by John Ostrowick

Use mysql --sigint-ignore

mysql --sigint-ignore

and to clear a line, use control+U

并清除一行,使用control+U