MySQL Reset Root Password

by Vivek Gite · 13 comments

I forgot the MySQL root password. How do I reset MySQL root password?

It is possible to reset the root password. Use the following procedure for resetting the password for any MySQL root accounts on Unix / Linux / BSD like operating systems.

You need to login as root user under UNIX / Linux.

Step # 1: Stop MySQL

Type the following command to stop mysql server under RHEL / Fedora / CentOS Linux:
# /etc/init.d/mysqld stop
Use the following command if you are using FreeBSD:
# /usr/local/etc/rc.d/mysql-server stop
Use the following command if you are using Debian / Ubuntu Linux:
# /etc/init.d/mysql stop
Alternatively, you can stop the MySQL server by sending a kill to the mysqld process, using the path name of the .pid file in the following bash specific command:
# kill $(cat /var/run/mysqld/mysqld.pid)
OR
# pgrep -u mysql mysqld
# kill PID

Or, simply use killall command under Linux:
# killall mysql

Step # 2: Create new mysql password sql script

Create a sql file called /root/mysql.reset.sql as follows (replace YOUR-NEW-MYSQL-PASSWORD with actual new password you would like to use):
# vi /root/mysql.reset.sql
Append the following code:

UPDATE mysql.user SET Password=PASSWORD('YOUR-NEW-MYSQL-PASSWORD') WHERE User='root';
FLUSH PRIVILEGES;
 

Save and close the file.

Step # 3: Set new mysql root password

Type the following command:
# mysqld_safe --init-file=/root/mysql.reset.sql &
Sample output:

nohup: ignoring input and redirecting stderr to stdout
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[20970]: started

And you are done with resetting operation. Simply, stop server and start again:
# killall mysqld
# /etc/init.d/mysql start

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!

{ 13 comments… read them below or add one }

1 yoander (sedlav) 04.06.09 at 1:39 pm

In RHEL / Fedora / CentOS Linux yun can type also service mysqld stop

2 Raheem 04.06.09 at 4:35 pm

Thank you ! Nice piece of info.
In fact, my colleague did the most stupid thing of his life yesterday by deleting all the users in the mysql table :(
I did get back everything by simply altering the commands in the above given example to insert a user called root, then granting permission to all databases, reset its password etc.

3 Mihai Secasiu 04.06.09 at 4:58 pm

Or you could simply stop mysql then start it without privilege checking like this:
mysqld –skip-grant-tables
connect to it as root, run that update query, then kill the server and start it normally

4 Vivek Gite 04.06.09 at 5:04 pm

mysqld –skip-grant-tables method is considered as insecure and in some case it may corrupt the data.

HTH

5 dardo 04.06.09 at 8:15 pm

Excuse me for my bad English, I usually read it all time but rather speak or write it.

Thanks for share this method.

I install mySQL on my NSLU2 this weekend and with the Debian default install there are no root user, but the debconf ask it’s password twice at install time.

I use the skip-grant-tables method, but i was thinking on more secure ways to do it because withe the skip-grant-tables any user potentially can take the control of your databases.

6 micman 04.11.09 at 2:59 pm

thanks :)

7 GaQuay 04.16.09 at 12:00 am

for gui, you can use webmin, it so easy for change password mysql :)

8 Frank Wang 04.20.09 at 2:07 pm

Thanks, very nice instruction. A little typo at the end:
“# /etc/init.d/mysql start” should be “# /etc/init.d/mysqld start”

9 V.Balaviswanathan 04.21.09 at 9:30 am

Thanks a lot for the tips that you have provided and I would like to know the same for debian and Ubuntu systems as in Ubuntu there is no root login as such right?

10 Vivek Gite 04.21.09 at 12:44 pm

@V.Balaviswanathan,

MySQL has root user too. Do not confuse it with system root user (to become root user Ubuntu enter sudo -s).

@ Frank,
/etc/init.d/mysql or /etc/init.d/mysqld depends upon your distro.

11 V.Balaviswanathan 04.21.09 at 12:52 pm

Thanks a lot Vivek for the tip that you have provided :)

12 Antony Dick M. 05.22.09 at 7:21 am

When there is a crash in connecting the MySQL Database do the following procedures

To Stop:
etc/init.d/mysql stop

To Start:
etc/init.d/mysql start

To Restart:
etc/init.d/mysql restart

To Reset The Password:
/etc/init.d/mysql reset-password

To Make Your Database To Get Connected When You Are In A Network:
gedit /etc/mysql/my.cnf
//search for bind address and then
change bind address 0.0.0.0

13 rizky 06.05.09 at 10:50 pm

whoa that’s cool dude

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 FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All