快速重置MySQL root 密码
blank
blank
发布于 2019-03-14 / 432 阅读 / 0 评论 / 0 点赞

快速重置MySQL root 密码

编辑 MySQL 配置文件/etc/my.cnf,在[mysqld]下添加一行skip-grant-tables

vim /etc/my.cnf

[mysqld]
#...
skip-grant-tables
#...

重启MySQL

systemctl restart mysqld

终端输入 mysql 直接登录MySQL数据库:

mysql

切换到mysql数据库

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

重置 root 密码

(需要注意的是
在MySQL5.7之后
已经没有password这个字段了
password字段改成了authentication_string
修改密码我们要修改这个字段的值)

mysql> update user set authentication_string=password('新密码') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

还原修改 /etc/my.cnf 文件将之前添加的skip-grant-tables 这句话注释掉 下次忘记密码还可以用

vim /etc/my.cnf

[mysqld]
#...
#skip-grant-tables
#...

DONE


评论