SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

MySQL | RENAME USER

In MySQL, the RENAME USER statement allows you to rename an existing user account.

Syntax:

RENAME USER old_user TO new_user [, old_user TO new_user] ...;
  • old_user: The current name of the user, specified as 'username'@'hostname'.

  • new_user: The new name you want to give to the user, also specified as 'username'@'hostname'.

Example:

Let's say you have a user named olduser connecting from localhost, and you want to rename this user to newuser. The SQL command would be:

RENAME USER 'olduser'@'localhost' TO 'newuser'@'localhost';

Important Points:

  1. Privileges: To use the RENAME USER statement, you need the global CREATE USER privilege or the UPDATE privilege on the mysql database.

  2. Atomicity: The RENAME USER statement is atomic. This means if any errors occur during the rename process, no renames will occur at all.

  3. Connection Impact: Existing connections for the user being renamed are not affected. They will continue to exist under the old username until they are closed. However, any subsequent connections will need to use the new username.

  4. Privileges: When you rename a user, the user��s privileges remain unchanged. The rename operation only affects the username and/or hostname.

  5. Password: The password for the account remains unchanged after the rename operation.

It's always a good idea to back up your MySQL user table from the mysql database before making significant changes to user accounts, including renaming them. This allows you to revert to the previous state if something goes wrong.

  1. How to use RENAME USER in MySQL:

    • The RENAME USER statement in MySQL is used to change the username of an existing user account.
    RENAME USER 'old_username'@'host' TO 'new_username'@'host';
    
  2. Renaming a MySQL user account:

    • To rename a MySQL user account, use the RENAME USER statement with the old and new usernames.
    RENAME USER 'john_doe'@'localhost' TO 'jane_doe'@'localhost';
    
  3. Changing MySQL username with RENAME USER:

    • Use RENAME USER to change the username for an existing MySQL user.
    RENAME USER 'old_username'@'host' TO 'new_username'@'host';
    
  4. Updating user privileges after RENAME USER in MySQL:

    • After renaming a user, it's essential to update privileges for the new username.
    FLUSH PRIVILEGES;
    
  5. MySQL RENAME USER examples:

    • Examples showcasing the syntax and usage of the RENAME USER statement.
    RENAME USER 'old_user'@'localhost' TO 'new_user'@'localhost';
    
  6. RENAME USER with host specification in MySQL:

    • Specify the host when renaming a user in MySQL.
    RENAME USER 'old_user'@'localhost' TO 'new_user'@'%';
    
  7. Undoing a user rename with RENAME USER:

    • If a user is accidentally renamed, use RENAME USER again to revert the changes.
    RENAME USER 'new_user'@'%' TO 'old_user'@'localhost';
    
UPDATE mysql.global_priv SET user = 'new_user' WHERE user = 'old_user';
FLUSH PRIVILEGES;