Recover MySQL root Password

by Vivek Gite · 127 comments

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 mysql root account password.

Step # 5: Exit and restart the 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

Featured Articles:

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!

{ 127 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 Vivek Gite 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

85 Mikheil Kalandadze 07.17.09 at 3:52 pm

THANK YOU.

Help is easy and usable ;) GREAT

86 dman3d 07.23.09 at 1:04 am

Thanks a lot pal,
your method rocks, save me a lot of time.

87 Senthil Kumar 07.30.09 at 6:00 pm

I have tried your solution 5 times. But not working :((.
[root@u15345903 /]# /etc/init.d/mysqld stop
Stopping MySQL: [ OK ]
[root@u15345903 /]# mysqld_safe –skip-grant-table &
[1] 2278
[root@u15345903 /]# Starting mysqld daemon with databases from /var/lib/mysql
mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.83 Source distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=’blocialdb123′ where user =’root’;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[root@u15345903 /]# /etc/init.d/mysqld stop
STOPPING server from pid file /var/run/mysqld/mysqld.pid
090730 13:58:21 mysqld ended

Stopping MySQL: [ OK ]
[1]+ Done mysqld_safe –skip-grant-table
[root@u15345903 /]# /etc/init.d/mysqld start
Starting MySQL: [ OK ]
[root@u15345903 /]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES)
[root@u15345903 /]#

88 Martin 08.06.09 at 11:45 am

i want to recover my password in windows.what should i do

89 frick 08.12.09 at 2:17 am

The new MYSQL has different table name for user. See http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html for details.

90 vorbis 08.13.09 at 4:20 pm

Thanks! Worked perfectly!

91 Jhon 08.16.09 at 9:48 pm

i search for a couple hours Thank you

92 LowKey 08.20.09 at 6:16 am

Nice! Very helpful , Thanks!

93 atrin 08.30.09 at 8:34 am

hi , at first i want to thank you for your post its very useful for me
actually i do all of above but when i want to access mysql with root (#mysql -u root -p)
it said that ” ERROR 1045 (28000): Access denied for user ‘root’@'localhost’ (using password: YES) ”

could you please help me about this

94 newbie 09.06.09 at 5:59 am

Awesome, I have been playing using MySQL on and off for almost 2years to understand it from a higher level and still getting use to it! This is the best straight forward instructions I’ve seen….and trust me I’ve looked at heaps :)

So from a newbie, THANKS for taking the time!
John B.

95 gajula vinod kumar 09.10.09 at 10:23 am

Great job
Without this i had reinstall and configure my bugzilla server
Thanks a lot

96 Michael Irwin 09.13.09 at 8:21 pm

Definitely the most valuable tip of the year so far! Well written, clearly written.

Thank you very much!

Mike I.

97 Carl 09.17.09 at 3:17 am

Thanks. Very clear and well written, extremely helpful.

98 Ashish kumar 09.17.09 at 6:21 am

thanx
this is gr8 job
so that i recover my mysql password

99 Amy 09.18.09 at 7:41 pm

How do i log in as root user if I do not know the password? Also, when I use the # /etc/init.d/mysql stop, it tells me that ‘#’ is not recognized as an internal or external command. I am not very knowledgeable about these things. I am on a windows system and I don’t know the root password. Please help. Thanks.

100 eric lee 09.28.09 at 10:42 pm

Cool man !! you’re cool!

101 Alfonso 09.30.09 at 9:56 pm

Hey… Thanks

this works perfect..

Funciona perfectamente…

102 Shekhar Charodiya 10.02.09 at 7:33 am

Thanks Buddy U make my day

Thanks again

can u guide me how i make user in mysql and give them right to use

If u have solution Guide me

103 Vivek Gite 10.02.09 at 9:30 am

@ Shekhar, try the following url:

http://www.cyberciti.biz/faq/mysql-user-creation/

104 Alejandro 10.15.09 at 3:10 am

Thaaaaaanks!!! finally could set my password with your help, was trying for days.. !!!

105 Tuvshuu 10.26.09 at 2:36 pm

Hi guys. This help looks soo helpfull but i dont know how to use it. i really need to reset my password. but i dont know how to Start the MySQL (mysqld) server/daemon process with the –skip-grant-tables option. I went to mysql\bin\mysqld but i cant type or do anything! can someone help me how to do those steps?

106 niraj tiwari 10.28.09 at 2:40 am

i created my joomla with it set up and creeat user root and no password now my user and password is not working

107 niraj tiwari 10.28.09 at 2:42 am

i miss my user and password

108 thank 11.13.09 at 7:07 pm

helped me with my problem too.

googled around but your solution was the leanest

109 Samir Dhok 11.15.09 at 2:37 pm

This works perfect.
Thanks

110 prajw2 11.15.09 at 3:17 pm

it works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!thankz a lot

111 irelock 11.19.09 at 4:43 pm

MY a$$ was saved. Id almost thank you in person. Thanks a million and 1.

112 Girish 11.24.09 at 5:16 am

yess…… its working………Thanks a lot………

113 Paul Mwiu 11.26.09 at 10:07 am

Thanks for the above information. It has really helped alot.
Cheers!!

114 vibhav 12.01.09 at 12:11 pm

awsem…..brilliant….
thanx a lot…..

115 Vipul 12.09.09 at 12:01 pm

Thanks to help me but I have not root user in user table of mysql database.
Can you please tell me why such kind of things happened?

116 Olle 12.10.09 at 1:12 pm

Yep, thanks!

117 danidiaz 01.05.10 at 4:33 pm

Excellent tutorial!!

Thank you very much!

118 Jayaprakash 01.09.10 at 1:10 am

This was absolutely what they say, hitting the nail on the head. I was getting the error message and was hitting my hed on the wall when this gentleman came along and it worked like smooth butter.
Thanx man , you rock

119 Roque 01.22.10 at 7:10 pm

Thank’s a lot ..!
Very simple way

120 Heliogabal 02.02.10 at 12:28 am

thank You so much – this was just what I needed.

121 neilh 02.15.10 at 10:02 pm

Am still trying to find a way to SET the root password. WAMP mysql 3.1.16. Win Vista 32 – currently no password required.

Nothing on the ‘MySQL Change root Password’ thread works.

122 neilh 02.15.10 at 10:04 pm

That should of course have been MySql 5.1.36

123 MK 02.18.10 at 1:25 pm

Thanks a lot!

saved my a#$

124 Bala Subramanian 03.03.10 at 5:18 am

Thanks a lot man! Your post saved me rite on time!

125 neil 03.10.10 at 6:04 am

the most explicitly perfect tutorial yet. Saved My Ass Too

126 gary 03.17.10 at 10:01 pm

Awesome tip, tks, it was concise while providing enough information to be effective.
Tks.

127 pedro 03.19.10 at 6:23 pm

Thanks dude. Saved me some problems :)

Btw, like was said previously, the commands will work in Windows as well.

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:

Next post: