Use the command option to connect to the MySQL server (refer to the MySQL official documentation)

Use the command option to connect to the MySQL server (refer to the MySQL official documentation)
This article describes how to use the command-line option to specify how to establish a connection to the MySQL server is such as MySQL or mysqldump client.
The client program to connect to the MySQL server, you must use the correct connection parameters, such as user name and password for the server running the hostname and MySQL account.
Each connection parameter has a default value, but can be specified on the command line or option file option program as needed to override the default values.
Here's an example of using the mysql client program, but the principles apply to other clients, such as
mysqldump, mysqladmin, or mysqlshow.

This call mysql command without specifying any explicit connection parameters:
mysql

In the absence of parameter options, the application defaults:

• The default host name localhost. On Unix, which has a special meaning, as described later.
• The default user name is the login name on Unix or Unix ODBC on Windows.
• Do not send your password, because neither offer nor provide --password -p.
• For mysql, the first nonoption argument as the name of the default database. Because there is no such parameters, so do not select the default mysql database.
To explicitly specify the host name and user name and password, please provide the appropriate options on the command line. To select the default database, add the database name parameter. Example:
MySQL --host = localhost --user = myname password --password = mydb
MySQL -H -u localhost myname -Ppassword mydb

For the password, the password value is optional:
• If you use the --password or -p option and specify a password value, the -p or --password = and the password later.
• If you use the -p or --password but did not specify a password value, the client program will prompt you for a password. Password is not displayed when a password.
This is more secure than a password on the command line, which may cause other users to view the system password line by executing commands like ps
• To explicitly specify no password, and the client program should not be prompted for a password, use the - -skip password options.
As mentioned above, contains the password value may pose a security risk on the command line. To avoid this risk, specify the --password or -p option, without specifying any of the following password value:
MySQL --host = localhost --user --password = myname mydb
MySQL -h localhost -u -p mydb myname

When the --password or -p option without a password value, the client program prints a prompt and waits for you to enter the password. (In these examples, mydb not be interpreted as a password because it is separated from the preceding password option by a space.)
On some systems, MySQL prompted for a password for the library routines automatically Passwords are limited to eight characters . This restriction is a property of the system library, not MySQL.
In the interior, MySQL without any limitation on the length of the password. Another solution is to change the password for the MySQL contain eight characters or less value, but this may not be acceptable, because a shorter password is often less secure.
The client program determines the type of connection to be established as follows:
• If the host or host is not specified as a local host, the connection will happen to the local host:
• On Windows, if the server is
already shared memory system to enable variable supports shared memory connections.
• On Unix, MySQL programs special treatment hostname localhost, which may be different from the way other Web-based applications: client uses Unix socket file to connect. MYSQL_UNIX_PORT --socket option or an environment variable can be used to specify the socket name.
• On Windows, if the host yes. (Period), or TCP / IP is not enabled and - you do not specify a socket or host is empty, if the server is started using named pipes, the client using named pipes to connect

enabled system variables to support named pipe connections. If the members of the Windows user group does not support named pipe connections, connect or not specified by the named pipe full access to the set of system variables, an error will occur.
• Otherwise, connect using TCP / IP.
--protocol option allows you to create a specific type of connection, even if the other option usually results in using different protocols. That is, --protocol explicitly specify the connection protocol and rewrite the previous rule is true even for localhost.
Use or check connectivity options associated with the selected protocol only. Other connection options are ignored. For example, when using --host = localhost on Unix, the client tries to use to connect to the local Unix socket file server. Even given the --port or -P option to specify the port number, this will happen.
To ensure that the client and server to establish a local TCP / IP connections, use --host or -h hostname specified value 127.0.0.1, or IP address or the name of the local server. You can also use --protocol = TCP options explicitly specify the connection protocol, even for localhost. Example:
MySQL --host = 127.0.0.1
MySQL --protocol the TCP =

If the server is configured to accept connections IPv6, the client can use --host = :: 1 is connected to the local server via IPv6.
On Windows, you can specify --pipe or --protocol = PIPE option, or by specifying. (Period) as the host name.
Members of the Windows group named Named Pipes support is not enabled system variable pipeline connections if the server is started, or to connect a user is not specified by the named pipe full access to the set of system variables, an error will occur.
If you do not want to use the default pipe name, please use the --socket option to specify the name of the pipe.
Connection to the remote server using TCP / IP. This command is used to connect to remote.example.com running default port number (3306) of:
MySQL --host = remote.example.com

