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

王林
王林 转载
2023-09-02 08:21:03 330浏览

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

让我们看看如何使用命令行选项与MySQL服务器建立连接,例如mysql或mysqldump这样的客户端。

为了使客户端程序能够连接到MySQL服务器,它必须使用正确的连接参数,例如服务器运行的主机名、MySQL帐户的用户名和密码。每个连接参数都有一个默认值,但在必要时可以使用在命令行或选项文件中指定的程序选项进行覆盖。

调用mysql

调用mysql而不指定任何显式连接参数的命令是−

mysql

由于没有参数选项,将应用默认值。

  • 默认主机名为localhost。在Unix上,它有特殊含义。

  • 默认用户名在Windows上是ODBC。在Unix上,是Unix上的登录名。

  • 没有发送密码,因为没有提供--password或-p。

  • 对于mysql,第一个参数被视为默认数据库的名称。由于没有这样的参数,因此mysql不选择任何默认数据库。

Imvoke - 指定主机名、用户名和密码

要明确指定主机名、用户名和密码,必须在命令行上提供适当的选项。如下所示:

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

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

如果用户希望显示特定的端口号,需要提到 - -port 或 –P 选项 −

mysql --host=remote.example.com --port=13306

以上就是使用命令选项连接到 MySQL 服务器的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除