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…
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 …
Thanksfor the help but I still hate mysql
Nice script, I think I should try it. Thanks
I can’t thank you enough for your guide. I am truly grateful.
Thanks! Works fine for me!!!
welcome
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
@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
Thanks a lot!
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
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.
@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
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.
@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
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.
@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
Great job! I was stuck without this hack!
I can login ok thanks to the skip-grant-tables option, but i am not able to login again after I restart the system. the database is hosted on windows
can anybody help with this problem it is really annoying now. i cannot remember the password. so I can login ok thanks to the skip-grant-tables option, but i am not able to login again after I restart the system.
Yes it is working for me …Thanks for your help
Excellent after 2 days of searching. Thanks Thanks
For ubuntu and debian users specifically who need to reset their MySQL root password, this sites steps worked flawlessly for me. Best of all, when I was finished, I only had to ‘restart’ the mysql server and I was able to login from another PC through a firewall for the first time ever to my server as the MySQL root without getting any error messages. Here is the link, decide for yourself! http://codesnippets.joyent.com/posts/show/2038
Worked a treat for mysql root password rest on Ubuntu 9.04 jaunty.
For Ubuntu. You will need to use the ‘kill -9 pid r’
Also bear in mind that ‘ps -ef |grep mysql’ will show the grep process in the results. n00bs like me will waste some time killing this, then ps’ing to check it’s gone, and finding it there again
It works. Thanks .
Everything else I found in internet was bogus, even official website of MySQL (http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html) did not help.
IT’s SIMPLE and IT WORKS.
Thank you very much
thanks …
Thank you very much for this. I somehow jacked up my db a few months ago and couldn’t get into it. I know nothing about databases, so I was pretty frustrated with MySql and phpMyAdmin until I came across a comment on HowtoForge directing me here. This definitely gets bookmarked.
This is very helpful,however am using Ubuntu and the query
UPDATE user SET password=PASSWORD(“value=42″) WHERE user=”root”;
did not work well….i hard to omit the word value and use it like:
UPDATE user SET password=PASSWORD(“42″) WHERE user=”root”;
Worked so smoothly! Thanks.
Thanks, I assumed reinstalling would reset root password, but it didn’t.
Thanks to your guide I was able to get going again!
WHEN YOU POST …the admins on this web see your IP
so they login on your mysql server whit password 42 …suckers
Don’t worry @H4king, I have no intentions to look up people IPs + “42″ would not be the password people would use.
However, thanks for the clever concern
Thanks a lot. it works, yippie!
i’m using window OS, and i’m really stuked so,
could you please provide steps for windows to solve the problem.
thanks in advance…
i’m using window OS, and i’m really stuked so,
could you please provide steps for windows to solve the problem.
thanks in advance…
Thank you very much. My installation did not include mysqld_safe or mysql-nt, but for some reason “mysqld –skip-grant-tables &” seems to have worked when other methods did not.
Great job! I was stuck!
Thank you so much, it helped a lot!
Great help. Works after I realized that the password is “ualue=42″ and not “value=42″ since I cut/pasted your instructions.
Thanks for the help. On Solaris 10 the system states that ” you are using safe mode and tried to update a table without a WHERE that uses a KEY colum… any ideas???
Last time I forgot my password and tried everything i could do but failed, until I found this great tool Password Genius. It works great, and you can google it.you can try to google it.
Man! nice help… very good link, i solved my problem after following yours steps.
keep it up man
Nice explanation dude, thanx a lot; it worked to reset the password. u are the best !
searching for a solution 2 hours now…. it was that simple after all
thanx bro
Thank you…A very good tutorial…
Thanks a ton……………
i really frustrated with this.
i searchedd near 100 pages but nothing work but thanks to yo man you make me happy!!!!!!!!!!!!!!!!
Your tutorials helped alot man ,
can u post a tutorials about changing passwords via ssh
i mean cpanel root passwords etc..
Worked great! Saved me. Thank you!
Hello, This saved my me as i forgot the root password and we got some big problems when our vpn database was down, Thank you.
Thank you and Keep it
thanks, i found number of articles in the same subject but yours is simple and direct to the point it helped me allot. thanks again.