How to exit MySQL command line client program

To exit the mysql command line client program, you need to issue an exit or quit command as follows:

mysql> exit
nsebhastian@fn-mac ~ $ # back to the command line

However, there are times when you enter the mysql prompt and can’t exit normally because MySQL thinks you are still issuing a query.

In the following example, I mistakenly typed SELECT and hit the ENTER key, causing the mysql client program to think I’m issuing a multi-line command.

When I run the exit command, I’m still inside the program as follows:

mysql> SELECT 
    -> exit

This is because MySQL allows you to create a multi-line statement to run from the command line.

To stop this multi-line prompt mode, you need to issue the \c command to clear the current input statement:

mysql> SELECT 
    -> exit
    -> \c

The \c command cancels the current input and returns you to mysql prompt.

Alternatively, you can also send a semicolon ; to mysql so execute the current input.

It will probably cause an error, but it will take you back to the empty prompt:

mysql> SELECT
    -> exit
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near 'exit' at line 2


Once you’re back in the empty prompt, you can run the exit command again.

Finally, if all else fails, you can try exiting mysql command line by using the CTRL + D command.

The command should terminate the current mysql session and return you to the command line prompt.

Get 50 MySQL Snippets Book for FREE 🔥

50 MySQL snippets that you can use in various scenarios

Save 1000+ hours of research and 10x your productivity