Polls

Topics

Recover MySQL root password

Posted by Vivek on Tuesday April 18, 06 @4:18 pm

You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.

Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password

Step # 3: Connect to mysql server as the root user

Step # 4: Setup new root password

Step # 5: Exit and restart MySQL server

Here are commands you need to type for each step (login as the root user):

Step # 1 : Stop mysql service

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld.

Step # 2: Start to MySQL server w/o password:

# mysqld_safe --skip-grant-tables &
Output:

[1] 5988
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6025]: started

Step # 3: Connect to mysql server using mysql client:

# mysql -u root
Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.15-Debian_1-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

Step # 4: Setup new MySQL root user password

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step # 5: Stop MySQL Server:

# /etc/init.d/mysql stop
Output:

Stopping MySQL database server: mysqld
STOPPING server from pid file /var/run/mysqld/mysqld.pid
mysqld_safe[6186]: ended

[1]+  Done                    mysqld_safe --skip-grant-tables

Step # 6: Start MySQL server and test it

# /etc/init.d/mysql start
# mysql -u root -p

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or full RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in...

Discussion on This Article:

  1. Anonymous Says:

    THANKS!
    SAVED MY ASS!

  2. Anonymous Says:

    Thanks a lot!
    I’ve been googling for 2 hours and your solution was the only one that worked!

  3. Charles Says:

    thank you! i forgot my password and was afraid I had to reinstall mysql!. you saved me. props to you and msn search!

  4. Anonymous Says:

    On most *nix systems as root (or with sudo) you can also do:

    cat /root/.mysql_history|more

    Which is very informative.

  5. nixcraft Says:

    Nice :)

  6. Anonymous Says:

    Hi,

    When I try stop mysql on my server, I receive a error 1045. Access denied for user: ‘root@localhost’ (Using password: NO)

    Thanks

  7. Jm Says:

    HEllo,

    This one alo can help you for it.
    http://linux-tweaks.blogspot.com/2006/09/access-denied-for-user-rootlocalhost.html

  8. Timothy matara Says:

    This is great help, thanks for this posting!

  9. asia Says:

    i want free download of Recover MySQL root password

  10. Andrew Says:

    Brilliant guide - lifesaver!

  11. DngloZ Says:

    thanks for this great post

  12. Linda Botes Says:

    Thanks! I’ve been looking at numerous other solutions and none of them worked. This one worked, no hassles.

  13. Valentin Says:

    Great tutorial.. it just worked fine

  14. Gerard Says:

    Coolness!!! thanks for this post! already bookmarked!!!hehehe..

  15. Gerard Says:

    Coolness!!! This site is really the best! I also got a ftpbackup script from here! the best how to’s, I wish you the best and I hope that you could add more!!!

  16. amin Says:

    I got the following error

    ERROR! MySQL PID file could not be found!

    do have any solution

  17. Meef Says:

    Um… I’ve been trying to actually change the password for about 3 days now, and I tried this tutorial… No errors, but when I start the MySQL client and type in my password it just beeps and goes away :( Is there any other way?

  18. vivek Says:

    Open X terminal and at shell prompt use command:
    mysql -u root -p

    You should able to see an error message.

  19. murdock Says:

    Thanks for the help. This was just what I was looking for. I have no idea how I blew away my password, but at least this helped me get back in.

    Thanks!
    Murdock

  20. alex Says:

    THANK YOU!!!!!!!!!!!! THANK YOU!!!!!!!!!!!

  21. Sam Says:

    Dude, you rock my world. shotalot

  22. SPS Says:

    Yeah.. it is workin very well.. it saved me from formatting my Server… college 1……. :)..s o it works………..

  23. Sanford Says:

    Many thanks, this saved me a lot of time.

  24. Dave Brixius Says:

    Greetings. Thank you so much. We are just getting our server up and running and working through some mysql issues. You saved me a lot of time and headaches. Dave

  25. Amir Says:

    Thank you very much

  26. Charles Says:

    I love you all. This is the only solution that worked not even de.mysql’s solution worked…

  27. john Says:

    great, works exactly as shown. give the man a cigar!

  28. grateful Says:

    Vivek,

    does this affect the dbases / the rest of the users in any harmful way?

  29. Tony Says:

    another relieved joomla user here as Im now back in charge of mysql thanks a lot!

  30. Janus Says:

    Thank you.
    God bless.

  31. richard scrivener Says:

    You saved my ass!!! I can’t thank you enough!

  32. Nilesh Says:

    gr8 one! thanks.

  33. Victor Says:

    I am using a windows op, how do I recover the password under Windows

  34. Mark Says:

    I think I read this same article the last time my root acct got locked out. Thanks for hosting this page.

  35. gleb Says:

    I’ve done everything exactly as it says , but still canot connect with my mysql server. I allways have this error: ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES)
    Even when i do NOT use a password , it just not working! i tryed to reintall mysql through synaptic , but still the same shit… No difference. Can enyone give an advise?

  36. nisha Says:

    Hi,

    Can I remove an existing password following you guide, except that i leave this portion:

    mysql> update user set password=PASSWORD(”NEW-ROOT-PASSWORD”) where User=’root’;

    as this :

    mysql> update user set password=PASSWORD(”") where User=’root’;

    Would this work? I need to remove the password for a bit, for testing purposes…and then set the password again….any help/advise is much appreciated…

    -nisha-

  37. nisha Says:

    Hi again,

    The problem I mentioned above, well, I tested it,and it works….thank you for the perfect guide ;-)
    This is the i-can’t-remember-number-of-times that one of your articles has helped me… so, thanks for that too!

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

~ Last updated on: December 28, 2006

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.