文章目录
- 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)