Linux下MySQL重置密码 //ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

前言

在实际生产环境中,请注意数据安全,本例使用的方法为通过修改MySQL配置文件权限认证的方法进入mysql后修改user表,修改期间其他人访问数据库同样无需认证,所以使用此方法前请在服务器上配置其他用户禁止访问,具体方法比较多,按实际情况选择

案例

Linux版本:Linux legend_never_dies 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

MySQL版本:mysql Ver 14.14 Distrib 5.7.31, for Linux (x86_64) using EditLine wrapper

Linux操作用户:root

登录失败
网上找了一圈,方法大同小异,开篇的原因是有些方法的mysql命令是旧版本的,所以更新一下。

1编辑mysql的配置文件

1.1进入配置文件

[root@legend_never_dies ~]# vim /etc/my.cnf

1.2 添加跳过认证参数

在[mysqld]下面添加skip-grant-tables,wq保存退出

[mysqld]
skip-grant-tables

1.3 重启mysql

[root@legend_never_dies ~]# systemctl restart mysqld

1.4 进入mysql

[root@legend_never_dies ~]# mysql

进入成功

1.5 切换用户并修改user表

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 mysql.user set authentication_string=password("123456") where user='root' and host='localhost';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql> \q
Bye

1.6 退出并恢复mysql配置

[root@legend_never_dies ~]# vim /etc/my.cnf

将skip-grant-tables备注或删除,wq保存退出

[mysqld]
#skip-grant-tables

1.7 重启mysqld服务

[root@legend_never_dies ~]# systemctl restart mysqld

1.8 验证登录

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值