MySQL: Reset Lost Root Password
Here is a quick Step-by-Step “how to” which helps restoring MySQL root password that was lost/forgotten.
It happens to everybody, especially if several distributed (different) systems are maintained, where the password is not the same. Here is what needs to be done to restore it:
Step 1: Stop MySQL daemon if it is currently running
Depending on the operating system MySQL is installed on, the daemon can be checked/stopped differently. Here is an example on how to do it in Unix-like systems.
NOTE: You might need to run it as a Unix System superuser (root) - depending on how the system is configured,
and what permissions your Unix account is granted)Here is how to stop/kill the existing mysql daemon, in case it is running:
ps -ef | grep mysql - checks if mysql/mysqld is one of the running processes. pkill mysqld - kills the daemon, if it is running.
Note: if pkill (’process kill’) is not on a particular Unix system, use kill -9 ‘pid’, where ‘pid’ corresponds to processes that were found with ps -ef | grep mysql
Step 2: Run MySQL safe daemon with skipping grant tables
mysqld_safe --skip-grant-tables &Step 3: Login to MySQL as root with no password
mysql -u root mysqlStep 4: Run UPDATE query to reset the root password
In MySQL command line prompt issue the following two commands:
UPDATE user SET password=PASSWORD("ualue=42") WHERE user="root"; FLUSH PRIVILEGES;
“ualue=42” is a common password for “The Hitchhiker’s Guide to the Galaxy” people which reads “Ultimate Answer to Life, the Universe, and Everything=42“
Step 5: Stop MySQL safe daemon
Follow the first two steps, but this time kill (pkill) “mysqld_safe” instead of “mysqld”
Step 6: Start MySQL daemon
Depending on the operating system (Unix-like examples):
/etc/rc.d/rc.mysql start
OR
/etc/init.d/mysql start
OR
/etc/rc.5/mysql start
etc.. check existing MySQL configuration
Step 7: Root password is reset and ready to use
Password is reset. Privileges are flushed. Start MySQL and login as root with the password set in step 4:
mysql -u root -p mysql
Note: sometimes (most of the time) ‘root user’ privileges are required for the system (OS) in order to stop/start processes
what is next? Reset Lost Password in Sun Application Server…
howto mysql tutorialsYou can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
September 15th, 2007 at 3:03:32
Dude, you rock. I’m a total n00b with database stuffs … and hunting out this blog (?) on dial-up was so worth it for such a straight forward solution. Thank you …
February 18th, 2008 at 8:19:12
Thanksfor the help but I still hate mysql
August 11th, 2008 at 14:02:46
Nice script, I think I should try it. Thanks
September 15th, 2008 at 16:07:47
[…] forget and loose passwords at least once every so often. Some time ago I wrote a tutorial on how to reset lost root password in mysql, and here is another similar tutorial on how to reset the lost domain password but this time for […]
October 2nd, 2008 at 13:13:57
I can’t thank you enough for your guide. I am truly grateful.
October 25th, 2008 at 15:11:18
Thanks! Works fine for me!!!
October 26th, 2008 at 11:50:25
welcome
November 10th, 2008 at 3:41:05
I get the same error 1045 access denied for user ‘root’@'localhost’(using password: YES) but I am using Windows Vista and have forgotten my password
November 10th, 2008 at 10:30:04
@jeffrey,
What step did you follow on Windows? I suspect that you did not stop/start the right mysql processes. Also did you find how to run “mysqld_safe”?
– Toly
December 7th, 2008 at 7:06:31
Thanks a lot!
January 20th, 2009 at 15:16:13
Don’t use kill -9 with mysql! It shutdowns uncleanly. It’s very bad for a database! Use /etc/init.d/mysql stop
http://speculation.org/garrick/kill-9.html
January 24th, 2009 at 10:53:52
Sorry. I am a total zilch in mysql.
Question: in step 7, with this command ( mysql -u root -p mysql) , are you logging in as root with a password of mysql. As set in step 4? I thought the password set in Step 4 was: ‘value=42′.
Should I backup everything first before I do the above.
January 24th, 2009 at 17:10:57
@Max,
It can be confusing if you read it literally, but what really happens is you are telling mysql to log you in with the root user “-u root”, who will provide the password “-p”, AND choose a db named “mysql” for this user.
Check out “Step 3″, see there is “mysql -u root mysql”? That is exactly the same statement as in “Step 7″ except in “Step 3″ you are not saying “-p” —> “I am going to provide the password for this user”.
– Toly
February 19th, 2009 at 9:35:33
[…] http://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/ […]
March 24th, 2009 at 9:47:28
None of those methods worked on 5.0.32-Debian_7etch5. I tried init file method, tried skipping grant tables and various combination of user - no luck to reset the password.
March 24th, 2009 at 17:55:10
@auriux,
Make sure before you run
all myslq (mysqld, etc..) processes are killed (not running). You can check if that is the case by
Only when none of the mysql processes are running you can start mysqld_safe.
Let me know if you have any difficulties, and if you do, at what step?
Good Luck,
– Toly
April 22nd, 2009 at 14:28:40
I’ve tried this (because it seems a root password is installed by default on my system). I can login ok thanks to the skip-grant-tables option, but i am not able to login again after I restart the system.
This is on Dragonfly BSD.
April 23rd, 2009 at 2:52:38
@colin,
It might be that there is a startup script somewhere that resets a myslq server password in case it is empty (wild guess).
Try to actually SET the password, instead of filling it blank. Let me know if it works for you,
– Toly
June 5th, 2009 at 4:56:09
Thx for sharing Toly!