Multiple solutions for linux mysql forgotten password or Access denied for user 'root'@'localhost'

  • 2020-05-07 20:36:03
  • OfStack

My system is ubuntu6.06, recently installed mysql always has an error when entering mysql tool:
# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
changes the password of root users using the method described online:
# mysqladmin -uroot -p password 'newpassword'
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
Now I finally found the solution, as follows (please test method 3 first, thank you!) :
method 1:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql > UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql > FLUSH PRIVILEGES;
mysql > quit
# /etc/init.d/mysql restart
# mysql -uroot -p
Enter password: < Enter the newly created password newpassword >
mysql >

method 2:
directly use/etc mysql/debian cnf file [client] section provides the user name and password:
# mysql -udebian-sys-maint -p
Enter password: < Enter the password for section [client] >
mysql > UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql > FLUSH PRIVILEGES;
mysql > quit
# mysql -uroot -p
Enter password: < Enter the new password newpassword >
mysql >

method 3:
I have not tested this method, because my root user's default password has been changed by me. If you have time to test 1, please tell me the result, thank you!!
# mysql -uroot -p
Enter password: < Input/etc/mysql/debian cnf [client] section provides password file >
At this point, the long confused problem is solved!

Related articles: