Skip to content

How to Reset MySQL Root Password in Linux

How to Recover MySQL root password in Linux. A Step-by-Step Guide to Resetting Your MySQL Root Password on Linux Systems

Steps
6
Category
Databases
Covers
MySQL
Versions & support
About MySQL →
On this page 6 steps

Resetting the MySQL root password in Linux is a crucial task for database administrators, especially when access is lost or compromised. This guide will walk you through the steps to regain control of your MySQL database by resetting the root password. Whether you’re dealing with a forgotten password or enhancing security, following these instructions will help you securely reset your MySQL root password. The process involves stopping the MySQL service, starting it in safe mode, and updating the root password.

These step-by-step instructions will help you recover the mysql root account password quickly.

Prerequisites

Before we begin, make sure you have:

  • Access to the Linux server running MySQL with root privileges or sudo access.
  • MySQL or MariaDB installed on the server.

Step 1 of 6Stop the MySQL Service

The first step is to stop the MySQL service to perform maintenance tasks. Run the following command, depending on your Linux distribution:

  • For Ubuntu/Debian:
    sudo systemctl stop mysql 
    
  • For CentOS/RHEL:
    sudo systemctl stop mysqld 
    

Step 2 of 6Start MySQL in Safe Mode

Skip-Grant-Tables is an option that disables the authentication and authorization checks, allowing you to access MySQL without a password. To start MySQL with this option, use the following command:

sudo mysqld_safe --skip-grant-tables --skip-networking & 

The --skip-networking option will prevent remote connections to the MySQL server during this process.

Step 3 of 6Connect to MySQL

Now that MySQL is running with the Skip-Grant-Tables option, you can connect to it without a password. Run the following command:

mysql -u root 

You should now be connected to the MySQL server as the root user.

Step 4 of 6Set a New Root Password

To set a new root password, follow these steps:

  1. Select the mysql database:
    
    USE mysql;
    
    
  2. Update the root user’s password:
    • For MySQL 5.7 or newer:
      
      ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_new_password';
      
      
    • For MySQL 5.6 or older:
      
      UPDATE user SET Password=PASSWORD('your_new_password') WHERE User='root' AND Host='localhost';
      
      

      Replace your_new_password with your desired new password.

  3. Flush the privileges to apply the changes:
    
    FLUSH PRIVILEGES;
    
    
  4. Exit the MySQL command prompt:
    
    QUIT;
    
    

Step 5 of 6Exit MySQL and Restart the Service

  1. Kill the running mysqld_safe process:
    sudo killall mysqld_safe 
    
  2. Restart the MySQL service:
    • For Ubuntu/Debian:
      sudo systemctl start mysql 
      
    • For CentOS/RHEL:
      sudo systemctl start mysqld 
      

Step 6 of 6Test the New Root Password

To ensure your new root password works, try connecting to the MySQL server using the new password:

mysql -u root -p 

Enter the new password when prompted. If you can access the MySQL server, the password reset was successful.

Conclusion

Resetting the MySQL root password in Linux is a straightforward process if you follow the correct steps. By stopping the MySQL service, starting it in safe mode, and updating the root password, you can quickly regain access to your database. This guide ensures that you can securely handle password issues, maintaining the integrity and security of your MySQL database. Always remember to restart the MySQL service and verify the new password to complete the process.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

5 Comments

  1. I am using server version 10.11.3-MariaDB-1 on Debian 12.
    I am getting this error:

    “ERROR 1290 (HY000): The MariaDB server is running with the –skip-grant-tables option so it cannot execute this statement”

    Any ideas?

      1. Hi, Rahul.

        Thank you for your help.

        Step 2: “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘your_new_password’;”

  2. Hi, I am getting below error while resetting mysql root password.

    ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’

    Please help

Leave a Reply

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


Back to top