Resetting a forgotten MySQL root password

Posted by Steve on Thu 28 Sep 2006 at 09:12

Tags: ,

Resetting the root password of a MySQL database is trivial if you know the current password if you don't it is a little tricker. Thankfully it isn't too difficult to fix, and here we'll show one possible way of doing so.

If you've got access to the root account already, because you know the password, you can change it easily:

steve@steve:~$ mysql --user=root --pass mysql
Enter password:

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0

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

mysql> exit
Bye

However if you don't know the current password this approach will not work - you need to login to run any commands and without the password you'll not be able to login!

Thankfully there is a simple solution to this problem, we just need to start MySQL with a flag to tell it to ignore any username/password restrictions which might be in place. Once that is done you can successfully update the stored details.

First of all you will need to ensure that your database is stopped:

root@steve:~# /etc/init.d/mysql stop

Now you should start up the database in the background, via the mysqld_safe command:

root@steve:~# /usr/bin/mysqld_safe --skip-grant-tables &
[1] 6702
Starting mysqld daemon with databases from /var/lib/mysql
mysqld_safe[6763]: started

Here you can see the new job (number "1") has started and the server is running with the process ID (PID) of 6702.

Now that the server is running with the --skip-grant-tables flag you can connect to it without a password and complete the job:

root@steve:~$ mysql --user=root mysql
Enter password:

mysql> update user set Password=PASSWORD('new-password-here') WHERE User='root';
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2  Changed: 2  Warnings: 0

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

mysql> exit
Bye

Now that you've done that you just need to stop the server, so that you can go back to running a secure MySQL server with password restrictions in place. First of all bring the server you started into the foreground by typing "fg", then kill it by pressing "Ctrl+c" afterwards.

This will now allow you to start the server:

root@steve:~# /etc/init.d/mysql start
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

Now everything should be done and you should have regained access to your MySQL database(s); you should verify this by connecting with your new password:

root@steve:~# mysql --user=root --pass=new-password-here
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5 to server version: 5.0.24a-Debian_4-log

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

mysql> exit
Bye

If you'd like to automate this process you could start by looking at this simple shell script which will allow you to reset a password with one command.

Share/Save/Bookmark


Posted by micky (147.162.xx.xx) on Thu 28 Sep 2006 at 09:35
[ Send Message ]
You can also (on a Debian system) do it without shutting down mysql, just use the password of the debian user store in /etc/mysql/debian.cnf to wipe out the password of the root user.

[ Parent | Reply to this comment ]

Posted by mcphail (62.6.xx.xx) on Thu 28 Sep 2006 at 10:32
[ Send Message ]
Nice tip. but I'd suggest that you don't use the password on the command line (where it will be stored in your ~/.bash_history). Passing a bare "-p" flag will get mysql to prompt you for the password.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.164.xx.xx) on Thu 28 Sep 2006 at 15:21
It'll be viewable to all using ps axu too.

[ Parent | Reply to this comment ]

Posted by jeld (163.192.xx.xx) on Thu 28 Sep 2006 at 19:10
[ Send Message ]
Nope. MySQL client is smart enough not to show command line password in the ps list. It will get recorded in the bash history though.

[ Parent | Reply to this comment ]

Posted by Anonymous (62.31.xx.xx) on Fri 29 Sep 2006 at 00:18
It is visible for a short time until the client removes it from argv, and hence it may be swapped to disk.

[ Parent | Reply to this comment ]

Posted by Anonymous (213.202.xx.xx) on Sun 1 Oct 2006 at 10:38
When I enter passwords or sensitive information in the shell, I exit the shell with 'kill -9 0' (or the equivalent 'kill -9 $$') to just make the shell exit immediately, without saving the history.

[ Parent | Reply to this comment ]

Posted by goeb (84.184.xx.xx) on Sun 1 Oct 2006 at 22:21
[ Send Message | View Weblogs ]
A simple way to prevent a command from being recorded in .bash_history is using
export HISTCONTROL=ignoreboth
in your .bashrc file. With this set all you need to do to is to start the command with a space in front of it to prevent it from being written to the history file.

[ Parent | Reply to this comment ]

Posted by Anonymous (62.254.xx.xx) on Thu 28 Sep 2006 at 19:34
thanks, handy to have this on the site.

i once forgot the password and whilst i dont remember how i reset it the first time, whatever i did reset ALL user passwords, so the next time i used the debian-maintainer user/password and reset it that way :)

sno

[ Parent | Reply to this comment ]

Posted by Anonymous (210.185.xx.xx) on Fri 29 Sep 2006 at 06:00
One thing I noticed in your script Steve, is that you kill -9 the mysqld. Wouldn't it be nicer to send it a standard kill signal, and reduce the risk of corrupting the database?
Cheers,
Jonesy

[ Parent | Reply to this comment ]

Posted by Steve (62.30.xx.xx) on Tue 3 Oct 2006 at 09:07
[ Send Message | View Steve's Scratchpad | View Weblogs ]

Yes it probably would, still for a simple demonstration script it appears to work fairly well.

Steve

[ Parent | Reply to this comment ]

Posted by Anonymous (142.179.xx.xx) on Wed 18 Oct 2006 at 01:02
Great fixed my Problem !!

[ Parent | Reply to this comment ]

Posted by Anonymous (59.92.xx.xx) on Mon 30 Oct 2006 at 13:52
Very useful info. I had the same problem on windows since somehow root password stopped working, so I needed to reset it. And since there is no mysqld_safe on windows, I figured mysqld can be run by itself like so:

mysqld --skip-grant-tables

The other steps should remain same (changing password etc). I had mysql 5 installed as a windows service, so I did the following steps:

1. stopped service
2. mysqld --skip-grant-tables
3. changed password using procedure given.
4. restarted service

That was all.
Vivek Deveshwar

[ Parent | Reply to this comment ]

Posted by Anonymous (59.96.xx.xx) on Wed 12 Mar 2008 at 10:46
Good article for resetting password.
A small addup for this comment.
Some of the mysql versions may not have mysqld.exe you can make use of mysqld-nt.exe

Rest of the procedure is same.

Regards,
Kishore.

[ Parent | Reply to this comment ]

Posted by Anonymous (83.131.xx.xx) on Wed 8 Nov 2006 at 23:06
exelent

[ Parent | Reply to this comment ]

Posted by Anonymous (200.47.xx.xx) on Tue 17 Jul 2007 at 16:50
This explanation es wonderful... I was searching about this problem in many pages but you have helped me a lot with to much precision...

Muchas gracias,

Ruby Ortiz
From Colombia

[ Parent | Reply to this comment ]

Posted by Anonymous (75.13.xx.xx) on Sat 22 Sep 2007 at 00:51
Tho im running fedora it workd the same thanx bunches

[ Parent | Reply to this comment ]

Posted by Anonymous (62.136.xx.xx) on Sun 18 Nov 2007 at 14:47
Fantastic - this was the most helpful guide I found to doing this - sorted in a jiffy!

[ Parent | Reply to this comment ]

Posted by Anonymous (208.50.xx.xx) on Wed 26 Mar 2008 at 13:31
AHH! The script appears to be missing. Can it be found anywhere else?

Thanks!

[ Parent | Reply to this comment ]

Posted by Anonymous (193.137.xx.xx) on Tue 3 Jun 2008 at 19:20
# dpkg -l mysql-server* | grep ii
ii  mysql-server                        5.0.51a-3                     MySQL database server (meta package depending on the latest version)
ii  mysql-server-5.0                    5.0.51a-3                     MySQL database server binaries


# dpkg-reconfigure mysql-server-5.0
Stopping MySQL database server: mysqld.
[debconf screen appears]

 While not mandatory, it is highly       
 recommended that you set a password     
 for the MySQL administrative "root"     
 user.                                   
                                         
 If that field is left blank, the        
 password will not be changed.           
                                         
 New password for the MySQL "root"       
 user:                                   
 _______________________________________ 
                                         
                 <Ok>                    


Stopping MySQL database server: mysqld.
Stopping MySQL database server: mysqld.
Starting MySQL database server: mysqld.
Checking for corrupt, not cleanly closed and upgrade needing tables..

...et voilà, it seems :)

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search