MySQL tutorial: RENAME USER [EN]
top of page
CerebroSQL

MySQL: 

RENAME USER

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

The RENAME USER statement renames existing MySQL accounts. 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. When the read_only
system variable is enabled, RENAME USER additionally requires the
CONNECTION_ADMIN privilege (or the deprecated SUPER privilege).

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.
(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. For
additional information, including how to identify which objects name a
given account as the DEFINER attribute.

Each account name uses the format described in
https://dev.mysql.com/doc/refman/8.0/en/account-names.html. 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. However, RENAME USER does not automatically drop or
invalidate databases or objects within them that the old user created.
This includes stored programs or views for which the DEFINER attribute
names the old user. Attempts to access such objects may produce an
error if they execute in definer security context. (For information
about security context, see
https://dev.mysql.com/doc/refman/8.0/en/stored-objects-security.html.)

The privilege changes take effect as indicated in
https://dev.mysql.com/doc/refman/8.0/en/privilege-changes.html.

Example

bottom of page