How to Reset MySQL “root” Password in Linux

MySQL is an open-source database software widely used for data storage. Sometimes we forgot the MySQL root password. So don’t be panic, this tutorial will help you to reset the MySQL root password with simple steps.

Reset MySQL Root Password

  1. Start MySQL in Safe Mode – First of all, you are required to stop running the MySQL servers. Use one of the following commands to stop the MySQL server on your Linux system.
    systemctl stop mysql.service 
  2. Start MySQL in Safe Mode – Now start MySQL server in safe mode using the --skip-grant-tables option. Use the following command to start MySQL in safe mode. In safe mode, MySQL does not prompt for a login password.
    mysqld_safe --skip-grant-tables & 
  3. Reset MySQL root Password – Next, log in to MySQL server as root user and change password using the following set of commands. This will reset the MySQL root password on your system. For MySQL 5.6 or Below
    mysql -u root
    USE mysql; 
    UPDATE user SET password=PASSWORD("NEW-PASSWORD") WHERE User='root';
    FLUSH PRIVILEGES; 
    quit

    For MySQL 5.7 or Above

    mysql -u root
    SET PASSWORD FOR 'root'@'localhost' = PASSWORD("NEW-PASSWORD"); 
    FLUSH PRIVILEGES; 
    quit
  4. Restart MySQL Service – Once you change the password. Terminate the current mysqld process, and then start it again as regular service.
    sudo pkill mysqld && sudo pkill mysqld_safe  
    systemctl start mysql.service 

Verify New Password

After resetting the MySQL root account password and restarting, just verify the new password by the login.

mysql -u root -p 
Enter password: **********
Welcome to the MySQL monitor. Commands end with ; or \g. 
Your MySQL connection id is 0007 Server version: 5.7.33-0ubuntu0.18.04.1 (Ubuntu) 
Copyright (c) 2000, 2021, Oracle and/or its affiliates. 
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
Other names may be trademarks of their respective owners. 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>

 

Leave a Comment