MySQL :: MySQL 5.7 Reference Manual :: 4.5.1.2 mysql Client Commands (original) (raw)

4.5.1.2 mysql Client Commands

mysql sends each SQL statement that you issue to the server to be executed. There is also a set of commands that mysql itself interprets. For a list of these commands, type help or\h at the mysql> prompt:

mysql> help

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear the current input statement.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given
               outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing
               binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.

For server side help, type 'help contents'

If mysql is invoked with the--binary-mode option, allmysql commands are disabled exceptcharset and delimiter in noninteractive mode (for input piped to mysql or loaded using the source command).

Each command has both a long and short form. The long form is not case-sensitive; the short form is. The long form can be followed by an optional semicolon terminator, but the short form should not.

The use of short-form commands within multiple-line /* ... */ comments is not supported. Short-form commands do work within single-line /*! ... */ version comments, as do /*+ ... */ optimizer-hint comments, which are stored in object definitions. If there is a concern that optimizer-hint comments may be stored in object definitions so that dump files when reloaded withmysql would result in execution of such commands, either invoke mysql with the--binary-mode option or use a reload client other than mysql.

mysql> SELECT LAST_INSERT_ID(3);  
+-------------------+  
| LAST_INSERT_ID(3) |  
+-------------------+  
|                 3 |  
+-------------------+  
mysql> SELECT LAST_INSERT_ID();  
+------------------+  
| LAST_INSERT_ID() |  
+------------------+  
|                3 |  
+------------------+  
mysql> resetconnection;  
mysql> SELECT LAST_INSERT_ID();  
+------------------+  
| LAST_INSERT_ID() |  
+------------------+  
|                0 |  
+------------------+  

Here are a few tips about the pager command:

mysql> pager cat > /tmp/log.txt  

You can also pass any options for the program that you want to use as your pager:

mysql> pager less -n -i -S  
man less  
mysql> pager less -n -i -S -F -X  
mysql> pager cat | tee /dr1/tmp/res.txt \  
          | tee /dr2/tmp/res2.txt | less -n -i -S  

In this example, the command would send query results to two files in two different directories on two different file systems mounted on /dr1 and/dr2, yet still display the results onscreen using less.

You can also combine the tee andpager functions. Have atee file enabled and pager set to less, and you are able to browse the results using the less program and still have everything appended into a file the same time. The difference between the Unix tee used with thepager command and themysql built-in tee command is that the built-in tee works even if you do not have the Unix tee available. The built-intee also logs everything that is printed on the screen, whereas the Unix tee used withpager does not log quite that much. Additionally, tee file logging can be turned on and off interactively from within mysql. This is useful when you want to log some queries to a file, but not others.

The prompt command reconfigures the defaultmysql> prompt. The string for defining the prompt can contain the following special sequences.

Option Description
\C The current connection identifier
\c A counter that increments for each statement you issue
\D The full current date
\d The default database
\h The server host
\l The current delimiter
\m Minutes of the current time
\n A newline character
\O The current month in three-letter format (Jan, Feb, …)
\o The current month in numeric format
\P am/pm
\p The current TCP/IP port or socket file
\R The current time, in 24-hour military time (0–23)
\r The current time, standard 12-hour time (1–12)
\S Semicolon
\s Seconds of the current time
\t A tab character
\U Your full_user_name_@host_name account name
\u Your user name
\v The server version
\w The current day of the week in three-letter format (Mon, Tue, …)
\Y The current year, four digits
\y The current year, two digits
\_ A space
\ A space (a space follows the backslash)
\' Single quote
\" Double quote
\\ A literal \ backslash character
\x x, for any“_x_” not listed above

You can set the prompt in several ways:

export MYSQL_PS1="(\u@\h) [\d]> "  
$> mysql --prompt="(\u@\h) [\d]> "  
(user@host) [database]>  
[mysql]  
prompt=(\\u@\\h) [\\d]>\\_  

In this example, note that the backslashes are doubled. If you set the prompt using the prompt option in an option file, it is advisable to double the backslashes when using the special prompt options. There is some overlap in the set of permissible prompt options and the set of special escape sequences that are recognized in option files. (The rules for escape sequences in option files are listed in Section 4.2.2.2, “Using Option Files”.) The overlap may cause you problems if you use single backslashes. For example, \s is interpreted as a space rather than as the current seconds value. The following example shows how to define a prompt within an option file to include the current time in_`hh:mm:ss`_> format:

[mysql]  
prompt="\\r:\\m:\\s> "  
mysql> prompt (\u@\h) [\d]>\_  
PROMPT set to '(\u@\h) [\d]>\_'  
(user@host) [database]>  
(user@host) [database]> prompt  
Returning to default PROMPT of mysql>  
mysql>