Home >Database >Mysql Tutorial >Connect to MySQL server using command options
Let's see how to establish a connection with a MySQL server using command line options, such as a client like mysql or mysqldump.
In order for the client program to connect to the MySQL server, it must use the correct connection parameters, such as the host name where the server is running, the username and password of the MySQL account. Each connection parameter has a default value, but can be overridden if necessary using program options specified on the command line or in an options file.
The command to call mysql without specifying any explicit connection parameters is −
mysql
Since there are no parameter options, the default values will be applied.
The default host name is localhost. On Unix, it has a special meaning.
The default username on Windows is ODBC. On Unix, is the login name on Unix.
No password was sent because no --password or -p was supplied.
For mysql, the first parameter is treated as the name of the default database. Since there is no such parameter, mysql does not select any default database.
To explicitly specify hostname, username, and password, the appropriate options must be provided on the command line. As shown below:
mysql --host=localhost --user=myname --password=password mydb mysql -h localhost -u myname -ppassword mydb
The password value is optional.
If a --password or -p option is present, and a password value is mentioned, there shouldn't 't be any space between --password= or -p and the password that follows it.
If --password or -p doesn't specify a password value, the client program prompts the user to enter the password. The password doesn't get displayed when it is entered.
Next step is for client programs to determine the type of connection that needs to be made. To ensure that the client makes a TCP/IP connection to the local server only, the --host or -h option is used to specify a host name with the value of 127.0.0.1 (instead of localhost). Instead of this, the IP address or name of the local server can also be provided. The transport protocol can be explicitly mentioned even for localhost using the --protocol=TCP option. Some examples have been shown below −
mysql --host=127.0.0.1 mysql --protocol=TCP
If connections need to be made to remote servers, then use TCP/IP. This command would help connect to the server that runs on remote.example.com using the default port number which is 3306. It has been shown below −
mysql --host=remote.example.com
If the user wants to display a specific port number, the - -port or –P option needs to be mentioned −
mysql --host=remote.example.com --port=13306
The above is the detailed content of Connect to MySQL server using command options. For more information, please follow other related articles on the PHP Chinese website!