本节介绍了如何使用命令行选项来指定如何与 MySQL 服务器建立连接,适用于 mysql 或 mysqldump 等客户端。有关使用类似 URI 的连接字符串或键值对建立连接的信息,适用于 MySQL Shell 等客户端,请参阅 第 6.2.5 节,“使用类似 URI 字符串或键值对连接到服务器”。如果无法连接的其他信息,请参阅 第 8.2.22 节,“MySQL 连接问题排查”。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. For information on establishing connections using URI-like connection strings or key-value pairs, for clients such as MySQL Shell, see Section 6.2.5, “Connecting to the Server Using URI-Like Strings or Key-Value Pairs”. For additional information if you are unable to connect, see Section 8.2.22, “Troubleshooting Problems Connecting to MySQL”.
为了让客户端程序连接到 MySQL 服务器,必须使用正确的连接参数,例如服务器运行所在主机的名称以及 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. 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.
这里的示例使用了mysql客户端程序,但这些原理同样适用于其他客户端,如mysqldump、mysqladmin,或mysqlshow。The examples here use the mysql client program, but the principles apply to other clients such as mysqldump, mysqladmin, or mysqlshow.
此命令在未指定任何明确连接参数的情况下调用mysql:This command invokes mysql without specifying any explicit connection parameters:
mysql
由于没有参数选项,将使用默认值:Because there are no parameter options, the default values apply:
默认主机名是
localhost。在 Unix 系统上,这有特殊含义,如后文所述。The default host name islocalhost. On Unix, this has a special meaning, as described later.默认用户名在 Windows 上是
ODBC,在 Unix 上是你的 Unix 登录名。The default user name isODBCon Windows or your Unix login name on Unix.没有发送密码,因为既没有提供
--password,也没有提供-p。No password is sent because neither--passwordnor-pis given.对于 mysql,第一个非选项参数被视为默认数据库的名称。由于没有此类参数,mysql不会选择默认数据库。For mysql, the first nonoption argument is taken as the name of the default database. Because there is no such argument, mysql selects no default database.
要明确指定主机名和用户名以及密码,请在命令行上提供相应的选项。要选择默认数据库,请添加数据库名称参数。示例: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=passwordmydb mysql -h localhost -u myname -ppasswordmydb
对于密码选项,密码值是可选的:For password options, the password value is optional:
如果您使用
--password或-p选项并指定密码值,那么在--password=或-p与其后的密码之间 不能有空格。If you use a--passwordor-poption and specify a password value, there must be no space between--password=or-pand the password following it.如果您使用
--password或-p但未指定密码值,客户端程序会提示您输入密码。输入时密码不会显示。这比在命令行中提供密码更安全,因为在命令行中输入密码可能会被系统上的其他用户通过执行诸如 ps 的命令看到密码行。请参阅 第 8.1.2.1 节,“终端用户密码安全指南”。If you use--passwordor-pbut do not specify a password value, the client program prompts you to enter the password. 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. See Section 8.1.2.1, “End-User Guidelines for Password Security”.若要明确指定没有密码,并且客户端程序不应提示输入密码,请使用
--skip-password选项。To explicitly specify that there is no password and that the client program should not prompt for one, use the--skip-passwordoption.
如前所述,在命令行中包含密码值是一种安全风险。为避免此风险,请使用 --password 或 -p 选项,但不要跟随任何密码值: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:
mysql --host=localhost --user=myname --password mydb mysql -h localhost -u myname -p mydb
当 --password 或 -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. (In these examples, mydb is
not interpreted as a password because it is
separated from the preceding password option by a space.)
在某些系统上,MySQL 用于提示密码的库例程会自动将密码限制为八个字符。这个限制是系统库的属性,而不是 MySQL 的属性。在内部,MySQL 对密码长度没有任何限制。为了绕过受影响系统的限制,可以在选项文件中指定密码(参见 第 6.2.2.2 节,“使用选项文件”)。另一种解决方法是将 MySQL 密码更改为八个或更少字符的值,但这样做的缺点是较短的密码往往安全性较低。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. 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 6.2.2.2, “Using Option Files”). 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.
客户端程序按照以下方式确定建立何种类型的连接:Client programs determine what type of connection to make as follows:
如果未指定主机或主机为
localhost,将连接到本地主机:If the host is not specified or islocalhost, a connection to the local host occurs:在 Windows 上,如果服务器启用了
shared_memory系统变量以支持共享内存连接,则客户端使用共享内存进行连接。On Windows, the client connects using shared memory, if the server was started with theshared_memorysystem variable enabled to support shared-memory connections.在 Unix 上,MySQL 程序会以特殊方式处理主机名
localhost,这种方式可能与您对其他基于网络的程序的预期不同:客户端使用 Unix 套接字文件进行连接。可使用--socket选项或MYSQL_UNIX_PORT环境变量来指定套接字名称。On Unix, MySQL programs treat the host namelocalhostspecially, 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. The--socketoption or theMYSQL_UNIX_PORTenvironment variable may be used to specify the socket name.
在 Windows 上,如果
host是.(句点),或者未启用 TCP/IP 并且未指定--socket,或者主机为空,客户端将通过命名管道连接,如果服务器启动时启用了named_pipe系统变量以支持命名管道连接。如果不支持命名管道连接,或者发起连接的用户不是由named_pipe_full_access_group系统变量指定的 Windows 组的成员,将会发生错误。On Windows, ifhostis.(period), or TCP/IP is not enabled and--socketis not specified or the host is empty, the client connects using a named pipe, if the server was started with thenamed_pipesystem variable enabled to support named-pipe connections. If named-pipe connections are not supported or if the user making the connection is not a member of the Windows group specified by thenamed_pipe_full_access_groupsystem variable, an error occurs.否则,连接使用 TCP/IP。Otherwise, the connection uses TCP/IP.
--protocol 选项使您能够使用特定的传输协议,即使其他选项通常会导致使用不同的协议。也就是说,--protocol 显式指定传输协议,并覆盖之前的规则,即使对于 localhost 也是如此。The --protocol option enables you
to use a particular transport protocol even when other options
normally result in use of a different protocol. That is,
--protocol specifies the transport
protocol explicitly and overrides the preceding rules, even for
localhost.
只有与所选传输协议相关的连接选项会被使用或检查。其他连接选项将被忽略。例如,在 Unix 上使用 --host=localhost 时,客户端会尝试通过 Unix 套接字文件连接到本地服务器,即使提供了用于指定 TCP/IP 端口号的 --port 或 -P 选项。Only connection options that are relevant to the selected
transport protocol are used or checked. Other connection options
are ignored. For example, with
--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.
为了确保客户端与本地服务器建立 TCP/IP 连接,请使用 --host 或 -h 指定主机名为 127.0.0.1(而不是 localhost),或者指定本地服务器的 IP 地址或名称。即使是 localhost,你也可以使用 --protocol=TCP 选项显式指定传输协议。示例:To ensure that the client makes a TCP/IP connection to the local
server, use --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. You can also specify the transport protocol
explicitly, even for localhost, by using the
--protocol=TCP option. Examples:
mysql --host=127.0.0.1 mysql --protocol=TCP
如果服务器配置为接受 IPv6 连接,客户端可以使用 --host=::1 通过 IPv6 连接到本地服务器。请参见 第 7.1.13 节,“IPv6 支持”。If the server is configured to accept IPv6 connections, clients
can connect to the local server over IPv6 using
--host=::1. See
Section 7.1.13, “IPv6 Support”.
在 Windows 上,要强制 MySQL 客户端使用命名管道连接,请指定 --pipe 或 --protocol=PIPE 选项,或者将 .(点)指定为主机名。如果服务器未使用 named_pipe 系统变量启动以支持命名管道连接,或者发起连接的用户不是由 named_pipe_full_access_group 系统变量指定的 Windows 组的成员,则会发生错误。如果不想使用默认管道名称,请使用 --socket 选项指定管道名称。On Windows, to force a MySQL client to use a named-pipe
connection, specify the --pipe or
--protocol=PIPE option, or specify
. (period) as the host name. 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. Use the
--socket option to specify the
name of the pipe if you do not want to use the default pipe name.
连接到远程服务器使用 TCP/IP。此命令使用默认端口号(3306)连接运行在 remote.example.com 的服务器:Connections to remote servers use TCP/IP. This command connects to
the server running on remote.example.com using
the default port number (3306):
mysql --host=remote.example.com
要明确指定端口号,请使用 --port 或 -P 选项:To specify a port number explicitly, use the
--port or -P
option:
mysql --host=remote.example.com --port=13306
您也可以为本地服务器的连接指定端口号。不过,如前所述,在 Unix 上连接 localhost 默认使用套接字文件,因此除非按前面描述的方法强制使用 TCP/IP 连接,否则任何指定端口号的选项都会被忽略。You can specify a port number for connections to a local server,
too. However, as indicated previously, connections to
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.
对于此命令,程序在 Unix 上使用套接字文件,因此 --port 选项将被忽略:For this command, the program uses a socket file on Unix and the
--port option is ignored:
mysql --port=13306 --host=localhost
要使用端口号,请强制建立 TCP/IP 连接。例如,可以通过以下任一方式调用该程序:To cause the port number to be used, force a TCP/IP connection. For example, invoke the program in either of these ways:
mysql --port=13306 --host=127.0.0.1 mysql --port=13306 --protocol=TCP
有关控制客户端程序如何与服务器建立连接的选项的更多信息,请参阅第 6.2.3 节,“连接到服务器的命令选项”。For additional information about options that control how client programs establish connections to the server, see Section 6.2.3, “Command Options for Connecting to the Server”.
可以在不每次调用客户端程序时在命令行输入连接参数的情况下指定连接参数:It is possible to specify connection parameters without entering them on the command line each time you invoke a client program:
在选项文件的
[client]部分指定连接参数。文件的相关部分可能如下所示:Specify the connection parameters in the[client]section of an option file. The relevant section of the file might look like this:[client] host=
host_nameuser=user_namepassword=password有关更多信息,请参见 第 6.2.2.2 节,“使用选项文件”。For more information, see Section 6.2.2.2, “Using Option Files”.
一些连接参数可以使用环境变量来指定。示例:Some connection parameters can be specified using environment variables. Examples:
有关支持的环境变量列表,请参见 第 6.9 节,“环境变量”。For a list of supported environment variables, see Section 6.9, “Environment Variables”.