GRANT <role>

Assigns a previously created role to an existing user. The user can use then use the statement SET ROLE <rolename> to assume the privileges of the role, or SET ROLE ALL to assume all roles that have been assigned.

Synopsis

GrantRoleStmt

GRANT <role> - 图1

RolenameList

GRANT <role> - 图2

UsernameList

GRANT <role> - 图3

  1. GrantRoleStmt ::=
  2. 'GRANT' RolenameList 'TO' UsernameList
  3. RolenameList ::=
  4. Rolename ( ',' Rolename )*
  5. UsernameList ::=
  6. Username ( ',' Username )*

Examples

Connect to TiDB as the root user:

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

Create a new role analyticsteam and a new user 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)

Connect to TiDB as the jennifer user:

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

Note that by default jennifer needs to execute SET ROLE analyticsteam in order to be able to use the privileges associated with the analyticsteam role:

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

Connect to TiDB as the root user:

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

The statement SET DEFAULT ROLE can be used to associate the role analyticsteam to jennifer:

  1. SET DEFAULT ROLE analyticsteam TO jennifer;
  2. Query OK, 0 rows affected (0.02 sec)

Connect to TiDB as the jennifer user:

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

After this, the user jennifer has the privileges associated with the role analyticsteam and jennifer does not have to execute the statement SET ROLE:

  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. +----------------+
  16. 1 row in set (0.00 sec)

MySQL compatibility

The GRANT <role> statement in TiDB is fully compatible with the roles feature in MySQL 8.0. If you find any compatibility differences, report a bug.

See also