忘记root帐号密码的解决办法

在my.cnf文件的[mysqld]节点下面加入skip-grant-tables
[root@localhost ~]# vim /program/mysql/my.cnf
[mysqld]
skip-grant-tables
[root@localhost ~]# service mysqld restart # 重启MySQL
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]#
 
此时再连接数据库在输入密码时直接回车或输入任意字符都能连接成功
[root@localhost ~]# /program/mysql/bin/mysql -u root -p
Enter password: [回车]
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.16-log Source distribution
 
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> 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
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
 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
 
mysql> EXIT;
Bye
[root@localhost ~]#
 
再次编辑my.cnf文件,把[mysqld]节点下面的skip-grant-tables删除或注释掉
[root@localhost ~]# vim /program/mysql/my.cnf
[mysqld]
# skip-grant-tables
[root@localhost ~]# service mysqld restart # 重启MySQL
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@localhost ~]# 

Copyright © 2024 码农人生. All Rights Reserved