MySQL 5.7忘記root密碼后修改的詳細(xì)教程
前言
一直以來(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 -p Enter 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 mysql root 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=mysql mysql 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.sock root 23237 21825 0 14:22 pts/0 00:00:00 grep -i mysql [root@mytestlnx02 ~]# service mysqld stop Stopping 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 start Starting mysqld: [ OK ] [root@mytestlnx02 ~]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server 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 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 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
在my.cnf文件中,把剛才加入的那一行“skip-grant-tables”注釋或刪除掉。 然后重啟MySQL服務(wù)后需要執(zhí)行命令set password=password('newpassword');
后,問(wèn)題搞定。
[root@mytestlnx02 ~]# service mysqld start Starting mysqld: [ OK ] [root@mytestlnx02 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 Copyright (c) 2000, 2017, 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; ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. mysql> set password=password('Kd8k&dfdl023'); Query OK, 0 rows affected, 1 warning (0.00 sec)
后面查詢(xún)了一下相關(guān)資料,發(fā)現(xiàn)MySQL 5.7在安全方面有下一些新特性。
1. 用戶(hù)表 mysql.user 的 plugin字段不允許為空, 默認(rèn)值是 mysql_native_password,而不是 mysql_old_password,不再支持舊密碼格式;
2. 增加密碼過(guò)期機(jī)制,過(guò)期后需要修改密碼,否則可能會(huì)被禁用,或者進(jìn)入沙箱模式; 是否啟用密碼過(guò)期由參數(shù)default_password_lifetime控制。
mysql> show variables like 'default_password_lifetime'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | default_password_lifetime | 0 | +---------------------------+-------+ 1 row in set (0.00 sec) mysql>
3:增加了密碼安全等級(jí)以及密碼復(fù)雜度設(shè)置。參數(shù)如下:
mysql> show variables like 'validate_password%'; +--------------------------------------+--------+ | Variable_name | Value | +--------------------------------------+--------+ | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +--------------------------------------+--------+ 7 rows in set (0.00 sec)
4. 使用 mysql_install_db 初始化時(shí),默認(rèn)會(huì)自動(dòng)生成隨機(jī)密碼,隨機(jī)密碼放在/var/log/mysqld.log中,并且不創(chuàng)建除 root@localhost和mysql.sys@localhost 外的其他賬號(hào),也不創(chuàng)建 test 庫(kù);
[root@mytestlnx02 mysql]# yum localinstall mysql-community-{server,client,common,libs}-* [root@mytestlnx02 mysql]# rpm -qa | grep -i mysql mysql-community-client-5.7.18-1.el6.i686 mysql-community-libs-5.7.18-1.el6.i686 perl-DBD-MySQL-4.013-3.el6.x86_64 mysql-community-server-5.7.18-1.el6.i686 mysql-community-common-5.7.18-1.el6.i686 mysql-community-libs-compat-5.7.18-1.el6.i686 [root@mytestlnx02 mysql]# service mysqld start Initializing MySQL database: [ OK ] Installing validate password plugin: [ OK ] Starting mysqld: [ OK ] [root@mytestlnx02 mysql]# [root@mytestlnx02 mysql]# grep 'temporary password' /var/log/mysqld.log 2017-05-05T06:10:57.802143Z 1 [Note] A temporary password is generated for root@localhost: w99s(m-q_ML: mysql> select user ,host from user; +-----------+-----------+ | user | host | +-----------+-----------+ | mysql.sys | localhost | | root | localhost | +-----------+-----------+ 2 rows in set (0.00 sec)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Mysql 5.7.9 shutdown 語(yǔ)法實(shí)例詳解
- Centos 7下使用RPM包安裝MySQL 5.7.9教程
- 解決MySQL 5.7.9版本sql_mode=only_full_group_by問(wèn)題
- MySQL 5.7.9 服務(wù)無(wú)法啟動(dòng)-“NET HELPMSG 3534”的解決方法
- mysql 5.7.9 免安裝版配置方法圖文教程
- MYSQL5.7.9開(kāi)啟慢查詢(xún)?nèi)罩镜募记?/a>
- Mysql5.7修改root密碼教程
- mysql5.7及mysql 8.0版本修改root密碼的方法小結(jié)
- Windows10下MySQL5.7.19安裝教程 MySQL忘記root密碼修改方法
- 更新至MySQL 5.7.9的詳細(xì)教程
相關(guān)文章
軟件測(cè)試-MySQL(六:數(shù)據(jù)庫(kù)函數(shù))
這篇文章主要介紹了MySQL數(shù)據(jù)庫(kù)函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04mysql使用GROUP BY分組實(shí)現(xiàn)取前N條記錄的方法
這篇文章主要介紹了mysql使用GROUP BY分組實(shí)現(xiàn)取前N條記錄的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了mysql中GROUP BY分組的相關(guān)使用技巧,需要的朋友可以參考下2016-06-06MySQL?InnoDB?存儲(chǔ)引擎的底層邏輯架構(gòu)
這篇文章主要為大家介紹了MySQL?InnoDB?存儲(chǔ)引擎的底層邏輯架構(gòu)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09MySQL 主從同步,事務(wù)回滾的實(shí)現(xiàn)原理
這篇文章主要介紹了MySQL 主從同步,事務(wù)回滾的實(shí)現(xiàn)原理,幫助大家更好的理解和使用MySQL,感興趣的朋友可以了解下2020-12-12MySQL中隔離級(jí)別RC與RR的區(qū)別及說(shuō)明
這篇文章主要介紹了MySQL中隔離級(jí)別RC與RR的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08linux下安裝升級(jí)mysql到新版本(5.1-5.7)
這篇文章主要介紹了linux下安裝升級(jí)mysql到新版本(5.1-5.7),需要的朋友可以參考下2016-03-03解決mysql數(shù)據(jù)庫(kù)數(shù)據(jù)遷移達(dá)夢(mèng)數(shù)據(jù)亂碼問(wèn)題
最近接手一個(gè)java項(xiàng)目,需要進(jìn)行重構(gòu),使用國(guó)產(chǎn)數(shù)據(jù)庫(kù)達(dá)夢(mèng)8替換mysql數(shù)據(jù)庫(kù)需要滿(mǎn)足這個(gè)要求,在遷移過(guò)程中遇到mysql數(shù)據(jù)庫(kù)數(shù)據(jù)遷移達(dá)夢(mèng)數(shù)據(jù)亂碼問(wèn)題,怎么解決呢,下面把過(guò)程分享給大家一起看看吧2021-08-08MySQL實(shí)現(xiàn)批量檢查表并進(jìn)行repair與optimize的方法
這篇文章主要介紹了MySQL實(shí)現(xiàn)批量檢查表并進(jìn)行repair與optimize的方法,結(jié)合實(shí)例形式分析了MySQL批量修復(fù)與優(yōu)化表的相關(guān)技巧,需要的朋友可以參考下2016-04-04MySQL 5.7升級(jí)8.0報(bào)異常:ONLY_FULL_GROUP_BY的問(wèn)題解決
本文主要介紹了MySQL 5.7升級(jí)8.0報(bào)異常的問(wèn)題解決,主要是ONLY_FULL_GROUP_BY,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11