4.2.4 Connecting to the MySQL Server Using Command Options使用命令选项连接到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. 本节介绍如何使用命令行选项指定如何为mysqlmysqldump等客户端建立与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客户端程序,但这些原则适用于其他客户端,如mysqldumpmysqladminmysqlshow

This command invokes mysql without specifying any explicit connection parameters:此命令在不指定任何显式连接参数的情况下调用mysql

mysql

Because there are no parameter options, the default values apply:因为没有参数选项,所以应用默认值:

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:对于密码选项,密码值是可选的:

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:客户端程序确定要进行哪种类型的连接,如下所示:

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 --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.例如,在Unix上使用--host=localhost时,客户端会尝试使用Unix套接字文件连接到本地服务器,即使给出了--port-P选项来指定TCP/IP端口号。

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. 为确保客户端与本地服务器建立TCP/IP连接,请使用--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 --host=::1. See Section 5.1.13, “IPv6 Support”.如果服务器配置为接受IPv6连接,则客户端可以使用--host=::1通过IPv6连接到本地服务器。请参阅第5.1.13节,“IPv6支持”

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. 在Windows上,要强制MySQL客户端使用命名管道连接,请指定--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 remote.example.com using the default port number (3306):与远程服务器的连接使用TCP/IP。此命令使用默认端口号(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 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上连接到localhost使用套接字文件,因此,除非您按照前面的描述强制TCP/IP连接,否则任何指定端口号的选项都将被忽略。

For this command, the program uses a socket file on Unix and the --port option is ignored:对于此命令,程序在Unix上使用套接字文件,忽略--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:每次调用客户端程序时,可以指定连接参数,而无需在命令行上输入: