Adrian Ber
Adrian Ber

Reputation: 21370

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.

Upvotes: 16

Views: 35770

Answers (3)

For example, you can change the username root to john as shown below. *I recommend to use RENAME USER ... because directly modifying mysql.user is sometimes problematic. *You probably need to log in with the user root:

RENAME USER 'root'@'localhost' to 'john'@'localhost';

Or:

UPDATE mysql.user SET User='john' WHERE User='root';
FLUSH PRIVILEGES;

Then, you can log in to MySQL:

mysql -u john -p

Upvotes: -1

Adrian Ber
Adrian Ber

Reputation: 21370

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';

Upvotes: 42

electr0sheep
electr0sheep

Reputation: 67

I just wanted to say that for me, there was no column 'password'.

To change password, the correct field was authentication_string

So the command is

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

I'm no MySQL expert, so I'm not sure exactly why, but what I said is correct, at least in my case.

Upvotes: 1

Related Questions