This section describes use of command-line options to specify how to establish connections to the MySQL server, for clients such as mysql or mysqldump. 本节介绍如何使用命令行选项指定如何为mysql或mysqldump等客户端建立与MySQL服务器的连接。For information on establishing connections using URI-like connection strings or key-value pairs, for clients such as MySQL Shell, see Section 4.2.5, “Connecting to the Server Using URI-Like Strings or Key-Value Pairs”. 有关使用类URI连接字符串或键值对建立连接的信息,对于MySQL Shell等客户端,请参阅第4.2.5节,“使用类URI字符串或键值配对连接到服务器”。For additional information if you are unable to connect, see Section 6.2.21, “Troubleshooting Problems Connecting to MySQL”.如果您无法连接,有关更多信息,请参阅第6.2.21节,“连接到MySQL的故障排除”。
For a client program to connect to the MySQL server, it must use the proper connection parameters, such as the name of the host where the server is running and the user name and password of your MySQL account. 为了使客户端程序连接到MySQL服务器,它必须使用正确的连接参数,例如服务器运行的主机名以及MySQL帐户的用户名和密码。Each connection parameter has a default value, but you can override default values as necessary using program options specified either on the command line or in an option file.每个连接参数都有一个默认值,但您可以根据需要使用在命令行或选项文件中指定的程序选项覆盖默认值。
The examples here use the mysql client program, but the principles apply to other clients such as mysqldump, mysqladmin, or mysqlshow.这里的示例使用mysql客户端程序,但这些原则适用于其他客户端,如mysqldump、mysqladmin或mysqlshow。
This command invokes mysql without specifying any explicit connection parameters:此命令在不指定任何显式连接参数的情况下调用mysql:
mysql
Because there are no parameter options, the default values apply:因为没有参数选项,所以应用默认值:
The default host name is 默认主机名为localhost。在Unix上,这有一个特殊的含义,如稍后所述。localhost
. On Unix, this has a special meaning, as described later.
The default user name is 默认用户名在Windows上为ODBC,在Unix上为Unix登录名。ODBC
on Windows or your Unix login name on Unix.
No password is sent because neither 没有发送密码,因为既没有给出--password
nor -p
is given.--password
也没有给出-p
。
For mysql, the first nonoption argument is taken as the name of the default database. 对于mysql,第一个nonoption参数被视为默认数据库的名称。Because there is no such argument, mysql selects no default database.因为没有这样的参数,mysql不选择默认数据库。
To specify the host name and user name explicitly, as well as a password, supply appropriate options on the command line. To select a default database, add a database-name argument. Examples:要明确指定主机名和用户名以及密码,请在命令行上提供适当的选项。要选择默认数据库,请添加数据库名称参数。示例:
mysql --host=localhost --user=myname --password=password
mydb mysql -h localhost -u myname -ppassword
mydb
For password options, the password value is optional:对于密码选项,密码值是可选的:
If you use a 如果使用--password
or -p
option and specify a password value, there must be no space between --password=
or -p
and the password following it.--password
或-p
选项并指定密码值,则--password=
或-p
与其后面的密码之间不得有空格。
If you use 如果使用--password
or -p
but do not specify a password value, the client program prompts you to enter the password. --password
或-p
但未指定密码值,则客户端程序会提示您输入密码。The password is not displayed as you enter it. 输入密码时不会显示密码。This is more secure than giving the password on the command line, which might enable other users on your system to see the password line by executing a command such as ps. 这比在命令行上提供密码更安全,因为命令行可能会使系统上的其他用户通过执行ps等命令来查看密码行。See Section 6.1.2.1, “End-User Guidelines for Password Security”.请参阅第6.1.2.1节,“密码安全最终用户指南”。
To explicitly specify that there is no password and that the client program should not prompt for one, use the 要明确指定没有密码并且客户端程序不应提示输入密码,请使用--skip-password
option.--skip-password
选项。
As just mentioned, including the password value on the command line is a security risk. 正如刚才提到的,在命令行中包含密码值是一种安全风险。To avoid this risk, specify the 为了避免这种风险,请指定--password
or -p
option without any following password value:--password
或-p
选项,而不指定任何以下密码值:
mysql --host=localhost --user=myname --password mydb mysql -h localhost -u myname -p mydb
When the 当给出--password
or -p
option is given with no password value, the client program prints a prompt and waits for you to enter the password. --password
或-p
选项而没有密码值时,客户端程序会打印一个提示并等待您输入密码。(In these examples, (在这些示例中,mydb
is not interpreted as a password because it is separated from the preceding password option by a space.)mydb
不被解释为密码,因为它与前面的密码选项用空格隔开。)
On some systems, the library routine that MySQL uses to prompt for a password automatically limits the password to eight characters. That limitation is a property of the system library, not MySQL. 在某些系统上,MySQL用于提示输入密码的库例程会自动将密码限制为八个字符。这种限制是系统库的属性,而不是MySQL。Internally, MySQL does not have any limit for the length of the password. To work around the limitation on systems affected by it, specify your password in an option file (see Section 4.2.2.2, “Using Option Files”). 在内部,MySQL对密码的长度没有任何限制。要解决受其影响的系统的限制,请在选项文件中指定您的密码(请参阅第4.2.2.2节,“使用选项文件”)。Another workaround is to change your MySQL password to a value that has eight or fewer characters, but that has the disadvantage that shorter passwords tend to be less secure.另一种解决方法是将MySQL密码更改为包含八个或更少字符的值,但这样做的缺点是较短的密码往往不太安全。
Client programs determine what type of connection to make as follows:客户端程序确定要进行哪种类型的连接,如下所示:
If the host is not specified or is 如果未指定主机或主机是localhost
, a connection to the local host occurs:localhost
,则会连接到本地主机:
On Windows, the client connects using shared memory, if the server was started with the 在Windows上,如果服务器启动时启用了shared_memory
system variable enabled to support shared-memory connections.shared_memory
系统变量以支持共享内存连接,则客户端将使用共享内存进行连接。
On Unix, MySQL programs treat the host name 在Unix上,MySQL程序对主机名localhost
specially, in a way that is likely different from what you expect compared to other network-based programs: the client connects using a Unix socket file. localhost
的处理方式可能与其他基于网络的程序不同:客户端使用Unix套接字文件进行连接。The 可以使用--socket
option or the MYSQL_UNIX_PORT
environment variable may be used to specify the socket name.--socket
选项或MYDateTimeUNIX_PORT
环境变量来指定套接字名称。
On Windows, if 在Windows上,如果host
is .
(period), or TCP/IP is not enabled and --socket
is not specified or the host is empty, the client connects using a named pipe, if the server was started with the named_pipe
system variable enabled to support named-pipe connections. host
是.
(句点),或者没有启用TCP/IP,并且没有指定--socket
或者主机为空,如果服务器启动时启用了named_pipe
系统变量以支持命名管道连接,则客户端将使用命名管道连接。If named-pipe connections are not supported or if the user making the connection is not a member of the Windows group specified by the 如果不支持命名管道连接,或者进行连接的用户不是named_pipe_full_access_group
system variable, an error occurs.named_pipe_full_access_group
系统变量指定的Windows组的成员,则会发生错误。
Otherwise, the connection uses TCP/IP.否则,连接将使用TCP/IP。
The --protocol
option enables you to use a particular transport protocol even when other options normally result in use of a different protocol. --protocol
选项使您能够使用特定的传输协议,即使其他选项通常会导致使用不同的协议。That is, 也就是说,--protocol
specifies the transport protocol explicitly and overrides the preceding rules, even for localhost
.--protocol
明确指定了传输协议,并覆盖了前面的规则,即使是对于localhost
也是如此。
Only connection options that are relevant to the selected transport protocol are used or checked. 仅使用或检查与所选传输协议相关的连接选项。Other connection options are ignored. 其他连接选项将被忽略。For example, with 例如,在Unix上使用--host=localhost
on Unix, the client attempts to connect to the local server using a Unix socket file, even if a --port
or -P
option is given to specify a TCP/IP port number.--host=localhost
时,客户端会尝试使用Unix套接字文件连接到本地服务器,即使给出了--port
或-P
选项来指定TCP/IP端口号。
To ensure that the client makes a TCP/IP connection to the local server, use 为确保客户端与本地服务器建立TCP/IP连接,请使用--host
or -h
to specify a host name value of 127.0.0.1
(instead of localhost
), or the IP address or name of the local server. --host
或-h
指定主机名值127.0.0.1
(而不是localhost
),或本地服务器的IP地址或名称。You can also specify the transport protocol explicitly, even for 您还可以通过使用localhost
, by using the --protocol=TCP
option. Examples:--protocol=TCP
选项显式指定传输协议,即使是对于localhost
也是如此。示例:
mysql --host=127.0.0.1 mysql --protocol=TCP
If the server is configured to accept IPv6 connections, clients can connect to the local server over IPv6 using 如果服务器配置为接受IPv6连接,则客户端可以使用--host=::1
. See Section 5.1.13, “IPv6 Support”.--host=::1
通过IPv6连接到本地服务器。请参阅第5.1.13节,“IPv6支持”。
On Windows, to force a MySQL client to use a named-pipe connection, specify the 在Windows上,要强制MySQL客户端使用命名管道连接,请指定--pipe
or --protocol=PIPE
option, or specify .
(period) as the host name. --pipe
或--protocol=PIPE
选项,或指定.
(句点)作为主机名。If the server was not started with the 如果服务器启动时未启用named_pipe
system variable enabled to support named-pipe connections or if the user making the connection is not a member of the Windows group specified by the named_pipe_full_access_group
system variable, an error occurs. named_pipe
系统变量以支持命名管道连接,或者进行连接的用户不是named_pipe_full_access_group
系统变量指定的Windows组的成员,则会发生错误。Use the 如果不想使用默认管道名称,请使用--socket
option to specify the name of the pipe if you do not want to use the default pipe name.--socket
选项指定管道的名称。
Connections to remote servers use TCP/IP. This command connects to the server running on 与远程服务器的连接使用TCP/IP。此命令使用默认端口号(3306)连接到在remote.example.com
using the default port number (3306):remote.example.com
上运行的服务器:
mysql --host=remote.example.com
To specify a port number explicitly, use the 要明确指定端口号,请使用--port
or -P
option:--port
或-P
选项:
mysql --host=remote.example.com --port=13306
You can specify a port number for connections to a local server, too. However, as indicated previously, connections to 您也可以为连接到本地服务器指定端口号。但是,如前所述,默认情况下,在Unix上连接到localhost
on Unix use a socket file by default, so unless you force a TCP/IP connection as previously described, any option that specifies a port number is ignored.localhost
使用套接字文件,因此,除非您按照前面的描述强制TCP/IP连接,否则任何指定端口号的选项都将被忽略。
For this command, the program uses a socket file on Unix and the 对于此命令,程序在Unix上使用套接字文件,忽略--port
option is ignored:--port
选项:
mysql --port=13306 --host=localhost
To cause the port number to be used, force a TCP/IP connection. For example, invoke the program in either of these ways:要使用端口号,请强制TCP/IP连接。例如,通过以下任一方式调用程序:
mysql --port=13306 --host=127.0.0.1 mysql --port=13306 --protocol=TCP
For additional information about options that control how client programs establish connections to the server, see Section 4.2.3, “Command Options for Connecting to the Server”.有关控制客户端程序如何建立与服务器连接的选项的更多信息,请参阅第4.2.3节,“连接到服务器的命令选项”。
It is possible to specify connection parameters without entering them on the command line each time you invoke a client program:每次调用客户端程序时,可以指定连接参数,而无需在命令行上输入:
Specify the connection parameters in the 在选项文件的[client]
section of an option file. The relevant section of the file might look like this:[client]
部分指定连接参数。文件的相关部分可能如下:
[client] host=host_name
user=user_name
password=password
For more information, see Section 4.2.2.2, “Using Option Files”.有关更多信息,请参阅第4.2.2.2节,“使用选项文件”。
Some connection parameters can be specified using environment variables. Examples:可以使用环境变量指定一些连接参数。示例:
To specify the host for mysql, use 要指定mysql的主机,请使用MYSQL_HOST
.MYSQL_HOST
。
On Windows, to specify the MySQL user name, use 在Windows上,要指定MySQL用户名,请使用USER
.USER
。
For a list of supported environment variables, see Section 4.9, “Environment Variables”.有关支持的环境变量列表,请参阅第4.9节,“环境变量”。