SET DEFAULT ROLE
SetDefaultRoleStmt:
SetDefaultRoleOpt:
RolenameList:
UsernameList:
以 root
用户连接 TiDB:
创建新角色 analyticsteam
和新用户 jennifer
:
CREATE ROLE analyticsteam;
Query OK, 0 rows affected (0.02 sec)
GRANT SELECT ON test.* TO analyticsteam;
Query OK, 0 rows affected (0.02 sec)
CREATE USER jennifer;
Query OK, 0 rows affected (0.01 sec)
GRANT analyticsteam TO jennifer;
以 用户连接 TiDB:
mysql -h 127.0.0.1 -P 4000 -u jennifer
以 root
用户连接 TiDB:
mysql -h 127.0.0.1 -P 4000 -u root
执行 SET DEFAULT ROLE
语句将用户 jennifer
与 analyticsteam
角色相关联:
SET DEFAULT ROLE analyticsteam TO jennifer;
Query OK, 0 rows affected (0.02 sec)
以 jennifer
用户连接 TiDB:
此时 jennifer
用户无需执行 SET ROLE
语句就能拥有 analyticsteam
角色相关联的权限:
SHOW GRANTS;
+---------------------------------------------+
| Grants for User |
| GRANT USAGE ON *.* TO 'jennifer'@'%' |
| GRANT Select ON test.* TO 'jennifer'@'%' |
| GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
+---------------------------------------------+
3 rows in set (0.00 sec)
+----------------+
| Tables_in_test |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
SET DEFAULT ROLE
语句不会自动将相关角色授予 (GRANT
) 用户。若尝试为 jennifer
尚未被授予的角色执行 SET DEFAULT ROLE
语句会导致以下错误:
SET DEFAULT ROLE analyticsteam TO jennifer;