How to reset root MySQL

If you have forgotten MySQL root password and have root access to linux system follow the below steps.

  • Stop the MySQL server process as root users using command
$ sudo service mysql stop
  • Start MySQL serve rin safe mode with skip grant table command
$ sudo mysqld_safe —skip-grant-tables —skip-networking &
If it throughs any error saying mysqld folder not found. Create the folder
$ sudo mkdir -p /var/run/mysqld
  • login with MySQL server using command, it won't ask for password.
$ mysql -u root -p
  • Once logged in follow below MySQL sql query to change new password
mysql> use mysql;
​mysql> update user set authentication_string=password('NEWPASSWORD') where user='root';
​mysql> flush privileges;
​mysql > quit
  • Now stop MySQL server using kill command
$ killall mysqld_safe
  • Start MySQL server using default service and login with new password.
$ sudo service mysql start  


$ sudo /etc/init.d/mysql start

Also can be reset using temporary file using below steps

  1. Kill/Stop running MySQL server first
$ sudo  kill `cat /mysql-data-directory/`


$ sudo killall mysqld
  1. Create a temporary file (init_mysql.tmp) with following sql with new password in place of "MyNewPassword" in current folder.
    1. For MySQL 5.7.6 and later:
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword';
    2. For MySQL 5.7.5 and earlier:
      SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
  2. Start MySQL server using temporary file created in previous step.
$ mysqld --init-file=/home/me/init_mysql.tmp &
  1. After the server has started successfully delete the temporary file /home/me/init_mysql.tmp
  2. Now login in to MySQL server with the pemoprary password give in file and reset using the below SQL commands
mysql> UPDATE mysql.user SET authentication_string = PASSWORD('MyNewPass'), password_expired = 'N'     WHERE User = 'root' AND Host = 'localhost';
  1. Once set restart the MySQL server and all done.

Tags: MySQL, Reset Password