首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

MySQL 5.7 忘記 Root 密碼重置完整教學(含 authentication_string 關鍵點)

     編輯:MySQL 資料庫相關知識

MySQL 5.7 密碼存儲字段從 Password 改為 authentication_string,舊教程失效。本文提供經驗證的重置流程,含官方隨機密碼方式對比。

通用重置流程

  1. service mysql stop
  2. mysqld_safe --skip-grant-tables &(生產加 --skip-networking)
  3. mysql -u root 免密進入
  4. 更新密碼:
UPDATE mysql.user SET authentication_string = PASSWORD('123qwe')
WHERE user = 'root' AND Host = 'localhost';
FLUSH PRIVILEGES; QUIT;
  1. service mysql restart
  2. 登入後 **必須執行** 完成功能解鎖:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123';
-- 或
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123');

官方隨機密碼方式 (5.6+)

  1. 首啟生成 /root/.mysql_secret,cat 取得密碼。
  2. mysqladmin -u root -h localhost password '123456' -p 輸入隨機密碼修改。

實測該方式在某些環境下不可靠,推薦上述 skip-grant-tables 通用法。

舊版 (5.6 前) 參考

service mysqld stop
mysqld_safe --skip-grant-tables &
mysql -u root -p  # 直接 Enter
USE mysql;
UPDATE user SET password = PASSWORD('123456') WHERE user = 'root';
FLUSH PRIVILEGES; QUIT
service mysqld restart

關鍵提醒

  • 5.7 **必須操作 authentication_string** 字段。
  • 修改後 **必須 ALTER USER 或 SET PASSWORD** 刷新快取,否則功能受限(如無法建表、授權)。
  • 生產環境重置後務必檢查應用連線池密碼同步更新。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved