How to Reset or Change the MySQL Root Password (Step-by-Step Guide)
Introduction
Managing database security is a fundamental responsibility for developers, system administrators, and database managers. One of the most important aspects of database security is controlling access to administrative accounts. In the popular relational database management system MySQL, the root user is the most powerful account and has complete control over the database server.
The root user can create databases, manage user permissions, modify server configurations, and perform other administrative tasks. Because of this high level of authority, protecting the root account with a strong password is critical.
However, there are situations where users need to change or reset the MySQL root password. For example, you may forget the password, inherit a server from another administrator, or simply want to update the password as part of regular security maintenance.
In this SEO-optimized guide, you will learn how to change the MySQL root password, how to reset it if you forgot it, and best practices for securing your MySQL server.
Before discussing the steps, it is important to understand why resetting the root password may be necessary.
1. Forgotten Root Password
One of the most common situations is forgetting the MySQL root password, which prevents administrative access to the database.
2. Security Maintenance
Organizations often enforce password rotation policies to improve security.
3. Server Migration
When transferring server control between administrators, changing credentials ensures security.
4. Unauthorized Access Prevention
If there is any suspicion that the password has been compromised, it should be changed immediately.
Resetting or changing the password ensures that the database environment remains secure and properly managed.
Method 1: Change MySQL Root Password (If You Know the Current Password)
If you already know the root password, changing it is simple and can be done through the MySQL command line.
Step 1: Log in to MySQL
Open a terminal or command prompt and enter:
mysql -u root -p
Step 2: Change the Password
Once logged in, execute the following SQL command:
ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘NewPassword’;
Step 3: Apply the Changes
Reload the privilege tables by running:
FLUSH PRIVILEGES;
Your MySQL root password has now been successfully updated.
Method 2: Reset MySQL Root Password (If You Forgot It)
If you cannot remember the root password, you must reset it by starting MySQL in recovery mode.
Step 1: Stop the MySQL Service
First, stop the MySQL server:
sudo systemctl stop mysql
sudo service mysql stop
Step 2: Start MySQL in Safe Mode
Start MySQL with the skip-grant-tables option:
sudo mysqld_safe –skip-grant-tables &
Step 3: Access the MySQL Server
Open another terminal window and run:
mysql -u root
Step 4: Reset the Root Password
Run the following commands:
FLUSH PRIVILEGES;
ALTER USER ‘root’@‘localhost’ IDENTIFIED BY ‘NewPassword’;
Replace NewPassword with the password you want to use.
Step 5: Restart MySQL Normally
Restart the MySQL service:
sudo systemctl restart mysql