Reading time: < 1 minute
If we install MySQL and forget the root user password, we can recover it using the following steps:
First, we start MySQL with sudo from the Linux console:
sudo mysql
Once inside, to restore the root password, execute the following command:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new_password';
Replace ‘new_password’ with the password you want to set.
To verify that the password has been changed, use the following command:
SELECT user,authentication_string FROM mysql.user;
It will display a table with the encrypted passwords of MySQL users.
If everything is fine, update the privileges:
FLUSH PRIVILEGES;
Finally, exit the MySQL console:
exit;