版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wawa8899/article/details/86063926
环境: CentOS6.7 + MySQL5.6,忘记root密码。
重置方法:
1. 停止MySQL服务
[root@vm ~]# service mysql stop
2. 修改MySQL配置文件,取消账号验证
[root@vm ~]# vi /etc/my.cnf
[mysqld]
skip-grant-tables
3. 修改root密码
[root@vm ~]# mysql -uroot -p
Enter password: 不需要输入密码,直接回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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 mysql.user set password=password('password') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@vm ~]#
4. 恢复MySQL配置文件,去掉刚添加的配置
[root@vm ~]# vi /etc/my.cnf
[mysqld]
#skip-grant-tables
5. 启动MySQL服务,使用root账密登录成功
[root@vm ~]# service mysql start
[root@vm ~]# mysql -uroot -p
Enter password:password
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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>