MySQL 5.7忘記root密碼如何修改?

来源:http://www.cnblogs.com/kerrycode/archive/2017/05/05/6813497.html
-Advertisement-
Play Games

一直以來,MySQL的應用和學習環境都是MySQL 5.6和之前的版本,也沒有去關註新版本MySQL 5.7的變化和新特性。今天幫人處理忘記root密碼的時時候,發現以前的方法不奏效了。具體情況如下所示: 案例環境如下: 操作系統 : Red Hat Enterprise Linux Server ... ...


一直以來,MySQL的應用和學習環境都是MySQL 5.6和之前的版本,也沒有去關註新版本MySQL 5.7的變化和新特性。今天幫人處理忘記root密碼的時時候,發現以前的方法不奏效了。具體情況如下所示:

 

案例環境如下:

        操作系統 : Red Hat Enterprise Linux Server release 6.6 (Santiago)

        資料庫版本: 5.7.18 MySQL Community Server (GPL)

 

忘記密碼,輸入錯誤的密碼時遇到下麵錯誤信息:

 

[root@mytestlnx02 ~]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@mytestlnx02 ~]#

 

檢查MySQL服務是否啟動,如果啟動,關閉MySQL服務

 

[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

然後啟動MySQL,進入MySQL後,修改root密碼,操作過程中遇到ERROR 1054 (42S22): Unknown column 'password' in 'field list',查了一下user表的表結構,發現原來MySQL 5.7下,user表已經沒有Password欄位。加密後的用戶密碼存儲於authentication_string欄位。具體操作過程如下所示:

 

[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

clip_image001

 

在my.cnf文件中,把剛纔加入的那一行“skip-grant-tables”註釋或刪除掉。 然後重啟MySQL服務後需要執行命令set password=password('newpassword');後,問題搞定。

[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)

 

後面查詢了一下相關資料,發現MySQL 5.7在安全方面有下一些新特性(參考MySQL 5.7版本新特性連載(三)

 

1. 用戶表 mysql.user 的 plugin欄位不允許為空, 預設值是 mysql_native_password,而不是 mysql_old_password,不再支持舊密碼格式;

 

clip_image002

 

2. 增加密碼過期機制,過期後需要修改密碼,否則可能會被禁用,或者進入沙箱模式; 是否啟用密碼過期由參數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:增加了密碼安全等級以及密碼複雜度設置。參數如下:

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 初始化時,預設會自動生成隨機密碼,隨機密碼放在/var/log/mysqld.log中,並且不創建除 root@localhost和mysql.sys@localhost 外的其他賬號,也不創建 test 庫;

[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)

 

參考資料:

http://imysql.com/2015/07/08/mysql-57-new-feature-part-3.shtml

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 在我們很多情況下的開發,為了方便或者通用性的考慮,都首先考慮SQL Server資料庫進行開發,但有時候客戶的生產環境是Oracle或者其他資料庫,那麼我們就需要把對應的數據結構和數據腳本轉換為對應的資料庫,數據結構一般來說,語法都遵循了SQL92的標準,或者我們根據不同的PowerDesigner... ...
  • 一、排序檢索數據 1.排序數據:SELECT prod_name FROM Products ORDER BY prod_name(對prod_name列以字母順序排序數據) ORDER BY子句的位置在指定一條ORDER BY子句時,應該保證它是SELECT語句中最後一條子句。如果它不是最後的子句 ...
  • 當我們電腦與oracle編碼不一致時,可能出現中文亂碼問題,這時候我們只要將cmd的編碼改成和oracle一致即可 (sql plus界面顯示亂碼) (更改cmd編碼為UTF8後顯示正常) 使用chcp 65001 命令改變字元集 附贈幾個常用字元集: 936 簡體中文(預設) 65001 UTF ...
  • 本文出處:http://www.cnblogs.com/wy123/p/6815049.html 最新在學習MySQL,純新手,對Linux瞭解的也不多,因為是下載的最新版的MySQL(MySQL5.7.18)二進位包,CentOS7.2下測試安裝,方便以後折騰。大概步驟如下,安裝刪除反覆折騰了幾遍 ...
  • 在 http://download.csdn.net/download/wuqinfei_cs/7740373 下載PL/SQL Developer。 1. 解壓,在instantclient_11_2目錄下新建NETWORK文件夾,再在其下新建ADMIN文件夾。將oracle\product\11 ...
  • 雖然PXC支持線上增加群集節點,但是目前尚未解決wsrep_sst_method=xtrabackup 或wsrep_sst_method=mysqldump時報錯的問題,因此嘗試手動完成xtrabackup備份和恢復並添加到群集節點。 場景: 有三台乾凈的CentOS 6的伺服器,IP配置為:19 ...
  • 1.查看索引 (1)單位是GB SELECT CONCAT(ROUND(SUM(index_length)/(1024*1024*1024), 2), ' GB') AS 'Total Index Size' FROM information_schema.TABLES WHERE table_sc ...
  • MongoDB 是一款NoSql資料庫,沒有固定的模式,即同一個集合中的不同文檔結構可以不同,如:第一條記錄{name:”xiaoming”},第二條記錄:{name:”xiaoli”,age:15},這在關係型資料庫中是無法實現的。對於MongoDB,其中:collections即集合相當於關係型 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...