Environment: CentOS7 MySQL5.7
mysql -uroot -p
, and then prompt after entering the password
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
- stop mysql service
sudo systemctl mysqld stop
Skip verification
Edit mysql configuration file
sudo vim /etc/my.cnf
Add a line at the end to make it skip the permission check when logging in
skip_grant_tables
Change the password and
start the MySQL service,
sudo systemctl mysqld start
Login to MySQL
mysql -uroot -p
At this point, you are prompted to enter a password, enter any password and press Enter to enter MySQL.
Switch the database to the mysql library
mysql>USE mysql;
change Password
mysql>update user set authentication_string = password(‘newpasswd’) where user='root';
Refresh MySQL privilege-related tables
mysql>flush privileges;
quit
mysql>exit;
Restart the service Delete or comment the skip permission statement added in
the my.cnf fileRestart the service and log in with the modified password.
sudo systemctl mysqld restart