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.

Advertisement

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

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

17 comments… add one
  • Antony Dick M. May 22, 2009 @ 7:21

    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

  • rizky Jun 5, 2009 @ 22:50

    whoa that’s cool dude

  • Camilo Sep 19, 2010 @ 23:40

    I use Fedora 12 and It doesn’t work for me.

  • Paul Jardine Mar 17, 2012 @ 8:11

    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

  • jenie Apr 24, 2012 @ 14:51

    I NEED THE CURRENT PASSWORD FOR ROOT IN XAMPP INORDER TO CHANGE MY PASSWORD.

    THANK YOU!

  • cgz - exito en los negocios por internet Jan 3, 2013 @ 19:54

    Thanks for the information, but I can`t go to mysql. this is de error:
    ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: YES)

    grettings

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.