DROP USER

DROP USER 语句用于从 TiDB 系统数据库中删除用户。如果用户不存在,使用关键词 IF EXISTS 可避免出现警告。执行 DROP USER 语句需要拥有 CREATE USER 权限。

语法图

DropUserStmt

DROP USER - 图1

Username

DROP USER - 图2

  1. DropUserStmt ::=
  2. 'DROP' 'USER' ( 'IF' 'EXISTS' )? UsernameList
  3. Username ::=
  4. StringName ('@' StringName | singleAtIdentifier)? | 'CURRENT_USER' OptionalBraces

示例

  1. DROP USER 'idontexist';
  1. ERROR 1396 (HY000): Operation DROP USER failed for idontexist@%
  1. DROP USER IF EXISTS 'idontexist';
  1. Query OK, 0 rows affected (0.01 sec)
  1. CREATE USER 'newuser' IDENTIFIED BY 'mypassword';
  1. Query OK, 1 row affected (0.02 sec)
  1. GRANT ALL ON test.* TO 'newuser';
  1. Query OK, 0 rows affected (0.03 sec)
  1. SHOW GRANTS FOR 'newuser';
  1. +-------------------------------------------------+
  2. | Grants for newuser@% |
  3. +-------------------------------------------------+
  4. | GRANT USAGE ON *.* TO 'newuser'@'%' |
  5. | GRANT ALL PRIVILEGES ON test.* TO 'newuser'@'%' |
  6. +-------------------------------------------------+
  7. 2 rows in set (0.00 sec)
  1. REVOKE ALL ON test.* FROM 'newuser';
  1. Query OK, 0 rows affected (0.03 sec)
  1. SHOW GRANTS FOR 'newuser';
  1. +-------------------------------------+
  2. | Grants for newuser@% |
  3. +-------------------------------------+
  4. | GRANT USAGE ON *.* TO 'newuser'@'%' |
  5. +-------------------------------------+
  6. 1 row in set (0.00 sec)
  1. DROP USER 'newuser';
  1. Query OK, 0 rows affected (0.14 sec)
  1. SHOW GRANTS FOR 'newuser';
  1. ERROR 1141 (42000): There is no such grant defined for user 'newuser' on host '%'

MySQL 兼容性

  • 在 TiDB 中删除不存在的用户时,使用 IF EXISTS 可避免出现警告。Issue #10196

另请参阅