最近升级了 MySQL 至 5.7.11 版本,root 密码忘记了,按照之前的方式 mysqld_safe --skip-grant-tables 不管用了,因为从 5.7.6 版本开始默认是不安装 mysqld_safe 了,如下:
下面为 MySQL 5.7.6 版本以上重置 root 密码的方法:
1,停止 mysql 服务
[[email protected] usr]# systemctl stop mysqld
2,设置 mysqld 选项 --skip-grant-tables 参数
[[email protected] usr]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
3,重新启动 mysql
[[email protected] usr]# systemctl start mysqld
4,执行 mysql -u root 登录 mysql,并更改密码
[[email protected] usr]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server 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 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> 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: 1 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
5,重置完密码后,去掉 --skip-grant-tables 参数后,重启 mysql
[[email protected] usr]# systemctl unset-environment MYSQLD_OPTS [[email protected] usr]# systemctl restart mysqld [[email protected] usr]# mysql -h localhost -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server 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 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>
时间: 2024-10-06 10:00:01