一、DCL
DCL英文全称是Data Control Language(数据控制语言),用来管理数据库用户、控制数据库的访
问权限。
二、管理用户
1.查询与创建用户
代码如下(示例):
-- DCL 管理用户
-- 1.查询用户
use mysql;
select *from user;
-- 2.创建用户
-- create user '用户名'@'主机名' identified by '密码';
-- 创建用户itcast 只能在当前主机localhost访问,密码123456
create user 'itcast'@'localhost' identified by '123456';
2. 创建用户任意主机访问%
-- 创建用户heima 可以在任意主机访问该数据库 密码123456
create user 'heima'@'%' identified by '123456';
3.修改用户密码
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';
4.删除用户密码
drop user '用户名'@'主机名';
三、SQL语句
-- DCL 管理用户
-- 1.查询用户
use mysql;
select *from user;
-- 2.创建用户
-- create user '用户名'@'主机名' identified by '密码';
-- 创建用户itcast 只能在当前主机localhost访问,密码123456
create user 'itcast'@'localhost' identified by '123456';
-- 创建用户heima 可以在任意主机访问该数据库 密码123456
create user 'heima'@'%' identified by '123456';
-- 3.修改用户heima的访问密码1234
alter user 'heima'@'%' identified with mysql_native_password by '1234';
-- 4.删除itcast@localhost用户
drop user 'itcast'@'localhost';
四、权限管理
MySQL中定义了很多种权限,但是常用的就以下几种:
1.查询权限
2.授予权限
在授予权限之前,查看heima能看到的数据库只有information_schema
执行完SQL语句可以查看SQL具备的权限
这个时候在cmd也可以看到heima这个用户所有的数据库多了一个itcast权限数据库。
3.撤销权限
五、SQL语句
-- DCL 权限控制
-- 1.查询权限
show grants for 'heima'@'%';
-- 2.授予权限
grant all on itcast.*to 'heima'@'%';
-- 3.撤销权限
SHOW GRANTS FOR 'heima'@'%';
revoke all on itcast.*from 'heima'@'%';