If you would like to reset the password for the root user's MySQL password, this guide will help you through that process.
Reset MySQL Password
Stop the MySQL Server
You will want to have the correct version before continuing with this guide and can get it using this command:
Before continuing with the following, you will want to know that these steps are intended to be run by the root user using su or sudo commands. Logging in as your root user is not recommended.
To change the MySQL password, you will need first to stop the MySQL service.
service mysql stop
If you know your password, you can change it using the MySQL command line with the following for the root user in this example
mysql -u root -p
Replace the new_password above with your new password.
By running MySQL without loading any information about the user privileges, you can access the command line without using root privileges which would require a password. You can do this by not allowing the database to load the grant tables. You'll also want to skip networking to avoid any security risks.
It is important to note that using --skip-networking below is very insecure and should only be run while resetting the root user's password if you have forgotten it.
Start the MySQL command prompt using this command:
mysqld_safe --skip-grant-tables --skip-networking &
You'll now be able to access the database without using the root password.
mysqk -u root
A MySQL command prompt will appear:
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
You can now change the password and will want to remain in the MySQL command prompt.
Changing The Password
You will first want to use the FLUSH PRIVILEGES command.
Now we can actually change the root password and exit the MySQL command line by pressing CTRL + C to exit.
You can then use one of the below commands to change your password based on the version of MySQL you are using.
For MySQL 5.7.6 and newer, use the following command in the MySQL command prompt.
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
For MySQL 5.7.5 and older users in the MySQL command prompt:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');
Enter your new password above and replace the new_password.
Once you have received the message that the query was OK, you can Flush Privileges
Restart the MySQL Server
Now you will need to start the service.
sudo service mysql start
You can now confirm that the password reset worked by running the following command:
mysql -u root -p