DCL英文全称是Data Control Language(数据控制语言),用来管理数据库用户、控制数据库的访问权限。
1.管理用户
查询用户
select * from user;
创建用户
create user '用户名'@'主机名' identified by '密码';
修改用户密码
alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';
删除用户
drop user '用户名'@'主机名';
注意
主机名可以使用%通配
这类sql开发人员操作的比较少,主要是数据库管理员使用。
例子:
//创建用户it, 只能够在当前主机localhost访问, 密码123456;
create user 'it'@'localhost' identified by '123456';
//创建用户haha, 可以在任意主机访问该数据库, 密码123456;
create user 'haha'@'%' identified by '123456';
//修改用户haha的访问密码为1234;
alter user 'haha'@'%' identified with mysql_native_password by '1234';
//删除 it@localhost 用户
drop user 'it'@'localhost';
2.控制权限
查看权限
show grants for '用户名'@'主机名';
授予权限
grant 权限列表 on 数据库.表名 to '用户名'@'主机名';
撤销权限
revoke 权限列表 on 数据库.表名 from '用户名'@'主机名';
注意事项
多个权限之间,使用逗号分隔
授权时,数据库名和表名可以使用 * 进行通配,代表所有。
例子
//查询 'haha'@'%' 用户的权限
show grants for 'haha'@'%';
//授予 'haha'@'%' 用户shop数据库所有表的所有操作权限
grant all on shop.* to 'haha'@'%';
//撤销 'haha'@'%' 用户的shop数据库的所有权限
revoke all on shop.* from 'haha'@'%';