Resetting a forgotten MySQL root password
Posted by Steve on Thu 28 Sep 2006 at 09:12
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.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
export HISTCONTROL=ignorebothin 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 ]
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 ]
Cheers,
Jonesy
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
Yes it probably would, still for a simple demonstration script it appears to work fairly well.
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
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 ]
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 ]
[ Parent | Reply to this comment ]
Muchas gracias,
Ruby Ortiz
From Colombia
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
Thanks!
[ Parent | Reply to this comment ]
# 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 ]