MySQL Root Password Recovery

When the official documentation from MySQL for root password reset does not work, here’s another solution.

Get the password for the debian-sys-maint user. Just

cat /etc/mysql/debian.cnf

and get the password.

Then

mysql -u debian-sys-maint -p

and enter the password you got in the previous step.

Once on the MySQL shell, you can define the password using

UPDATE USER SET password=password('new_root_password') WHERE USER='root';
commit;
FLUSH PRIVILEGES;

where new_root_password is the new password for the root.
If there are no root entries, or if you’re still getting problems logging in using root, grant all the privileges to the root user:

GRANT ALL privileges ON *.* TO 'root'@'localhost' IDENTIFIED BY 'new_root_password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

where, again, the new_root_password is the new password for the root.

This should do work and now you can

mysql -u root -p

and enter the new password you’ve chosen for the root.