Recover MySQL root password

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 read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 84 comments… read them below or add one }

1 Anonymous 04.29.06 at 1:16 pm

THANKS!
SAVED MY ASS!

2 Anonymous 05.04.06 at 10:11 am

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

3 Charles 05.19.06 at 8:34 am

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 05.20.06 at 2:33 am

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

cat /root/.mysql_history|more

Which is very informative.

5 nixcraft 05.20.06 at 2:58 am

Nice :)

6 Anonymous 10.20.06 at 4:15 pm

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 12.04.06 at 12:54 am
8 Timothy matara 01.12.07 at 6:28 am

This is great help, thanks for this posting!

9 asia 03.08.07 at 7:11 am

i want free download of Recover MySQL root password

10 Andrew 05.10.07 at 2:16 pm

Brilliant guide – lifesaver!

11 DngloZ 05.12.07 at 4:00 pm

thanks for this great post

12 Linda Botes 07.05.07 at 7:51 am

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

13 Valentin 07.23.07 at 10:34 pm

Great tutorial.. it just worked fine

14 Gerard 07.31.07 at 12:10 pm

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

15 Gerard 07.31.07 at 12:15 pm

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 08.17.07 at 8:15 am

I got the following error

ERROR! MySQL PID file could not be found!

do have any solution

17 Meef 08.30.07 at 5:44 pm

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 08.30.07 at 6:10 pm

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

You should able to see an error message.

19 murdock 09.05.07 at 3:05 am

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 09.27.07 at 3:03 pm

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

21 Sam 10.03.07 at 10:26 am

Dude, you rock my world. shotalot

22 SPS 10.14.07 at 3:09 pm

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

23 Sanford 11.30.07 at 1:49 am

Many thanks, this saved me a lot of time.

24 Dave Brixius 11.30.07 at 2:51 am

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 01.29.08 at 8:16 am

Thank you very much

26 Charles 02.05.08 at 6:47 am

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

27 john 02.06.08 at 5:30 pm

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

28 grateful 02.12.08 at 11:21 am

Vivek,

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

29 Tony 02.12.08 at 12:02 pm

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

30 Janus 03.09.08 at 9:46 am

Thank you.
God bless.

31 richard scrivener 03.12.08 at 4:38 am

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

32 Nilesh 03.20.08 at 12:03 pm

gr8 one! thanks.

33 Victor 04.22.08 at 1:10 pm

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

34 Mark 05.09.08 at 3:44 pm

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

35 gleb 05.11.08 at 1:10 pm

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 05.12.08 at 6:45 am

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 05.13.08 at 7:59 am

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!

38 Marty Wolf 05.16.08 at 6:07 pm

I tried all others, which failed!!!! This one works!!!! Thank you for making the instructions so simple to follow for us NON-LINUX Gurus. Much Thanks!

39 Jens 05.17.08 at 10:50 am

Thanks alot!! Also saved me some hours :)

40 Emmi 05.20.08 at 11:58 pm

Gleb, you need to use the unix shell as root to kill the mysql server process directly, then you can follow the mysql start without privileges command and get back in that way.

If you don’t know how to get a process list and send it a signal, you should read up just a little bit on that.

man ps
man kill

But something like:
ps aux | grep mysql
kill -TERM [processnumber# from command above]

(yes there is a one step command, but I personally ashew wildcards in my kill commands)

41 dario 06.10.08 at 6:31 pm

thank you very very much!

42 Feodor 06.12.08 at 10:21 am

thanks for this clear tutorial

43 sixhome 06.15.08 at 2:09 pm

Thanks! Many thanks!

44 FAISAL SAEED 06.24.08 at 9:53 am

Good work. Thanks a alot!

Faisal

45 uberdog 06.25.08 at 5:22 pm

BIG TNX!!!

46 Ochieng, Edmund 06.27.08 at 1:31 pm

After staying idle most of the day since i messed up the database, this is the best thing that happened on this day

47 Ko 07.03.08 at 9:08 am

THANKS!!!

Useful and to the point!

Ko.

48 Wesley Chapel Homes for Rent 07.11.08 at 5:36 pm

The gift that keeps on giving, 2 years and counting, this helped me out big time… Thanks!

49 Paolo 07.21.08 at 11:52 am

Fantastic! thanks a lot!

50 Robin86 07.23.08 at 4:13 pm

great! good job :)
this is really worth printable…

51 Fatiha 07.31.08 at 12:37 pm

Thank you, it works now.

52 jim 08.04.08 at 4:48 pm

