centOS7登录mysql提示ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

登录mysql数据库的时候,提示ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)的问题
mysql环境:8.0.12
解决方法:
1.编辑/etc/my.cnf,在mysqld下追加skip-grant-tables。(注意是mysqld下)
vi /etc/my.cnf

2.重启mysql服务
systemctl restart mysql
如果出现Failed to restart mysql.service: Unit not found.
可以试试systemctl restart mysql
重启成功之后再次进行登录

3.mysql -uroot -p直接登录,按enter键,无需输入密码

4.执行sql语句将密码置位空
mysql> use mysql;
mysql> update user set authentication_string='' where user='root';

5.将/etc/my.cnf追加的语句注释
vi /etc/my.cnf

6.重启mysql服务
systemctl restart mysql
出现错误参考2

7.执行登录语句,并将密码改为helloworld
mysql -uroot --connect-expired-password #登录
mysql> use mysql;
mysql> alter user 'root'@'localhost' identified by 'helloworld' password expire never; # 修改密码操作
如果出现ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,可执行下面三个语句,再进行修改密码操作
mysql> SHOW VARIABLES LIKE 'validate_password%';
mysql> set global validate_password_length=4;
mysql> set global validate_password_policy=0;
密码修改成功之后刷新数据库,再退出
mysql> FLUSH PRIVILEGES;
mysql> quit #退出

8.重启mysql服务
systemctl restart mysql
出现错误参考2

9.使用mysql -uroot -p登录,密码输入helloworld,完成!

你可能感兴趣的:(centOS7登录mysql提示ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES))