SET DEFAULT ROLE

This statement sets a specific role to be applied to a user by default. Thus, they will automatically have the permissions associated with a role without having to execute SET ROLE <rolename> or SET ROLE ALL.

Synopsis

SetDefaultRoleStmt

SET DEFAULT ROLE - 图1

  1. SetDefaultRoleStmt ::=
  2. "SET" "DEFAULT" "ROLE" ( "NONE" | "ALL" | Rolename ("," Rolename)* ) "TO" 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)

SET DEFAULT ROLE will not automatically GRANT the associated role to the user. Attempting to SET DEFAULT ROLE for a role that jennifer does not have granted results in the following error:

  1. SET DEFAULT ROLE analyticsteam TO jennifer;
  2. ERROR 3530 (HY000): `analyticsteam`@`%` is is not granted to jennifer@%

MySQL compatibility

The SET DEFAULT 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