1. 处理的状态

首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库。因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的状态下,其他的用户也可以任意地登录和修改MySQL的信息。可以采用将MySQL对外的端口封闭,并且停止Apache以及所有的用户进程的方法实现服务器的准安全状态。最安全的状态是到服务器的Console上面操作,并且拔掉网线。

2. 重置密码

  1. 修改mysql配置文件,增加skip-grant-tables
  1. [root@demo ~]# vim /etc/my.cnf
  2. [mysqld]
  3. skip-grant-tables
  1. 重启mysqld
  1. [root@demo ~]# service mysqld restart
  2. Stopping mysqld: [ OK ]
  3. Starting mysqld: [ OK ]
  1. 登录mysql,并修改root密码.
  1. [root@demo ~]# mysql -u root
  2. Welcome to the MySQL monitor. Commands end with ; or \g.
  3. Your MySQL connection id is 3
  4. Server version: 5.5.53 MySQL Community Server (GPL)
  5. Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  6. Oracle is a registered trademark of Oracle Corporation and/or its
  7. affiliates. Other names may be trademarks of their respective
  8. owners.
  9. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  10. mysql> use mysql
  11. Reading table information for completion of table and column names
  12. You can turn off this feature to get a quicker startup with -A
  13. Database changed
  14. mysql> update user set password=password('new-pass') where user='root';
  15. Query OK, 5 rows affected (0.00 sec)
  16. Rows matched: 5 Changed: 5 Warnings: 0
  1. 配置文件去除skip-grant-tables,并重启服务器.
  1. [root@demo ~]# vim /etc/my.cnf
  2. [root@demo ~]# service mysqld restart
  1. 使用新密码登录到mysql
  1. [root@demo ~]# mysql -h localhost -u root -p