前言
一直以來(lái),MySQL的應(yīng)用和學(xué)習(xí)環(huán)境都是MySQL 5.6和之前的版本,也沒(méi)有去關(guān)注新版本MySQL 5.7的變化和新特性。今天幫人處理忘記root密碼的時(shí)時(shí)候,發(fā)現(xiàn)以前的方法不奏效了。
具體情況如下所示:
案例環(huán)境如下:
操作系統(tǒng) : Red Hat Enterprise Linux Server release 6.6 (Santiago)
數(shù)據(jù)庫(kù)版本: 5.7.18 MySQL Community Server (GPL)
忘記密碼,輸入錯(cuò)誤的密碼時(shí)遇到下面錯(cuò)誤信息:
| [root@mytestlnx02 ~]# mysql -u root -pEnter password: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)[root@mytestlnx02 ~]# |
檢查MySQL服務(wù)是否啟動(dòng),如果啟動(dòng),關(guān)閉MySQL服務(wù)
| [root@mytestlnx02 ~]# ps -ef | grep -i mysqlroot 22972 1 0 14:18 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysqlmysql 23166 22972 0 14:18 pts/0 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sockroot 23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql[root@mytestlnx02 ~]# service mysqld stopStopping mysqld: [ OK ][root@mytestlnx02 ~]# |
找到MySQL的my.cnf配置文件,在/etc/my.cnf (有些版本是/etc/mysql/my.cnf)在里面增加下面一段信息:
| [mysqld] skip-grant-tables |
然后啟動(dòng)MySQL,進(jìn)入MySQL后,修改root密碼,操作過(guò)程中遇到ERROR 1054 (42S22): Unknown column 'password' in 'field list',查了一下user表的表結(jié)構(gòu),發(fā)現(xiàn)原來(lái)MySQL 5.7下,user表已經(jīng)沒(méi)有Password字段。加密后的用戶(hù)密碼存儲(chǔ)于authentication_string字段。
具體操作過(guò)程如下所示:
| [root@mytestlnx02 ~]# service mysqld startStarting mysqld: [ OK ][root@mytestlnx02 ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 4Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners. 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 namesYou can turn off this feature to get a quicker startup with -A Database changedmysql> update user set password=PASSWORD('Kd8k&dfdl023') -> where user='root';ERROR 1054 (42S22): Unknown column 'password' in 'field list'mysql> update mysql.user set authentication_string=password('Kd8k&dfdl023') 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 |