MYSQL 修改密码 skip-grant-tables

一 关于出现”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    
1
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。
1
2
6 重启服务
service mysqld restart
再次登录即可成功
 

---是不是一个命令也可以

在[mysqld]下行添加 skip-grant-tables,保存即可。

使用管理员身份打开 cmd 操作如下
①重启mysql:
net stop mysql
net start mysql
②进入 mysql ,登录(无密码,回车键入):
mysql -u root -p
③输入 use mysql ,修改 root 的密码:
update user set authentication_string=password('新密码') where user='123456';
flush privileges;
 

----------------------------------

 
[root@mha3 ~]# vi /etc/my.cnf
 先免密登录

 
[root@mha3 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
 
[root@mha3 ~]# mysql -uroot -p
Enter password: 
 

 
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set authentication_string='' where user='root';  密码清空
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2  Changed: 2  Warnings: 0

mysql> exit
Bye
[root@mha3 ~]# vi /etc/my.cnf

启用 密码

[root@mha3 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@mha3 ~]# mysql -uroot -p
Enter password:  空 

mysql> alter user 'root'@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@mha3 ~]# mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@mha3 ~]# mysql -uroot -p
Enter password:   正确密码

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值