56

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?

Pacerier
  • 26,733
  • 82
  • 197
  • 273
Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51
  • 2
    Did you tried shutting down your computer? /s –  Jul 02 '18 at 12:38
  • try `exit;` here you need to add a semicolon to terminate the satement. – Hemant Kumar Mar 25 '21 at 10:57
  • I dub this problem set domain as: "Command Line Interface Quote Mode Tarpit" where you have to unwind your delimiters in the reverse order they were issued, then you need to know the secret word like `;` or `\q` in order to tell mysql to stop receiving lines from `readline`. I prefer the behavior of postgresql where the typical unix interrupt escape codes terminate the CLI session regardless of quote mode. 300 thousand of you had to type: `How do I exit the mysql interactive terminal?`. – Eric Leschinski Nov 13 '22 at 14:07

5 Answers5

48

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.

Pacerier
  • 26,733
  • 82
  • 197
  • 273
  • 1
    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? – Alkanshel 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 – Alkanshel Jun 12 '18 at 17:52
24

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.

Eric Leschinski
  • 6,828
  • 6
  • 45
  • 51
  • 2
    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 09 '13 at 00: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. – Alkanshel Feb 10 '17 at 21:03
8

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

kenorb
  • 24,736
  • 27
  • 129
  • 199
Daniyal Javaid
  • 181
  • 1
  • 2
4

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
Little Brain
  • 155
  • 4
1

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

Albin
  • 9,307
  • 11
  • 50
  • 89
  • 4
    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