首頁  >  文章  >  資料庫  >  使用命令選項連接到 MySQL 伺服器

使用命令選項連接到 MySQL 伺服器

王林
王林轉載
2023-09-02 08:21:03811瀏覽

使用命令选项连接到 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。 #

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.example.com using the default port number which is 3306. below −

mysql --host=remote.example.com

如果使用者希望顯示特定的連接埠號,則需要提到- -port 或–P 選項−

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

以上是使用命令選項連接到 MySQL 伺服器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除