How to exit MySQL command prompt?

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?


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.


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.


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


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


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