Linux中MYSQL5.7出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

一 关于出现”ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)“错误的解决:

1 查看版本和修改配置

mysql -V								//查看对应的版本号,一般我们的服务器版本为mysql5.7.30
whereis my.cnf							//查找mysql对应的配置文件,路径一般为/etc/my.cnf,mysql8.0以上在/etc/mysql/conf.d/mysql.cnf
vim /etc/my.cnf

在[mysqld]中添加以下内容:
skip-grant-tables

2 重启服务

service mysqld restart	

3 修改密码

mysql -uroot -p								//输入后直接按enter即可,无需输入密码
use mysql
select * from user;							//查看是否有authentication_string字段
update user set authentication_string=password("你的新密码") where user='root';	//若数据库是5.7及以上的用该命令.因为5.7之前没有authentication_string字段,5.7之前的字段叫password
//update user set password=password("你的新密码") where user="root";		//5.7之前的版本用这个命令
flush privileges;	

4 修改登录主机

//update user set host = '%' where user = 'root';				//修改mysql只能在本地登录而无法远程登录(建议用下面的)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你刚刚改的密码';		//这里面*.*代表是所有库.所有表, root是用户名, %代表所有ip都可访问,也可指定ip访问,例如'root'@'172.17.5.90',123456代表root用户的密码;
flush privileges;
select host,user from user;										//查看host可以看到localhsot已经变成%。
quit;

5 修改回配置

vim /etc/my.cnf
然后去掉刚刚添加的skip-grant-tables。

6 重启服务

service mysqld restart
再次登录即可成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值