thanks this worked perfectly!!

53 kailash 08.04.08 at 10:51 pm

Thanks a ton. I was stuck since a very long time. Could not find anywhere but here it was very clear.
Thanks very much!

54 NetWebLogic 08.15.08 at 11:10 am

Bookmarked for future reference! Thanks for the very straightforward explanation, can’t think of a better way to do it!

55 Steffen 08.16.08 at 10:07 am

Thank you very much! Saved some hours of reinstalling mysql.

56 Mohammad 08.25.08 at 3:10 pm

Thank you. I’ve been trying using Toad and Password recovery tools but to no avail. This got it done!

57 Matt 09.02.08 at 11:19 pm

Thank you very much. Windows users, you can do all of those commands from the command prompt, make sure you run it in admin mode, and change directory to where the mysql files are located, so when you run those commands it works. :)

58 me 09.24.08 at 6:25 am

Thank you , this is very helpful, as some one said it was the one excellent solution worked for me.

my day is better

59 akzidenz 09.26.08 at 7:02 pm

Thanks a lot!
Its much more elegant than the solution in the official mysql reference.

60 Richard J. Thomma 11.24.08 at 12:53 pm

It works — the very only one by far — Kubuntu 8.10
Description is crisp concise and foolproof

Thank You very much

I wish I knew howto tell google

61 Mike Runs 11.24.08 at 8:44 pm

Excellent!

Buy that person a beer! Or other meaningful beverage of choice.

Thanks!!

62 Didrik Nordström 12.18.08 at 6:05 pm

Thank you! Saved my ass too.

63 Tim 12.21.08 at 5:13 am

/etc/init.d/mysql stop
* Stopping MySQL database server mysqld [fail]

You ware going to need to fill in the gaps. If it fails then…….

64 More 01.05.09 at 5:32 pm

amazing… thanks a lot!!

65 ThouArtMyGod 01.08.09 at 6:51 pm

Thanks ever so much

66 Stephen Robinson 02.17.09 at 4:23 pm

thanx, wasted 2 hours googling and this method works,
Stephen :)

67 Skyline 02.19.09 at 2:11 pm

skip-grant-tables…. NICE!

68 Jhon F. Ortiz O. 02.20.09 at 4:33 pm

Hi!
Wonderful guide. Congratulations!
Bye!

69 megan 02.24.09 at 5:06 am

So Simple, yet so valuable. THANKS!

70 Bais 03.10.09 at 9:05 am

Very good, on ubuntu 9.04 mysqld 5.1 come installed with a mysterous password, I reseted it with this procedure and It works fine, thank you.

71 Levon 03.11.09 at 10:55 pm

Thank you very much, I’ve waisted more than 2 hours trying to recover MySql password.
Thanks again :)

72 ofir 03.20.09 at 2:59 pm

You saved me a lot of frustration, Thanks

73 dawg 04.11.09 at 8:10 am

omg, dude, you’re a genious….

74 Timmy 04.21.09 at 4:12 pm

Thank You..

75 carolija.eu 04.24.09 at 1:37 am

Nice, thanks !

76 Michael Watson 04.27.09 at 5:21 am

This worked really well. Thank you :)

77 newbie 05.12.09 at 2:05 pm

Thanks!

It works gr8!

78 Darrell 05.12.09 at 2:27 pm

THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU THANK YOU.

Most sincerely,
Darrell Goodman
Toronto, Canada

79 the_guv 05.17.09 at 8:50 am

you are a ruddy hero.

thank you.

80 Rafael 05.20.09 at 10:18 pm

Tutoriais como esse deveriam ficar na wikipedia dos tutoriais!

Abraço,

81 Stuart 05.26.09 at 2:42 am

This didn’t work for me. I now get the following when trying to log in: (I’m logged in as root):

ctru2:~ # mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)

What does this mean, and how do I get around it?

82 Jorge Calás 05.26.09 at 2:39 pm

Thanks!!!! It was really easy and worked like charm.

Server version: 5.0.51a-24 (Debian)

83 ChrisW 06.03.09 at 10:51 am

if you are using windows, you will still have the problem if you are using phpMyAdmin, open the config.inc.php, should be in your windows directory, change the password in there.

84 zabi 07.02.09 at 6:50 pm

hi i want to know that how to remove or recover password which is on file when we open it in mysql it is asking for password how to remove them plz any one tell

Leave a Comment

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

Previous post: The art of customer service

Next post: Linux display bandwidth usage on network interface by host