DROP ROLE

This statement removes a role, that was previously created with CREATE ROLE.

Synopsis

DropRoleStmt

DROP ROLE - 图1

RolenameList

DROP ROLE - 图2

  1. DropRoleStmt ::=
  2. 'DROP' 'ROLE' ( 'IF' 'EXISTS' )? RolenameList
  3. RolenameList ::=
  4. Rolename ( ',' Rolename )*

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)

Connect to TiDB as the root user:

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

Drop the role for the analyticsteam:

  1. DROP ROLE analyticsteam;
  2. Query OK, 0 rows affected (0.02 sec)

jennifer no longer has the default role of analyticsteam associated, nor can set the role to analyticsteam.

Connect to TiDB as the jennifer user:

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

Show the privileges of jennifer:

  1. SHOW GRANTS;
  2. +--------------------------------------+
  3. | Grants for User |
  4. +--------------------------------------+
  5. | GRANT USAGE ON *.* TO 'jennifer'@'%' |
  6. +--------------------------------------+
  7. 1 row in set (0.00 sec)
  8. SET ROLE analyticsteam;
  9. ERROR 3530 (HY000): `analyticsteam`@`%` is is not granted to jennifer@%

MySQL compatibility

The DROP 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