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)
# 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 &
nohup: ignoring input and redirecting stderr to stdout Starting mysqld daemon with databases from /var/lib/mysql mysqld_safe: started
And you are done with resetting operation. Simply, stop server and start again:
# killall mysqld
# /etc/init.d/mysql start