Home > Database > Mysql Tutorial > body text

Connect to MySQL server using command options

王林
Release: 2023-09-02 08:21:03
forward
856 people have browsed it

使用命令选项连接到 MySQL 服务器

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.

Call mysql

The command to call mysql without specifying any explicit connection parameters is −

mysql
Copy after login

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.

Imvoke - Specify hostname, username, and password

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
Copy after login

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.

Type of Connection

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
Copy after login

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
Copy after login

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
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template