Search code examples
mysqldatabaserenamerootsuperuser

How to change root username in MySQL


I'm running MySQL in Ubuntu, default installation.

How can I change the username from root to another one, let's say admin? Preferably from the command line.


Solution

  • After connecting to MySQL run

    use mysql;
    update user set user='admin' where user='root';
    flush privileges;
    

    That's it.

    If you also want to change password, in MySQL < 5.7, run

    update user set password=PASSWORD('new password') where user='admin';
    

    before flush privileges;. In MySQL >= 5.7, the password field in the user table was renamed to authentication_string, so the above line becomes:

    update user set authentication_string=PASSWORD('new password') where user='admin';