To reset a forgotten MySQL root password, you can follow these steps:
Step 1: Stop the MySQL Service
First, you need to stop the MySQL service. You can do this with the following command:
sudo service mysql stop
Step 2: Start MySQL in Safe Mode
Next, start MySQL in safe mode, which allows you to bypass the usual authentication:
sudo mysqld_safe --skip-grant-tables &
Step 3: Log in to MySQL
Now, log in to MySQL without a password:
mysql -u root
Step 4: Reset the Root Password
Once logged in, run the following commands to reset the root password:
FLUSH PRIVILEGES; -- This command ensures that the privilege changes take effect
ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPassword';
Replace YourNewPassword with your desired new password.
Step 5: Exit MySQL
After resetting the password, exit the MySQL prompt:
EXIT;
Step 6: Stop Safe Mode and Restart MySQL
Stop the MySQL safe mode process and restart the MySQL service normally:
sudo service mysql stop
sudo service mysql start
Step 7: Log in with the New Password
Finally, you can log in to MySQL using the new root password:
mysql -u root -p
You will be prompted to enter the new password.
Note
Make sure to follow these steps carefully, as improper handling can lead to data loss or service disruption. If you want to learn more about MySQL administration, consider exploring related labs or tutorials.
