DCL介绍: DCL语言 是用来管理数据库用户,控制权限数据库的访问权限;
· 控制哪个用户可以访问
· 控制用户有哪些权限
MySQL用户的信息,用户权限信息都是存放在【系统数据库 mysql 】,【user表】中的;
① DCL 管理用户
User表
-- 查询用户 -- 选择系统数据库; use mysql; select * from user;
通过Host 字段 主机地址与 User字段 用户名,可以确认唯一用户 【 ‘用户名’@‘主机名’ 】;
Host 字段表示主机地址,如果为localhost 表示当前用户只能在本机上访问数据库;
-- 创建用户 create user '用户名'@'主机名' identified by '密码';
-- 创建一个【只能够】在本机使用的用户
create user 'testRoot'@'localhost' identified by '123456';
-- 创建一个用户【能够在任意主机进行访问】使用【百分号 %】
create user 'testUser'@'%' identified by '123456';
-- 修改用户密码 alter user '用户名'@'主机名' identified with mysql_native_password by '新密码';
alter user 'testUser'@'%' identified with mysql_native_password by '1234';
-- 删除用户 drop user '用户名'@'主机名';
drop user 'testUser'@'%';
② 权限控制
Mysql中定义了许多权限,常用的为一下几种:
ALL,ALL PRIVIEGES 表示所有权限;
· select 查询数据;
· insert 插入数据;
· update 修改数据;
· delete 删除数据;
· alter 修改表;
· drop 删除数据库/表/视图
· 创建数据库/表
-- 查询权限 show grants for '用户名'@'主机名'
-- GRANT USAGE ON *.* TO 'testRoot'@'localhost' 表示只能够连接上数据库
show grants for 'testRoot'@'localhost';
-- 【指定用户】授予权限【指定数据库,指定表】 grant 权限列表 on 数据库名.表名 to '用户名'@'主机名'
-- GRANT ALL PRIVILEGES ON `mysql_test`.`test_user_new` TO 'testRoot'@'localhost'
-- 授予所有权限在`mysql_test`.`test_user_new`表中
grant all on mysql_test.test_user_new to 'testRoot'@'localhost';
grant select,update on mysql_test.test_user_new to 'testRoot'@'localhost';
-- 如果是所有表 *.*
-- GRANT ALL PRIVILEGES ON *.* TO 'testRoot'@'localhost'
-- 表示授予所有权限在所有库中所有表
grant all on *.* to 'testRoot'@'localhost';
-- 撤销权限 revoke 权限列表 on 数据库名.表名 from '用户名'@'主机名'
revoke all on *.* from 'testRoot'@'localhost';
【注意】
· 多个权限之间,使用逗号分隔
· 授权时,数据库名与表名 都可以使用 * 进行统配,代表所有;