文章目录
- 一、用户管理
- 1.1 查询用户
- 1.2 创建用户
- 1.3 修改用户密码
- 1.4 删除用户
- 二、权限控制
- 2.1 查询权限
- 2.2 授予权限
- 2.3 撤销权限
一、用户管理
1.1 查询用户
- 1) 找到mysql数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys | |
+--------------------+
9 rows in set (0.00 sec)
2)使用数据库
use mysql; select * from user;3)当前数据库所有的用户
mysql> select * from user;图形化界面看到更直观,当前只有root用户,其他是mysql数据库默认生成

1.2 创建用户
语法格式:
create user '用户名'@'主机名' identifiedby '密码';创建本地连接的用户admin
-- 创建用户admin,只能够在当前主机localhost访问,123456; mysql> create user 'admin'@'localhost' identified by '123456'; Query OK, 0 rows affected (0.01 sec)
创建远程连接的用户admin2
-- 创建远程用户admin2,可以在任意主机访问该数据库,密码123456; mysql> create user 'admin2'@'%' identified by '123456'; Query OK, 0 rows affected (0.00 sec)
1.3 修改用户密码
语法格式:
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';-- 修改用户admin的访问密码为654321; mysql> alter user 'admin'@'localhost' identified with mysql_native_password by '654321'; Query OK, 0 rows affected (0.00 sec)
1.4 删除用户
语法格式:
drop user '用户名'@'主机名';-- 删除admin用户 mysql> drop user 'admin'@'localhost'; Query OK, 0 rows affected (0.00 sec)
注意:主机名可以使用%通配,这类SQL开发人员操作的比较少,主要是DBA(Database Administrator 数据库管理员)使用。
二、权限控制
2.1 查询权限
语法格式:
show grants for '用户名'@'主机名';查询admin2用户的权限
-- admin2用户的权限目前是NO,没有任何权限 mysql> show grants for 'admin2'@'%'; +------------------------------------+ | Grants for admin2@% | +------------------------------------+ | GRANT USAGE ON *.* TO 'admin2'@'%' | +------------------------------------+ 1 row in set (0.00 sec)

2.2 授予权限
相关权限列表

语法格式:
grant 权限列表 on 数据库名.表名 to '用户名'@'主机名';-- 给admin2用户授予test数据库所有权限 mysql> grant all on test.* to 'admin2'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for 'admin2'@'%'; +--------------------------------------------------+ | Grants for admin2@% | +--------------------------------------------------+ | GRANT USAGE ON *.* TO 'admin2'@'%' | | GRANT ALL PRIVILEGES ON `test`.* TO 'admin2'@'%' | +--------------------------------------------------+ 2 rows in set (0.00 sec)
2.3 撤销权限
语法格式:
revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名';-- 撤销admin2用户test数据库所有权限 mysql> revoke all on test.* from 'admin2'@'%'; Query OK, 0 rows affected (0.00 sec) mysql> show grants for 'admin2'@'%'; +------------------------------------+ | Grants for admin2@% | +------------------------------------+ | GRANT USAGE ON *.* TO 'admin2'@'%' | +------------------------------------+ 1 row in set (0.00 sec)注意:多个权限之间,使用逗号分隔授权时,数据库名和表名可以使用*进行通配,代表所有。
