mysql sends each SQL statement that you issue to the server to be executed. mysql将您发出的每个SQL语句发送到服务器以供执行。There is also a set of commands that mysql itself interprets. 还有一组mysql自己解释的命令。For a list of these commands, type 要查看这些命令的列表,请在mysql>提示符下键入help
or \h
at the mysql>
prompt:help
或\h
:
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.
query_attributes(\) Sets string parameters (name1 value1 name2 value2 ...)
for the next query to pick up.
For server side help, type 'help contents'
If mysql is invoked with the 如果使用--binary-mode
option, all mysql commands are disabled except charset
and delimiter
in noninteractive mode (for input piped to mysql or loaded using the source
command).--binary-mode
选项调用mysql,则除非交互模式下的charset
和delimiter
(用于通过管道传输到mysql或使用源命令加载的输入)外,所有mysql命令都将被禁用。
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 with 如果担心优化器提示注释可能存储在对象定义中,因此用mysql
would result in execution of such commands, either invoke mysql with the --binary-mode
option or use a reload client other than mysql.mysql
重新加载转储文件时会导致执行此类命令,请使用--binary-mode
选项调用mysql
或使用mysql
以外的重新加载客户端。
help [
, arg
]\h [
, arg
]\? [
, arg
]? [
arg
]
Display a help message listing the available mysql commands.显示一条帮助消息,列出可用的mysql命令。
If you provide an argument to the 如果为help命令提供参数,mysql会将其用作搜索字符串,以从mysql参考手册的内容中访问服务器端帮助。help
command, mysql uses it as a search string to access server-side help from the contents of the MySQL Reference Manual. For more information, see Section 4.5.1.4, “mysql Client Server-Side Help”.有关更多信息,请参阅第4.5.1.4节,“mysql客户端-服务器端帮助”。
charset
, charset_name
\C
charset_name
Change the default character set and issue a 更改默认字符集并发出SET NAMES
statement. SET NAMES
语句。This enables the character set to remain synchronized on the client and server if mysql is run with auto-reconnect enabled (which is not recommended), because the specified character set is used for reconnects.如果mysql在启用自动重新连接的情况下运行(不建议),这使得字符集在客户端和服务器上保持同步,因为指定的字符集用于重新连接。
clear
, \c
Clear the current input. Use this if you change your mind about executing the statement that you are entering.清除当前输入。如果您改变了执行所输入语句的想法,请使用此选项。
connect [
, db_name
[host_name
]]\r [
db_name
[host_name
]]
Reconnect to the server. The optional database name and host name arguments may be given to specify the default database or the host where the server is running. If omitted, the current values are used.重新连接到服务器。可以提供可选的数据库名称和主机名参数,以指定默认数据库或服务器运行的主机。如果省略,则使用当前值。
If the 如果connect
command specifies a host name argument, that host takes precedence over any --dns-srv-name
option given at mysql startup to specify a DNS SRV record.connect
命令指定了主机名参数,则该主机优先于mysql启动时给出的任何--dns-srv-name
选项,以指定DNS SRV记录。
delimiter
, str
\d
str
Change the string that mysql interprets as the separator between SQL statements. The default is the semicolon character (更改mysql解释为SQL语句之间分隔符的字符串。默认值是分号字符(;
).;
)。
The delimiter string can be specified as an unquoted or quoted argument on the 分隔符字符串可以在分隔符命令行上指定为无引号或有引号的参数。引用可以使用单引号(delimiter
command line. Quoting can be done with either single quote ('
), double quote ("
), or backtick (`
) characters. '
)、双引号("
)或反引号(`
)字符。To include a quote within a quoted string, either quote the string with a different quote character or escape the quote with a backslash (要在引号字符串中包含引号,请用不同的引号字符对字符串进行引号,或用反斜杠(\
) character. \
)字符转义引号。Backslash should be avoided outside of quoted strings because it is the escape character for MySQL. For an unquoted argument, the delimiter is read up to the first space or end of line. For a quoted argument, the delimiter is read up to the matching quote on the line.应避免在引号字符串之外使用反斜杠,因为它是MySQL的转义符。对于未加引号的参数,分隔符被读取到第一个空格或行末。对于带引号的参数,分隔符被读取到行上的匹配引号。
mysql interprets instances of the delimiter string as a statement delimiter anywhere it occurs, except within quoted strings. mysql将分隔符字符串的实例解释为它出现的任何地方的语句分隔符,但引号内的字符串除外。Be careful about defining a delimiter that might occur within other words. 在定义可能出现在其他单词中的分隔符时要小心。For example, if you define the delimiter as 例如,如果将分隔符定义为X
, it is not possible to use the word INDEX
in statements. X
,则无法在语句中使用单词INDEX
。mysql interprets this as mysql将其解释为INDE,后跟分隔符X。INDE
followed by the delimiter X
.
When the delimiter recognized by mysql is set to something other than the default of 当mysql识别的分隔符设置为默认值;
, instances of that character are sent to the server without interpretation. ;
以外的值时,该字符的实例被发送到服务器而不进行解释。However, the server itself still interprets 然而,服务器本身仍在解释;作为语句分隔符,并相应地处理语句。;
as a statement delimiter and processes statements accordingly. This behavior on the server side comes into play for multiple-statement execution (see Multiple Statement Execution Support), and for parsing the body of stored procedures and functions, triggers, and events (see Section 25.1, “Defining Stored Programs”).服务器端的这种行为适用于多语句执行(请参阅多语句执行支持),以及解析存储过程和函数、触发器和事件的主体(请参阅第25.1节,“定义存储程序”)。
edit
, \e
Edit the current input statement. 编辑当前输入语句。mysql checks the values of the mysql检查EDITOR
and VISUAL
environment variables to determine which editor to use. EDITOR
和VISUAL
环境变量的值,以确定使用哪个编辑器。The default editor is vi if neither variable is set.如果两个变量都没有设置,默认编辑器是vi。
The edit
command works only in Unix.edit
命令仅在Unix中有效。
ego
, \G
Send the current statement to the server to be executed and display the result using vertical format.将当前语句发送到要执行的服务器,并使用垂直格式显示结果。
exit
, \q
Exit mysql.退出mysql。
go
, \g
Send the current statement to the server to be executed.将当前语句发送到要执行的服务器。
nopager
, \n
Disable output paging. See the description for 禁用输出分页。请参阅pager
.pager
的说明。
The nopager
command works only in Unix.nopager
命令仅在Unix中有效。
notee
, \t
Disable output copying to the tee file. See the description for 禁止将输出复制到tee文件。请参阅tee
.tee
的说明。
nowarning
, \w
Disable display of warnings after each statement.禁止在每条语句后显示警告。
pager [
, command
]\P [
command
]
Enable output paging. 启用输出分页。By using the 通过在调用mysql时使用--pager
option when you invoke mysql, it is possible to browse or search query results in interactive mode with Unix programs such as less, more, or any other similar program. --pager
选项,可以在Unix程序(如less、more或任何其他类似程序)的交互模式下浏览或搜索查询结果。If you specify no value for the option, mysql checks the value of the 如果您没有为该选项指定值,mysql会检查PAGER
environment variable and sets the pager to that. PAGER
环境变量的值,并将分页器设置为该值。Pager functionality works only in interactive mode.寻呼机功能仅在交互模式下工作。
Output paging can be enabled interactively with the 输出分页可以使用pager
command and disabled with nopager
. pager
命令交互式启用,也可以使用nopager
命令禁用。The command takes an optional argument; if given, the paging program is set to that. With no argument, the pager is set to the pager that was set on the command line, or 该命令接受一个可选参数;如果给定,则将寻呼程序设置为该值。在没有参数的情况下,分页器被设置为在命令行上设置的分页器,如果没有指定分页器,则设置为stdout
if no pager was specified.stdout
。
Output paging works only in Unix because it uses the 输出分页仅在Unix中有效,因为它使用popen()
function, which does not exist on Windows. popen()
函数,而Windows上不存在该函数。For Windows, the 对于Windows,可以使用tee
option can be used instead to save query output, although it is not as convenient as pager
for browsing output in some situations.tee
选项来保存查询输出,尽管在某些情况下,它不如分页器浏览输出方便。
print
, \p
Print the current input statement without executing it.打印当前输入语句而不执行它。
prompt [
, str
]\R [
str
]
Reconfigure the mysql prompt to the given string. The special character sequences that can be used in the prompt are described later in this section.将mysql提示符重新配置为给定的字符串。本节稍后将介绍提示符中可以使用的特殊字符序列。
If you specify the 如果指定不带参数的prompt命令,mysql会将prompt重置为prompt
command with no argument, mysql resets the prompt to the default of mysql>
.mysql>
的默认值。
query_attributes
name
value
[name
value
...]
Define query attributes that apply to the next query sent to the server. 定义应用于发送到服务器的下一个查询的查询属性。For discussion of the purpose and use of query attributes, see Section 9.6, “Query Attributes”.有关查询属性的目的和使用的讨论,请参阅第9.6节,“查询属性”。
The query_attributes
command follows these rules:query_attributes
命令遵循以下规则:
The format and quoting rules for attribute names and values are the same as for the 属性名称和值的格式和引用规则与delimiter
command.delimiter
命令相同。
The command permits up to 32 attribute name/value pairs. Names and values may be up to 1024 characters long. If a name is given without a value, an error occurs.该命令最多允许32个属性名称/值对。名称和值的长度最多为1024个字符。如果给定的名称没有值,则会发生错误。
If multiple 如果在执行查询之前发出多个query_attributes
commands are issued prior to query execution, only the last command applies. After sending the query, mysql clears the attribute set.query_attributes
命令,则只应用最后一个命令。发送查询后,mysql会清除属性集。
If multiple attributes are defined with the same name, attempts to retrieve the attribute value have an undefined result.如果定义了多个同名属性,则尝试检索属性值会得到未定义的结果。
An attribute defined with an empty name cannot be retrieved by name.无法按名称检索用空名称定义的属性。
If a reconnect occurs while mysql executes the query, mysql restores the attributes after reconnecting so the query can be executed again with the same attributes.如果在mysql执行查询时发生重新连接,mysql会在重新连接后恢复属性,以便可以使用相同的属性再次执行查询。
quit
, \q
Exit mysql.退出mysql。
rehash
, \#
Rebuild the completion hash that enables database, table, and column name completion while you are entering statements. 重建完成哈希,使您在输入语句时能够完成数据库、表和列名。(See the description for the (请参阅--auto-rehash
option.)--auto-rehash
选项的说明。)
resetconnection
, \x
Reset the connection to clear the session state. This includes clearing any current query attributes defined using the 重置连接以清除会话状态。这包括清除使用query_attributes
command.query_attributes
命令定义的任何当前查询属性。
Resetting a connection has effects similar to 重置连接的效果类似于mysql_change_user()
or an auto-reconnect except that the connection is not closed and reopened, and re-authentication is not done. mysql_change_user()
或自动重新连接,除了连接不会关闭和重新打开,也不会进行重新身份验证。See mysql_change_user(), and Automatic Reconnection Control.请参阅mysql_change_user()
和自动重新连接控制。
This example shows how 此示例显示了resetconnection
clears a value maintained in the session state:resetconnection
如何清除会话状态中维护的值:
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 | +------------------+
source
, file_name
\.
file_name
Read the named file and executes the statements contained therein. 读取指定的文件并执行其中包含的语句。On Windows, specify path name separators as 在Windows上,将路径名分隔符指定为/
or \\
./
或\\
。
Quote characters are taken as part of the file name itself. For best results, the name should not include space characters.引用字符被视为文件名本身的一部分。为了获得最佳效果,名称不应包含空格字符。
status
, \s
Provide status information about the connection and the server you are using. 提供有关连接和您正在使用的服务器的状态信息。If you are running with 如果您在启用--safe-updates
enabled, status
also prints the values for the mysql variables that affect your queries.--safe-updates
的情况下运行,status
还会打印影响查询的mysql变量的值。
system
, command
\!
command
Execute the given command using your default command interpreter.使用默认命令解释器执行给定的命令。
Prior to MySQL 8.0.19, the 在MySQL 8.0.19之前,system
command works only in Unix. As of 8.0.19, it also works on Windows.system
命令仅在Unix中有效。从8.0.19开始,它也适用于Windows。
tee [
, file_name
]\T [
file_name
]
By using the 通过在调用mysql时使用--tee
option when you invoke mysql, you can log statements and their output. --tee
选项,您可以记录语句及其输出。All the data displayed on the screen is appended into a given file. 屏幕上显示的所有数据都附加到给定的文件中。This can be very useful for debugging purposes also. 这对于调试目的也非常有用。mysql flushes results to the file after each statement, just before it prints its next prompt. mysql在每条语句之后,就在打印下一个提示之前,将结果刷新到文件中。Tee functionality works only in interactive mode.T恤功能仅在交互模式下有效。
You can enable this feature interactively with the 您可以使用tee
command. Without a parameter, the previous file is used. tee
命令以交互方式启用此功能。如果没有参数,则使用前一个文件。The 可以使用tee
file can be disabled with the notee
command. Executing tee
again re-enables logging.notee
命令禁用tee
文件。再次执行tee
将重新启用日志记录。
use
, db_name
\u
db_name
Use 使用db_name
as the default database.db_name
作为默认数据库。
warnings
, \W
Enable display of warnings after each statement (if there are any).允许在每条语句后显示警告(如果有的话)。
Here are a few tips about the 以下是关于pager
command:pager
命令的一些提示:
You can use it to write to a file and the results go only to the file:您可以使用它写入文件,结果只会写入文件:
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
In the preceding example, note the 在前面的示例中,请注意-S
option. You may find it very useful for browsing wide query results. -S
选项。您可能会发现它对于浏览广泛的查询结果非常有用。Sometimes a very wide result set is difficult to read on the screen. 有时,屏幕上很难看到非常宽的结果集。The -S
option to less can make the result set much more readable because you can scroll it horizontally using the left-arrow and right-arrow keys. -S
选项可以使结果集更具可读性,因为您可以使用左箭头和右箭头键水平滚动它。You can also use 您还可以在less中交互式地使用-S
interactively within less to switch the horizontal-browse mode on and off. -S
来打开和关闭水平浏览模式。For more information, read the less manual page:有关更多信息,请阅读less手册页:
man less
The 如果输出适合一个屏幕,则可以使用较少的-F
and -X
options may be used with less to cause it to exit if output fits on one screen, which is convenient when no scrolling is necessary:-F
和-X
选项使其退出,这在不需要滚动时很方便:
mysql> pager less -n -i -S -F -X
You can specify very complex pager commands for handling query output:您可以指定非常复杂的分页器命令来处理查询输出:
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./dr1
和/dr2
上安装的两个不同文件系统上的两个目录中的两个文件发送查询结果,但仍然使用较少的内容在屏幕上显示结果。
You can also combine the 您还可以组合tee
and pager
functions. tee
和pager
功能。Have a 启用tee
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. tee
文件并将pager
设置为更少,您就可以使用less程序浏览结果,并且仍然可以同时将所有内容附加到文件中。The difference between the Unix 与tee
used with the pager
command and the mysql built-in tee
command is that the built-in tee
works even if you do not have the Unix tee available. pager
命令一起使用的Unix tee
和mysql内置tee
命令之间的区别在于,即使您没有可用的Unix tee
,内置tee
也能工作。The built-in 内置的tee
also logs everything that is printed on the screen, whereas the Unix tee used with pager
does not log quite that much. tee
也会记录屏幕上打印的所有内容,而与pager
一起使用的Unix tee
不会记录那么多。Additionally, 此外,可以从mysql中交互式地打开和关闭tee文件日志记录。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 default mysql>
prompt. The string for defining the prompt can contain the following special sequences.prompt
命令重新配置默认的mysql>
提示符。用于定义提示的字符串可以包含以下特殊序列。
\C | |
\c | |
\D | |
\d | |
\h | |
\l | |
\m | |
\n | |
\O | |
\o | |
\P | am/pm |
\p | |
\R | |
\r | |
\S | |
\s | |
\t | |
\U |
|
\u | |
\v | |
\w | |
\Y | |
\y | |
\_ | |
\ | |
\' | |
\" | |
\\ | \ backslash character\ 反斜杠字符 |
\ |
|
You can set the prompt in several ways:您可以通过多种方式设置提示:
Use an environment variable.使用环境变量。 You can set the 您可以将MY_RESPS1环境变量设置为提示字符串。例如:MYSQL_PS1
environment variable to a prompt string. For example:
export MYSQL_PS1="(\u@\h) [\d]> "
Use a command-line option.使用命令行选项。 You can set the 您可以将命令行上的--prompt
option on the command line to mysql. --prompt
选项设置为mysql。For example:例如:
shell> mysql --prompt="(\u@\h) [\d]> "
(user@host) [database]>
Use an option file.使用选项文件。 You can set the 您可以在任何mysql选项文件的prompt
option in the [mysql]
group of any MySQL option file, such as /etc/my.cnf
or the .my.cnf
file in your home directory. For example:[mysql]
组中设置提示选项,例如/etc/my.cnf
或主目录中的.my.cnf
文件。例如:
[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. prompt
选项时将反斜杠加倍。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”.) (第4.2.2.2节,“使用选项文件”列出了选项文件中转义序列的规则。)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. \s
被解释为空格,而不是当前秒值。The following example shows how to define a prompt within an option file to include the current time in 以下示例显示了如何在选项文件中定义提示,以
format:hh:mm:ss
>hh:mm:ss>
格式包含当前时间:
[mysql] prompt="\\r:\\m:\\s> "
Set the prompt interactively.以交互方式设置提示。 You can change your prompt interactively by using the 您可以使用prompt
(or \R
) command. For example:prompt
(或\R
)命令以交互方式更改提示。例如:
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>