MySQL Reset Root Password

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:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Your support makes a big difference:

I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft takes a lot of my time and hard work to produce. If everyone who reads nixCraft, who likes it, helps fund it, my future would be more secure. You can donate as little as $1 to support nixCraft:

17 comment

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.

I had problems in Ubuntu, due to apparmor preventing mysql from reading sql files from the root directory. Placing the init-file in /usr/share/mysql directory solved the problem. Or you can look at /etc/apparmor.d/usr.sbin.mysqld