To explicitly specify the port number, use the --port or -P option:
MySQL --host --port = 13306 = remote.example.com

You can also specify the port number for the connection to the local server. However, as mentioned, is connected to the local host using Unix socket file by default, so unless previously mentioned force a TCP / IP connection, otherwise it will ignore any option to specify the port number.
For this command, the program used on Unix socket file and ignore --port options:
MySQL --host = localhost --port = 13306

To use the port number, forced TCP / IP connection. For example, the calling program by any of the following ways:
MySQL --host --port = 13306 = 127.0.0.1
MySQL the TCP --port = 13306 = --protocol

• In the [client] section of the file option to specify connection parameters. Related part of the file may appear as follows:
[Client]
Host = host_name
User USER_NAME =
password password =


2, connected to the compression control
connection to the server compression protocol may be used to reduce the number of bytes transmitted through the connection. By default, the connection is not compressed, but if the client and server support compression, compression can be connected.
Compression connection from the client, but will affect the CPU load client and server side, since both perform compression and decompression operations.
Because enabling compression can degrade performance, so the benefits were seen in lower network bandwidth, network transmission time cost control compression and decompression operations, and a large result set.
Compression applied to the main control program and the connection client participation / copied from the server to the server. Compression set is not available for copy control is connected, X protocol connection or joint connection table.
These configuration parameters can be used to control the compressed connections:
• --compress client program supports a command line option to specify a connection to the server using compression.
• Programs that use the MySQL C API, the function returns: compression designated server connection.
• For master / server replication, enable slave_compressed_protocol system variable specifies the master server from compression connection.
In each case, when you specify the use of compression, if supported by both parties, the connection using the zlib compression algorithm, otherwise it will fall back to uncompressed connection.

3, set the environment variable
can set the environment variable at a command prompt to call the impact of the current command processor, it can also permanently set the environment variable to affect future calls.
To permanently set the variable, the interface can be provided for this purpose in the startup file or use the system.

To specify a value for the environment variable, use the syntax for the command processor. For example, on Windows, you can set the USER variable to specify MySQL account name. To do this, use the following syntax:
the SET your_name the USER =
syntax depends shell on Unix. Suppose you want to use MYSQL_TCP_port variables specified TCP / IP port number. Typical syntax (e.g. sh, ksh, bash, zsh, etc.) as follows:
the MYSQL_TCP_PORT = 3306
Export the MYSQL_TCP_PORT

The first command sets the variable, export command to export a variable to the shell environment for MySQL and other processes can access its value.

For csh and tcsh, use setenv to make the shell variable available to the environment:
setenv MYSQL_TCP_PORT 3306

Set environment variables to take effect immediately commands can be executed at the command prompt, these settings remain in effect only until logout.
To validate the settings at each login, providing the interface to the system, or one or more of the appropriate command in the startup file, the command interpreter reads the file each time it starts.

On Windows, you can use the System control panel to set the environment variable (in the "Advanced"). On Unix, a typical shell or startup file is .bashrc .bash_profile (for bash) or .tcshrc (for tcsh).
Suppose you have MySQL installed in / usr / local / MySQL / bin, and you want to call these programs is easy.
To do this, set the value of the PATH environment variable to include this directory. For example, if the shell is bash, add the following line to the file .bashrc:
the PATH = $ {} the PATH: / usr / local / MySQL / bin

bash startup files using different login and non-login shell, so you may want to add the settings to
.bashrc for login shell, to.bash_profile for non-login shell, to ensure the path settings unchanged.

If the shell is tcsh, add the following line to .tcshrc file:
setenv the PATH $ {} the PATH: / usr / local / MySQL / bin

If the appropriate startup file does not exist in your home directory, create it using a text editor.

After modify the path settings, open Windows on a new console window or log in again on Unix, so that the settings take effect.

Guess you like

Origin www.cnblogs.com/sunkang-dba/p/12065827.html