ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES/NO)

环境

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值