【MySQL】之忘记 root 密码

1、修改配置文件跳过权限检查

编辑 MySQL 配置文件 my.cnf,在 [mysqld] 配置节点添加 skip-grant-tables 配置:

[mysqld]
skip-grant-tables

2、重启 MySQL 服务

service mysqld restart

3、修改密码

此时终端用 mysql 命令登录时不需要用户密码:

mysql -u root
-------------
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
mysql>

然后我们可以使用以下两种方式修改密码:

1)使用 set password 命令(8.0 版本之后弃用):

mysql> SET PASSWORD FOR 'root'@'%' = PASSWORD('123456');

2)更新 mysql.user 表:

-- mysql 5.7 版本之前(不含5.7)
UPDATE `mysql`.`user` SET `password`=PASSWORD('123456') WHERE `user`='root';

-- mysql 5.7 版本之后(包含5.7)
UPDATE `mysql`.`user` SET `authentication_string`=PASSWORD('123456') WHERE `user`='root';

-- 密码修改生效
FLUSH PRIVILEGES;

4、还原权限检查配置

编辑 MySQL 配置文件 my.cnf,在 [mysqld] 配置节点删除 skip-grant-tables 配置,然后重启 MySQL 服务即可恢复密码验证登录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值