How to set, change, and recover a MySQL root password
To reset the password for MySQL you first must create a new file with the following contents:
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘PASSWORD’;
Where PASSWORD is the new password to be used. Save that file as ~/mysql-pwd.
Next, stop the MySQL daemon with the command:
sudo systemctl stop mysql
With the daemon stopped, issue the command:
sudo mysqld -init-file=~/mysql-pwd
Once your command prompt is returned, restart the MySQL daemon with the command:
sudo systemctl start mysql
You should now be able to log into the MySQL command prompt with the new admin password like so:
mysql -u root -p
When prompted, type the admin password and you’re good to go.