1. 程式人生 > >[MySQL] MySQL5.6忘記root密碼

[MySQL] MySQL5.6忘記root密碼

環境: CentOS6.7 + MySQL5.6,忘記root密碼。

重置方法:

1. 停止MySQL服務

[[email protected] ~]# service mysql stop

2. 修改MySQL配置檔案,取消賬號驗證

[[email protected] ~]# vi /etc/my.cnf
[mysqld]
skip-grant-tables

3. 修改root密碼

[[email protected] ~]# 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
[
[email protected]
~]#

4. 恢復MySQL配置檔案,去掉剛新增的配置

[[email protected] ~]# vi /etc/my.cnf
[mysqld]
#skip-grant-tables

5. 啟動MySQL服務,使用root賬密登入成功

[[email protected] ~]# service mysql start
[[email protected] ~]# 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>