文章目錄
- 1、連接命令
- 2、斷開連接
- 3、命令結束符
- 4、查看所有數據庫
- 5、切換到指定數據庫
- 6、查看當前使用的數據庫
- 7、查看庫中所有表
- 8、查看所有用戶
- 9、執行SQL腳本
- 10、查詢當前時間
1、連接命令
首先定位到MySQL安裝根目錄/bin目錄下,然后執行如下命令:
mysql -h[主機名] -P[端口] -u[用戶名] -p[密碼]
#例如:
mysql -hlocalhost -P3306 -uroot -p123
注:
-
localhost是指MySQL數據庫安裝在本機,如果是遠程機器,那么localhost換成對應機器的IP地址即可;
-
端口默認為3306的話,可以不輸入-P;
-
最好不要在一行中輸入密碼,這樣敲出去會被別人看到。
如果不想被看到密碼,可以使用如下方式連接:
mysql -hlocalhost -uroot -p
回車之后提示輸入密碼:
Enter password:
不過這回你輸入的密碼不會被顯示出來,心懷不軌的人也就看不到了,輸入完成點擊回車就成功連接到了服務器。
執行成功之后的界面如下:
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 24
Server version: 5.7.30 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.mysql>
最后一行的 mysql>
是一個客戶端的提示符,之后客戶端發送給服務器的命令都需要寫在這個提示符后邊。
注:如果你愿意,你可以多打開幾個命令行窗口,每個窗口都可以使用連接命令,從而達到運行多個客戶端程序的效果,每個客戶端程序都是互不影響的。
2、斷開連接
quit
exit
\q
注:如上三個命令是關閉客戶端程序的方式,不是關閉服務器程序的方式。
3、命令結束符
在書寫完一個命令之后需要以下邊這幾個符號之一結尾:
;
\g
\G
4、查看所有數據庫
show databases;
查詢結果:
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_cwtsb |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
5、切換到指定數據庫
use [數據庫名稱];
這里我們選擇 information_schema
use information_schema;
查詢結果:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
6、查看當前使用的數據庫
select database();
查詢結果:
+--------------------+
| database() |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
7、查看庫中所有表
show tables;
查詢結果:
+---------------------------------------+
| Tables_in_information_schema |
+---------------------------------------+
| CHARACTER_SETS |
| COLLATIONS |
...
| INNODB_SYS_TABLESTATS |
+---------------------------------------+
61 rows in set (0.00 sec)
8、查看所有用戶
select user,host,authentication_string FROM mysql.user;
查詢結果:
+---------------+-----------+-------------------------------------------+
| user | host | authentication_string |
+---------------+-----------+-------------------------------------------+
| root | localhost | *112646FC4B3886349C1C2A17DDFD146AD53C1B1 |
| mysql.session | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHER |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHER |
+---------------+-----------+-------------------------------------------+
3 rows in set (0.01 sec)
9、執行SQL腳本
source [腳本.sql文件]
10、查詢當前時間
select now();
查詢結果:
+---------------------+
| now() |
+---------------------+
| 2022-03-07 11:59:16 |
+---------------------+
1 row in set (0.00 sec)