10

In the mysql command line, pressing Control-C will cancel the programme, and bring you back to bash. In psql, the one for postgres, it will kill the current query and will not stop the psql programme. Is there any way to get the psql style behaviour for Control-C in the mysql programme? I keep finding myself pressing Control-C by habit and having to log back into mysql.

Control C doesn't kill bash, and log you out when you press it. Which I think is a good thing. Control-C means 'stop what you're doing'.

chicks
  • 3,639
  • 10
  • 26
  • 36
Amandasaurus
  • 30,211
  • 62
  • 184
  • 246

4 Answers4

13

Like Andy says, it's fixed in later versions.

There's no equivalent keyboard shortcut in older versions.

But you can prevent it from happening with the following CLI flag:

--sigint-ignore

Ignore SIGINT signals (typically the result of typing Control-C).
Dan Carley
  • 25,189
  • 5
  • 52
  • 70
  • The catch is that --sigint-ignore also removes the ability to stop long running queries. Oh! and nothing relevant to the question has been fixed in later version (<=5.5) AFAICT. See my answer bellow. – ndemou Sep 14 '14 at 19:00
7

If you press Ctrl-C in 5.1 during query execution, it will halt the execution with the message "Query execution was interrupted"

    mysql> INSERT INTO c SELECT rand()*1000, sha1(rand()) FROM c;
    Query aborted BY Ctrl+C
    ERROR 1317 (70100): Query execution was interrupted

A further Ctrl-C press kills the client.

Older versions die horribly as noted in question, so upgrade if possible :)

Edit:

Looks like it was added in 5.0.25 (and 5.1.10).

chicks
  • 3,639
  • 10
  • 26
  • 36
Andy
  • 5,190
  • 23
  • 34
  • 2
    It still has pretty stupid behaviour. When you press CTRL+C during a query, it should cancel the query. When you press CTRL+C at the prompt, it should do nothing and NOT exit MySQL. When you CTRL+C at the Bash prompt when nothing is happening, it does prints a new line, but basically does nothing. – Neil Feb 11 '10 at 15:43
  • Actually what I'd rather it did was exactly what psql does: if you are half way into entering a query, perhaps having entered several lines already, and you realize you have made a mistake and want to abort entering it, then pressing CTRL+C should clear back to the prompt. – harmic Mar 10 '14 at 23:09
  • I'll make it clear: ctrl-C **still** drops you out of mysql CLI even at version 5.5. Andy's answer is a bit vague (to me at least) but if it made you feel that newer versions fixed this behaviour you've been misled. – ndemou Sep 02 '14 at 21:16
  • @ndemou which platform are you on? My answer was tested on RHEL5. – Andy Sep 09 '14 at 16:49
  • @Andy tested on ubuntu 12.04. You are writing "Looks like it was added in 5.0.25". What do you think was added? That's the partof your answer that idon't understand. See also my answer below. – ndemou Sep 10 '14 at 04:16
6

If you add a \c to then end of your line instead of using Ctrl-c then you can kill the line without exiting mysql.

user32860
  • 161
  • 1
  • 2
3

Short answer: As of early 2014 there is no way to have the described behaviour in mysql. See relevant bug report and a page in dba.stackexchange where 3 different people comment that it's still not fixed in v5.5. Also as far as I can tell after a long googling there is no workaround worth using (see bellow why --sigint-ignore is not good). Andy's answer was edited to mention that "Looks like it was added in 5.0.25 (and 5.1.10)." but he was probably misled by comment with date [3 May 2006 19:04] in the aforementioned bug report.

The --sigint-ignore argument (see Dan Carley's answer) will indeed stop mysql from dropping you back to the shell when you hit ctrl-C but will also remove the ability to stop long running queries.

This is the kind of usability bugs that makes you go :-( Really obvious and probably extremely easy to fix but still here it is after many years.

ndemou
  • 1,215
  • 2
  • 16
  • 27