新聞中心
在《 登錄MySQL數(shù)據(jù)庫》一節(jié),我們了解到用戶可以通過 mysql 命令來登錄 MySQL 服務器。但是前面還有些參數(shù)沒有介紹到,本節(jié)將詳細介紹 MySQL 中登錄和退出服務器的方法。

啟動 MySQL 服務后,可以使用以下命令來登錄。
mysql -h hostname|hostlP -p port -u username -p DatabaseName -e "SQL語句"
對上述參數(shù)說明如下:
- -h:指定連接 MySQL 服務器的地址??梢杂脙煞N方式表示,hostname 為主機名,hostIP 為主機 IP 地址。
- -p:指定連接 MySQL 服務器的端口號,port 為連接的端口號。MySQL 的默認端口號是 3306,因此如果不指定該參數(shù),默認使用 3306 連接 MySQL 服務器。
- -u:指定連接 MySQL 服務器的用戶名,username 為用戶名。
- -p:提示輸入密碼,即提示 Enter password。
- DatabaseName:指定連接到 MySQL 服務器后,登錄到哪一個數(shù)據(jù)庫中。如果沒有指定,默認為 mysql 數(shù)據(jù)庫。
- -e:指定需要執(zhí)行的 SQL 語句,登錄 MySQL 服務器后執(zhí)行這個 SQL 語句,然后退出 MySQL 服務器。
例 1
下面使用 root 用戶登錄到 test 數(shù)據(jù)庫中,命令和運行過程如下:
C:\Users\11645>mysql -h localhost -u root -p test Enter password: **** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.29-log MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
上述命令中,通過值 localhost 指定 MySQL 服務器的地址,參數(shù) -u 指定了登錄 MySQL 服務器的用戶賬戶,參數(shù) -p 表示會出現(xiàn)輸入密碼提示信息,最后值“test”指定了登錄成功后要使用的數(shù)據(jù)庫。
由結果可以看到,輸入命令后,會出現(xiàn)“Enter password”提示信息,在這條信息之后輸入密碼,然后按 Enter 鍵。密碼正確后,就成功登錄到 MySQL 服務器了。
例 2
下面使用 root 用戶登錄到自己計算機的 mysql 數(shù)據(jù)庫,同時查詢 student 表的表結構,命令和運行過程如下:
C:\Users\11645>mysql -h localhost -u root -p test -e"DESC student" Enter password: **** +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(4) | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | | age | int(4) | YES | | NULL | | | stuno | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
結果顯示,執(zhí)行命令并輸入正確密碼后,窗口中就會顯示出 student 表的基本結構。
用戶也可以直接在 mysql 命令的 -p 后加上登錄密碼,登錄密碼與 -p 之間沒有空格。
例 3
下面使用 root 用戶登錄到自己計算機的 MySQL 服務器中,密碼直接加在 mysql 命令中。命令如下:
C:\Users\11645>mysql -h localhost -u root -proot mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.29-log MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
上述命令執(zhí)行后,后面不會提示輸入密碼。因為 -p 后面有密碼,MySQL 會直接使用這個密碼。
退出 MySQL 服務器的方式很簡單,只要在命令行輸入 EXIT 或 QUIT 即可?!癨q”是 QUIT 的縮寫,也可以用來退出 MySQL 服務器。退出后就會顯示 Bye。如下所示:
mysql> QUIT; Bye
當前標題:創(chuàng)新互聯(lián)數(shù)據(jù)庫教程:MySQL登錄和退出服務器
分享網(wǎng)址:http://www.5511xx.com/article/djjdcdc.html


咨詢
建站咨詢
