MySQL 5.7.6 以上版本的 root 密码重置

时间:2022-03-29 00:49:45

最近升级了 MySQL 至 5.7.11 版本,root 密码忘记了,按照之前的方式 mysqld_safe --skip-grant-tables 不管用了,因为从 5.7.6 版本开始默认是不安装 mysqld_safe 了,如下:


MySQL 5.7.6 以上版本的 root 密码重置

下面为 MySQL 5.7.6 版本以上重置 root 密码的方法:


1,停止 mysql 服务


[root@study usr]# systemctl stop mysqld


2,设置 mysqld 选项 --skip-grant-tables 参数


[root@study usr]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"


3,重新启动 mysql


[root@study usr]# systemctl start mysqld


4,执行 mysql -u root 登录 mysql,并更改密码


[root@study usr]# mysql -u rootWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.11 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> UPDATE mysql.user    ->     SET authentication_string = PASSWORD('newrootpasswd'), password_expired = 'N'    ->     WHERE User = 'root' AND Host = 'localhost';Query OK, 1 row affected, 1 warning (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 1mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.01 sec)


5,重置完密码后,去掉 --skip-grant-tables 参数后,重启 mysql


[root@study usr]# systemctl unset-environment MYSQLD_OPTS[root@study usr]# systemctl restart mysqld[root@study usr]# mysql -h localhost -u root -pEnter password:Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.11 MySQL Community Server (GPL)Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>


本文出自 “安静的疯子” 博客,请务必保留此出处http://quietmadman.blog.51cto.com/3269500/1768057