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

MySQL 5.7 修改 Root 密碼完整教學(含 authentication_string 字段變更)

     編輯:MySQL 資料庫相關知識

MySQL 5.7 將 user 表的 Password 字段改為 authentication_string,舊版教學多失效。本文提供經驗證可行的修改流程。

方法一:skip-grant-tables 免密登入法(推薦)

  1. 停止服務:killall -TERM mysqld
  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. 重啟服務:killall -TERM mysqld && mysqld_safe &
  2. 登入後執行 ALTER USER 完成功能解鎖:
ALTER USER 'root'@'localhost' IDENTIFIED BY '123';

方法二:官方 mysql_secure_installation 流程(5.6+ 版本)

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

舊版(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
# 重啟服務

關鍵提醒:5.7 必須操作 authentication_string 字段,並配合 ALTER USER 或 SET PASSWORD 刷新快取,否則雖能登入但功能受限。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved