环境
Ubuntu 20.04
mysql Ver 8.0.32
问题描述
mysql -u root -p,进入mysql登录时,输入密码出现
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
或者
造成原因
密码不匹配
update mysql.user set authentication_string='1234' where user='root'and Host ='localhost';
根据网上的方法,使用以上语句将密码更新为1234,结果仍然还是没用,最后发现是MYSQL8.0 user表没有password字段,authentication_string字段下是MySQL加密后的字符串密码,所以设置的1234并不是密码。
解决方法
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
使用以上命令进入mysql配置文件
在[mysqld]后面增加skip-grant-tables, wq保存退出
重新mysql服务
service mysql restart
使用mysql -u root -p 进行登录
此时无需密码,直接回车便可登录
use mysql;
update mysql.user set authentication_string='' where user='root'and Host ='localhost';
将authentication_string字段置为空,之后可以无密码登录
再次进入sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释或删除skip-grant-tables
重新mysql服务
service mysql restart
使用mysql -u root -p 再次进行登录,无需密码,直接回车登录
MySQL8.0以后想要修改密码的方法只能是:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ‘需要设置的密码’
参考
https://blog.csdn.net/aswkx/article/details/123896022
http://www.manongjc.com/detail/51-vsbgwzccjwxwmzm.html