• Sales: 1-800-961-2888
  • Support: 1-800-961-4454

MySQL - Resetting a lost MySQL root password


The MySQL root password allows full access to the MySQL database and allows for all actions to be undertaken including creating new users, new databases, setting access rules and so on.

Losing one can be a difficult issue to encounter. Luckily, resetting the root password is easy as long as you have sudo access to the Server.

Contents

Not the Server root user

A common issue is confusing the Server root user with the MySQL root user.

The Server root user is the server's main user. The MySQL root user has complete control over MySQL only. The two 'root' users are not connected in any way.

Stop MySQL

The first thing to do is stop MySQL. If you are using Ubuntu or Debian the command is as follows:

sudo /etc/init.d/mysql stop

For CentOS, Fedora, and RHEL the command is:

sudo /etc/init.d/mysqld stop

Safe mode

Next we need to start MySQL in safe mode - that is to say, we will start MySQL but skip the user privileges table. Again, note that you will need to have sudo access for these commands so you don't need to worry about any user being able to reset the MySQL root password:

sudo mysqld_safe --skip-grant-tables &

Note: The ampersand (&) at the end of the command is required.

Login

All we need to do now is to log into MySQL and set the password.

mysql -u root

Note: No password is required at this stage as when we started MySQL we skipped the user privileges table.

Next, instruct MySQL which database to use:

use mysql;

Reset Password

Enter the new password for the root user as follows:

update user set password=PASSWORD("mynewpassword") where User='root';

and finally, flush the privileges:

flush privileges;

Restart

Now the password has been reset, we need to restart MySQL by logging out:

quit

and simply stopping and starting MySQL.

On Ubuntu and Debian:

sudo /etc/init.d/mysql stop
...
sudo /etc/init.d/mysql start

On CentOS and Fedora and RHEL:

sudo /etc/init.d/mysqld stop
...
sudo /etc/init.d/mysql start

Login

Test the new password by logging in:

mysql -u root -p

You will be prompted for your new password.



© 2011-2013 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License


See license specifics and DISCLAIMER

12 Comments

for a stock RHEL6 lamp cloud server, the start/stop commands are very slightly different:
instead of "sudo /etc/init.d/mysql stop" try "sudo /etc/init.d/mysqld stop"

Thanks Ric, I'll add a note about that to the article. One letter does make all the difference sometimes.

These worked great. As most people know who search for assistance over the web, we get some that work and some that don't so much. I used these with bitnami lamp stack on ubuntu 12.04 lts

great tutorial man,,, i searched for almost 10 sites but only urs provided the best steps.... thnx a ton..thnx thnx thnx.......

Thanks a lot for the information. Easy, efficient, and to the point. It is extremely helpful.

Thanks and I followed the path that you have given. The only issue I had was the server stops as soon as I started it in the safe. To prevent that issue I used the command /usr/bin/mysqld_safe --skip-kill-mysqld --skip-grant-table which kept the mysql server up and running till I gave the killall mysqld command.

Interesting. Thanks for sharing your experience and solution, Chamara. Not sure what caused the issue for you, but I'll look into it more closely sometime.

Thanks for an excellent tutorial, I was able to reset my mysql root password and now everything is working fine.

U can use the debian-sys-maint user to recover it. no need to start mysql in safe mode. more info at http://newexception.com/recover-mysql-root-password

I have been searching all over the web for simple instructions on how to reset my forgotten root password for MySQL windows installation. Would greatly appreciate any help from someone much more experienced. Cheers,

I can't say from personal experience, but the MySQL official documentation describes a means of resetting the root password on Windows here:

http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html


In my case (Centos 5.8), I had to create the row in the User table. Instead of

update user set password=PASSWORD("mynewpassword") where User='root';

I had to use

insert into user (user,password) values ('root',PASSWORD("telemetry"));

Add new comment