MySQL 5.7 忘记root用户密码怎么办?

本文介绍了一种通过使用--skip-grant-tables参数来启动MySQL服务的方法,实现免密登录并更新root用户的密码。该方法包括停止服务、修改配置文件、更新密码等步骤。

1、解决思路

mysqld 服务提供了允许免密登录的启动参数"–skip-grant-tables",使用此参数启动 mysqld 服务后,所有mysql用户均可免密登录 mysqld 服务器, 并且拥有对所有表的全部操作权限。

2、先停止mysqld服务

$ systemctl stop mysqld

3、修改my.cnf配置文件以开启免密登录

[mysqld]

# 开启免密登录
# --skip-grant-tables:Start without grant tables. This gives all users FULL ACCESS to all tables
skip-grant-tables=true

4、启动mysqld服务

$ systemctl start mysqld

5、免密登录mysqld并修改root用户密码

$ mysql -uroot

mysql> update mysql.user set authentication_string=password('新密码') where user='root' and Host ='localhost';

mysql> flush privileges;

# 注:因为使用了"--skip-grant-tables"启动mysqld服务,故不能使用"alter user"语句
mysql> alter user root@'localhost' identified by '123456';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

6、修改my.cnf配置文件以关闭免密登录

[mysqld]

# 注释或删除"--skip-grant-tables"选项
# skip-grant-tables=true

7、重启mysqld

$ systemctl restart mysqld

0、参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值