13.7.1.7 RENAME USER Statement语句

RENAME USER old_user TO new_user
    [, old_user TO new_user] ...

The RENAME USER statement renames existing MySQL accounts. RENAME USER语句重命名现有的MySQL帐户。An error occurs for old accounts that do not exist or new accounts that already exist.对于不存在的旧帐户或已存在的新帐户,会发生错误。

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

As of MySQL 8.0.22, RENAME USER fails with an error if any account to be renamed is named as the DEFINER attribute for any stored object. 从MySQL 8.0.22开始,如果任何要重命名的帐户被命名为任何存储对象的DEFINER属性,RENAME USER将失败,并出现错误。(That is, the statement fails if renaming 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属性,请参阅孤立存储对象

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

RENAME USER 'jeffrey'@'localhost' TO 'jeff'@'127.0.0.1';

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

RENAME USER causes the privileges held by the old user to be those held by the new user. RENAME USER会使旧用户所拥有的权限变为新用户所拥有的权限。However, RENAME USER does not automatically drop or invalidate databases or objects within them that the old user created. 但是,RENAME USER不会自动删除或使旧用户创建的数据库或其中的对象无效。This includes stored programs or views for which the DEFINER attribute names the old 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节,“存储对象访问控制”。)

The privilege changes take effect as indicated in Section 6.2.13, “When Privilege Changes Take Effect”.特权变更按照第6.2.13节,“特权变更生效时”的规定生效。