忘记mysql数据库密码后的修改密码方式

当忘记mysql数据库密码如何进行密码修改(本文mysql版本为5.7)
1.进入mysql配置文件中

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

2.在[mysqld]中添加 skip-grant-tables(跳过数据权限验证)

[mysqld]
skip-grant-tables

3.重启数据库,让配置文件生效

[root@localhost ~]# systemctl restart mysqld

4.登录数据库
因为前面在配置文件中加入了跳过验证,所以这里不用输入密码,直接回车就可以进入到数据库了

[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

5.修改密码

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=password("新密码") where user='root';
Query OK, 2 rows affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 1

注意在mysql5.7版本里面没有password这个字段了,password字段换成了authentication_string,所以执行下面的命令会报错

mysql> update user set password=password("新密码") where user='root';
ERROR 1054 (42S22): Unknown column 'password' in 'field list'

6.刷新权限
在修改完后记得刷新权限,让刚刚修改的策略立刻生效
刷新完后,退出数据库

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye

以上就已经修改了密码,在修改完密码后,一定要记得把刚刚在数据库配置文件中加的skip-grant-tables注释掉
7.注释skip-grant-tables

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
#skip-grant-tables

8.再次重启数据库

[root@localhost ~]# systemctl restart mysqld

到这才算改好了密码,使用新密码登录数据库正常,密码修改完成
注意:skip-grant-tables这里的修改是跳过了数据库的安全验证,在这个模式下,所有人都可以随意的登录数据库,并进行修改,此项步骤很危险,修改完密码后一定要记得关闭。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhaixing_0307

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值