GRANT <role>

    GrantRoleStmt

    RolenameList

    GRANT <role> - 图2

    root 用户连接 TiDB:

    1. mysql -h 127.0.0.1 -P 4000 -u root

    创建新角色 analyticsteam 和新用户 jennifer

    1. CREATE ROLE analyticsteam;
    2. Query OK, 0 rows affected (0.02 sec)
    3. GRANT SELECT ON test.* TO analyticsteam;
    4. Query OK, 0 rows affected (0.02 sec)
    5. CREATE USER jennifer;
    6. Query OK, 0 rows affected (0.01 sec)
    7. GRANT analyticsteam TO jennifer;
    8. Query OK, 0 rows affected (0.01 sec)

    jennifer 用户连接 TiDB:

    1. SHOW GRANTS;
    2. +---------------------------------------------+
    3. | Grants for User |
    4. | GRANT USAGE ON *.* TO 'jennifer'@'%' |
    5. | GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
    6. +---------------------------------------------+
    7. SHOW TABLES in test;
    8. ERROR 1044 (42000): Access denied for user 'jennifer'@'%' to database 'test'
    9. SET ROLE analyticsteam;
    10. Query OK, 0 rows affected (0.00 sec)
    11. SHOW GRANTS;
    12. +---------------------------------------------+
    13. | Grants for User |
    14. +---------------------------------------------+
    15. | GRANT USAGE ON *.* TO 'jennifer'@'%' |
    16. | GRANT SELECT ON test.* TO 'jennifer'@'%' |
    17. | GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
    18. +---------------------------------------------+
    19. 3 rows in set (0.00 sec)
    20. SHOW TABLES IN test;
    21. +----------------+
    22. | Tables_in_test |
    23. +----------------+
    24. | t1 |
    25. +----------------+

    root 用户连接 TiDB:

      执行 SET DEFAULT ROLE 语句将用户 jenniferanalyticsteam 角色相关联:

      jennifer 用户连接 TiDB:

      1. mysql -h 127.0.0.1 -P 4000 -u jennifer

      此时 jennifer 用户无需执行 SET ROLE 语句就能拥有 analyticsteam 角色相关联的权限:

      1. SHOW GRANTS;
      2. +---------------------------------------------+
      3. | Grants for User |
      4. +---------------------------------------------+
      5. | GRANT USAGE ON *.* TO 'jennifer'@'%' |
      6. | GRANT SELECT ON test.* TO 'jennifer'@'%' |
      7. | GRANT 'analyticsteam'@'%' TO 'jennifer'@'%' |
      8. +---------------------------------------------+
      9. 3 rows in set (0.00 sec)
      10. SHOW TABLES IN test;
      11. +----------------+
      12. | Tables_in_test |
      13. +----------------+
      14. | t1 |
      15. +----------------+