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: Stop 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: Start 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: Connect 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: Set a New Root Password
To set a new root password, follow these steps:
- Select the mysql database:
USE mysql;
- 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.
- For MySQL 5.7 or newer:
- Flush the privileges to apply the changes:
FLUSH PRIVILEGES;
- Exit the MySQL command prompt:
QUIT;
Step 5: Exit MySQL and Restart the Service
- Kill the running mysqld_safe process:
sudo killall mysqld_safe
- Restart the MySQL service:
- For Ubuntu/Debian:
sudo systemctl start mysql
- For CentOS/RHEL:
sudo systemctl start mysqld
- For Ubuntu/Debian:
Step 6: Test 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.
5 Comments
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?
Hi JP,
In which command, are you getting the error?
Hi, Rahul.
Thank you for your help.
Step 2: “ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘your_new_password’;”
Hi, I am getting below error while resetting mysql root password.
ERROR 1054 (42S22): Unknown column ‘password’ in ‘field list’
Please help
Thanks dear. It helps me a lot to quickly reset mysql 5.7 password.