课程名称:课程版本(101/201/301)+ 课程名称
学习时长:
20
课程收获:
Tidb的的用户和角色
课程内容:
用户账号
mysql -h localhost --port 4000 --user alice --password
mysql -h localhost -P 4000 -u bob -p
创建用户
- 两种方式
使用标准的create user和grant语句
使用DML语句更新权限表
CREATE USER 'finley'@'localhost' IDENTIFIED BY 'some_pass';
Copy
GRANT ALL PRIVILEGES ON *.* TO 'finley'@'localhost' WITH GRANT OPTION;
Copy
CREATE USER 'finley'@'%' IDENTIFIED BY 'some_pass';
Copy
GRANT ALL PRIVILEGES ON *.* TO 'finley'@'%' WITH GRANT OPTION;
Copy
CREATE USER 'admin'@'localhost' IDENTIFIED BY 'admin_pass';
Copy
GRANT RELOAD,PROCESS ON *.* TO 'admin'@'localhost';
Copy
CREATE USER 'dummy'@'localhost';
设置密码
TiDB 将密码存在 mysql.user
系统数据库里面。只有拥有 CREATE USER
权限,或者拥有 mysql
数据库权限( INSERT
权限用于创建, UPDATE
权限用于更新)的用户才能够设置或修改密码。
- 在
CREATE USER
创建用户时通过IDENTIFIED BY
指定密码:Copy
CREATE USER 'test'@'localhost' IDENTIFIED BY 'mypass';
- 为一个已存在的账户修改密码,可以通过
SET PASSWORD FOR
或者ALTER USER
语句完成:Copy
SET PASSWORD FOR 'root'@'%' = 'xxx';
或者:Copy
ALTER USER 'test'@'localhost' IDENTIFIED BY 'mypass';
删除用户
drop user ‘alice’@‘localhost’;
忘记密码
- 修改配置文件,在
security
部分添加skip-grant-table
:Copy
[security]
skip-grant-table = true
- 使用修改之后的配置启动 TiDB,然后使用
root
登录后修改密码:Copy
mysql -h 127.0.0.1 -P 4000 -u root
设置 skip-grant-table
之后,启动 TiDB 进程会增加操作系统用户检查,只有操作系统的 root
用户才能启动 TiDB 进程。
限制
账户的资源限制不支持
授权
grant all privileges on test.db1 to ‘alice’@’%’;
grant select on test.* to ‘alicce’@’%’;
grant update on . to ‘alice’@’%’;
grant all privileges on ‘te%’.* to ‘alice’@’%’;
回收权限
revoke update on test.db1 from ‘alice’@’%’;
revoke all privileges on test.* from ‘alice’@‘localhost’;
查看权限
- show grants for ‘root’@’%’;
- 或者查看系统表
select insert_priv from mysql.user where user=‘alice’ and host=’%’;
刷新权限
- 直接修改权限表后,需要刷新权限
flush privileges;
- 不常使用的命令不支持
file,usage,shutdown, execute,process, index … - 不支持列级权限
角色管理
create role ‘analyst’@’%’;
drop role ‘analyst’@’%’;
角色授权和权限回收
grant select on . to ‘analyst’@’%’;
grant all privileges on test.* to ‘analyst’@’%’;
revoke all privileges on test.* from ‘analyst’@’%’;
将角色授权给用户
grant ‘analyst’ to ‘alice’@‘localhost’;
revoke ‘analyst’,‘developer’ from ‘alice’@‘localhost’,‘bob’@‘localhost’;
查看用户和角色的权限
show grants for ‘alice’@‘localhost’;
show grants for ‘alice’@‘localhost’ using ‘app_developer’;
给用户设置默认角色
set default role ‘app_developer’,‘app_admin’ to ‘alice’@‘localhost’;
set default role all to ‘alice’@‘localhost’;
set default role none to ‘alice’@‘localhost’;
改变默认角色
set role ‘app_developer’,‘app_admin’;
set role default;
set role all;
set role none;
set role all except ‘app_admin’;
学习过程中遇到的问题或延伸思考:
- 问题 1:
- 问题 2:
- 延伸思考 1:
- 延伸思考 2: