10bet网址
MySQL8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr)- 41.6Mb
PDF (A4)- 41.7Mb
Man Pages (TGZ)- 262.1 kb
Man Pages (Zip)- 372.2Kb
Info (Gzip)- 4.0Mb
Info (Zip)- 4.0Mb
Excerpts from this Manual

MySQL8.0 Reference Manual/.../ Connecting to the MySQL Server Using Command Options

4.2.4 Connecting to the MySQL Server Using Command Options

This section describes use of command-line options to specify how to establish connections to the MySQL server, for clients such asmysqlormysqldump. For information on establishing connections using URI-like connection strings or key-value pairs, for clients such as MySQL Shell, seeSection 4.2.5, “Connecting to the Server Using URI-Like Strings or Key-Value Pairs”. For additional information if you are unable to connect, seeSection 6.2.21, “Troubleshooting Problems Connecting to 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.

The examples here use themysqlclient program, but the principles apply to other clients such asmysqldump,mysqladmin, ormysqlshow.

This command invokesmysqlwithout specifying any explicit connection parameters:

mysql

Because there are no parameter options, the default values apply:

  • The default host name islocalhost. On Unix, this has a special meaning, as described later.

  • The default user name isODBCon Windows or your Unix login name on Unix.

  • No password is sent because neither--passwordnor-pis given.

  • Formysql, the first nonoption argument is taken as the name of the default database. Because there is no such argument,mysqlselects 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:

  • If you use a--passwordor-poption and specify a password value, there must beno spacebetween--password=or-pand the password following it.

  • 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 asps. SeeSection 6.1.2.1, “End-User Guidelines for Password Security”.

  • 一个显式地指定,没有密码d that the client program should not prompt for one, use the--skip-passwordoption.

As just mentioned, including the password value on the command line is a security risk. To avoid this risk, specify the--passwordor-poption without any following password value:

mysql --host=localhost --user=myname --password mydb mysql -h localhost -u myname -p mydb

When the--passwordor-poption is given with no password value, the client program prints a prompt and waits for you to enter the password. (In these examples,mydbisnotinterpreted as a password because it is separated from the preceding password option by a space.)

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 (seeSection 4.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:

  • If the host is not specified or islocalhost, a connection to the local host occurs:

    • On Windows, the client connects using shared memory, if the server was started with theshared_memorysystem variable enabled to support shared-memory connections.

    • 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.

  • 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.

  • Otherwise, the connection uses TCP/IP.

The--protocoloption enables you to use a particular transport protocol even when other options normally result in use of a different protocol. That is,--protocolspecifies the transport protocol explicitly and overrides the preceding rules, even forlocalhost.

Only connection options that are relevant to the selected transport protocol are used or checked. Other connection options are ignored. For example, with--host=localhoston Unix, the client attempts to connect to the local server using a Unix socket file, even if a--portor-Poption is given to specify a TCP/IP port number.

To ensure that the client makes a TCP/IP connection to the local server, use--hostor-hto specify a host name value of127.0.0.1(而不是localhost), or the IP address or name of the local server. You can also specify the transport protocol explicitly, even forlocalhost,通过使用--protocol=TCPoption. Examples:

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. SeeSection 5.1.13, “IPv6 Support”.

On Windows, to force a MySQL client to use a named-pipe connection, specify the--pipeor--protocol=PIPEoption, or specify.(period) as the host name. If the server was not started with thenamed_pipesystem variable enabled to support named-pipe connections 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. Use the--socketoption to specify the name of the pipe if you do not want to use the default pipe name.

Connections to remote servers use TCP/IP. This command connects to the server running onremote.example.comusing the default port number (3306):

mysql --host=remote.example.com

To specify a port number explicitly, use the--portor-Poption:

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 tolocalhoston 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.

For this command, the program uses a socket file on Unix and the--portoption is ignored:

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:

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, seeSection 4.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:

  • 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

    For more information, seeSection 4.2.2.2, “Using Option Files”.

  • Some connection parameters can be specified using environment variables. Examples:

    • To specify the host formysql, useMYSQL_HOST.

    • On Windows, to specify the MySQL user name, useUSER.

    For a list of supported environment variables, seeSection 4.9, “Environment Variables”.