13.7.1.5 DROP USER Statement语句

DROP USER [IF EXISTS] user [, user] ...

The DROP USER statement removes one or more MySQL accounts and their privileges. DROP USER语句删除一个或多个MySQL帐户及其权限。It removes privilege rows for the account from all grant tables它会从所有授权表中删除帐户的特权行.

Roles named in the mandatory_roles system variable value cannot be dropped.无法删除在mandatory_roles系统变量值中命名的角色。

To use DROP USER, you must have the global CREATE USER privilege, or the DELETE privilege for the mysql system schema. 要使用DROP USER,您必须具有全局CREATE USER权限,或mysql系统架构的DELETE权限。When the read_only system variable is enabled, DROP USER additionally requires the CONNECTION_ADMIN privilege (or the deprecated SUPER privilege).启用read_only系统变量时,DROP USER还需要CONNECTION_ADMIN权限(或不推荐使用的SUPER权限)。

As of MySQL 8.0.22, DROP USER fails with an error if any account to be dropped is named as the DEFINER attribute for any stored object. 从MySQL 8.0.22开始,如果要删除的任何帐户被命名为任何存储对象的DEFINER属性,DROP USER将失败并出错。(That is, the statement fails if dropping an account would cause a stored object to become orphaned.) (也就是说,如果删除帐户会导致存储对象成为孤立对象,则该语句将失败。)To perform the operation anyway, you must have the SET_USER_ID privilege; in this case, the statement succeeds with a warning rather than failing with an error. 要执行该操作,您必须具有SET_USER_ID权限;在这种情况下,语句成功时会发出警告,而不是失败时出错。For additional information, including how to identify which objects name a given account as the DEFINER attribute, see Orphan Stored Objects.有关其他信息,包括如何识别哪些对象将给定帐户命名为DEFINER属性,请参阅孤立存储对象

DROP USER either succeeds for all named users or rolls back and has no effect if any error occurs. DROP USER为所有命名用户成功,或者回滚,如果发生任何错误,则不会产生任何效果。By default, an error occurs if you try to drop a user that does not exist. 默认情况下,如果试图删除不存在的用户,则会发生错误。If the IF EXISTS clause is given, the statement produces a warning for each named user that does not exist, rather than an error.如果给出了If EXISTS子句,该语句将为每个不存在的命名用户生成警告,而不是错误。

The statement is written to the binary log if it succeeds, but not if it fails; in that case, rollback occurs and no changes are made. 如果语句成功,则将其写入二进制日志,但如果失败,则不会写入;在这种情况下,会发生回滚,并且不会进行任何更改。A statement written to the binary log includes all named users. 写入二进制日志的语句包括所有命名用户。If the IF EXISTS clause is given, this includes even users that do not exist and were not dropped.如果给出了IF EXISTS子句,则甚至包括不存在且未被删除的用户。

Each account name uses the format described in Section 6.2.4, “Specifying Account Names”. 每个账户名称使用第6.2.4节,“指定账户名称”中描述的格式。For example:例如:

DROP USER 'jeffrey'@'localhost';

The host name part of the account name, if omitted, defaults to '%'.帐户名的主机名部分(如果省略)默认为'%'

Important重要

DROP USER does not automatically close any open user sessions. DROP USER不会自动关闭任何打开的用户会话。Rather, in the event that a user with an open session is dropped, the statement does not take effect until that user's session is closed. 相反,如果删除了一个打开会话的用户,则该语句在该用户的会话关闭之前不会生效。Once the session is closed, the user is dropped, and that user's next attempt to log in fails. This is by design.一旦会话关闭,该用户将被删除,该用户的下一次登录尝试将失败。这是故意的

DROP USER does not automatically drop or invalidate databases or objects within them that the old user created. DROP USER不会自动删除或使旧用户创建的数据库或其中的对象无效。This includes stored programs or views for which the DEFINER attribute names the dropped user. 这包括DEFINER属性为其命名已删除用户的存储程序或视图。Attempts to access such objects may produce an error if they execute in definer security context. 如果在definer安全上下文中执行,则尝试访问此类对象可能会产生错误。(For information about security context, see Section 25.6, “Stored Object Access Control”.)(有关安全上下文的信息,请参阅第25.6节,“存储对象访问控制”。)