37

I have installed MySQL. Now I am stuck inside the MySQL command prompt. I ran MySQL like this:

C:\>mysql.exe
mysql>

Then I type in some invalid command like this:

mysql> /version
    ->

And no matter what I type, I can't exit MySQL command-line / terminal. E.g.:

  • exit

  • CtrlC

  • CtrlD

  • quit

  • Ctrl\

  • CtrlZ

  • bye

How do I exit the MySQL terminal to the default terminal?

  • 1
    Did you tried shutting down your computer? /s – VarunAgw Jul 2 '18 at 12:38
29

To add on to the other answer, you could simply end the current invalid query using a semicolon:

mysql> /version
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax.........
mysql> exit
Bye

c:\mysql\bin>

Or using \G (which is supposed to make rows display vertically):

mysql> /version
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax.........
mysql> exit
Bye

c:\mysql\bin>

Of course, both options assume you have no opening quote. If you do, you must first close it off with an ending quote.

  • What had tripped me up was I didn't fully understand how the mysql terminal treats input in its various quote modes as defined here: dev.mysql.com/doc/refman/5.0/en/entering-queries.html There are 5 modes: ->, '>, ">, `>, and /*> I tested it again on the latest versions, and it worked correctly. So it looks like a fixed bug. – Eric Leschinski Jun 30 '15 at 14:15
  • Semicolon doesn't work... nothing does. If I do \G it acts as if it exited that stupid -> mode, but then goes back into it after another command. Why is mysql so dumb about this? – Amalgovinus Mar 21 '17 at 17:50
  • The real answer is, Windows' mysql cli is screwed up and can get stuck. The easiest way on windows is just to use git bash – Amalgovinus Jun 12 '18 at 17:52
20

Why does ctrl-c not exit mysql input mode in Windows?

Because you have told MySQL to interpret your exit commands as valid input.

What makes the MySQL terminal hard to understand is there there are different modes for single quote, double quote, and normal mode.

So to get out of mysql input mode, you will have to do these steps:

  1. Get out of double quote mode.
  2. Get out of single quote mode.
  3. Get out of mysql mode.
  4. Exit mysql back to the default terminal.

Most basic example:

mysql> /version
    ->
    ->
    ->
    -> \c
mysql> exit
Bye

C:\>

You never left default mode in the above example so exit commands work correctly.

Example 2 (this is what is tripping you up).

mysql> hello
    ->
    -> look dash is on the left"
    "> In doublequote mode now, because doublequote above
    "> adding another doublequote breaks you out: "
    -> look a single quote ' here
    '> in single quote mode now.
    '> get out, in, then out again with three singlequotes: '''
    -> now it will listen to your escape code: \c
mysql> exit
Bye

C:\>

While you are in single quote mode or double quote mode, no escape sequences are respected. Even Ctrl-C and Ctrl-D are ignored in these modes.

In which one of the 26 universes does Ctrl-C not stop a program regardless of mode? We may never know. Bazinga.

  • 1
    I'm guessing quotes escape things, and its waiting for more input. That actually kind of makes sense when you might store something like "exit" inside a database – Journeyman Geek Aug 9 '13 at 0:58
  • @Eric, While Ctrl-D doesn't work when you are in quote mode, Ctrl-C still works if you are using older clients like mysql 5.5.43 and mysql 5.1.73. Tested on win 8.1. – Pacerier Jun 30 '15 at 14:03
  • Ctrl-c still doesn't leave it for me. I never get the "bye" message. Furthermore, when I'm stick in -> mode, the only thing I can do is Ctrl+C to get out? Absolutely nothing else gets a response from the interpreter. Mysql is intuitive in most cases, but this isn't one such case. – Amalgovinus Feb 10 '17 at 21:03
  • Maybe your terminal emulator is gobbling your Ctrl-codes and sending a different keycode to the process which it interprets as text. I feel your pain, Back when I wrote this post back in 2013, it was for this problem and a million others just like it that my former windows machine was nuked and chucked into the trash for contaminating my PC, it's sitting in a landfill somewhere. The only way I found to fix this was to get rid of Windows operating system and migrate to Mac and Linux ideologies which do not commit these crimes of inserting middlemen to prevent your Ctrl-C from working. – Eric Leschinski Oct 30 '18 at 12:57
1

SQL supports queries entered as multiple lines. Only when you enter a semi-colon ; will the query be executed. You also need to have terminated any strings in your query.

Watch out for copying and pasting queries with strings from a word processing package - quotes may have been replaced with 'smart quotes' and this will mess up your query.

If you've entered an unterminated query, it doesn't run, and that's why typing exit doesn't work - MySQL thinks you're still in the middle of a query. The command prompt changes to show what input is needed to terminate the query. For example a quote or double quote may be required. This is powerful and the command prompt is helpful but I found it confusing until I read the answers and comments on this thread.

read the specification here

The 'top level' prompt is:

mysql>

If you see this, then you can enter a command, end it with ; and press enter.

If you see a prompt like this:

'>
">
->

Then MySQL is waiting for you to terminate a string with a quote or a query with a semi-colon.

Here's how to tell MySQL to cancel your messed-up unterminated query and put you back to the main prompt:

\c

I think this is safer than terminating and running your unintended query. After this, you should be back to the > prompt and can exit with:

exit
1

\q does the job for me. Using Ubuntu Terminal.

0

You can try to escape by using Ctrl+Shift+D

  • 3
    This is an artifact of the specific terminal emulator you are using, so it would be helpful to know which one is yours. Yours has apparently remapped Ctrl-Shift-D to a keycode that mysql process interprets to mean exit. If this is the case, you should be able to remap keycodes in your terminal emulator so you don't have to get carpal tunnel in order to exit a program. – Eric Leschinski Oct 30 '18 at 13:01

protected by Community Sep 2 at 13:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.