萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL Root 密碼遺忘完整重置指南:Windows / Linux / macOS 通用方案

核心原理:跳過權限表啟動

以 --skip-grant-tables 啟動 mysqld,臨時禁用權限檢查,無密碼以 root 連線修改密碼,再正常重啟。

通用步驟 (所有平台)

  1. 停止 MySQL 服務
  2. 以 --skip-grant-tables --skip-networking 啟動 (--skip-networking 防止遠程無授權存取)
  3. 無密碼連線:mysql -u root
  4. 執行密碼重置 SQL
  5. 重啟 MySQL 正常模式
  6. 驗證新密碼

Windows 詳細操作

REM 1. 以管理員身分開啟 CMD / PowerShell
REM 2. 停止服務 (服務名視版本:MySQL80, MySQL57, mariadb)
net stop MySQL80

REM 3. 進入安裝目錄 bin 資料夾
cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"

REM 4. 前台啟動跳過權限 (新開一個視窗執行,保持運行)
mysqld --skip-grant-tables --skip-networking

REM 5. 新開視窗,無密碼登入
mysql -u root

REM 6. MySQL 客戶端內執行 (MySQL 8.0+)
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPass!2024';
-- 或 5.7 相容語法
-- UPDATE mysql.user SET authentication_string=PASSWORD('NewPass') WHERE User='root' AND Host='localhost';
-- FLUSH PRIVILEGES;
EXIT;

REM 7. 關閉跳過權限的視窗 (Ctrl+C),正常啟動服務
net start MySQL80

REM 8. 驗證
mysql -u root -p'NewStrongPass!2024' -e "SELECT VERSION();"

Linux (systemd) 詳細操作

# 1. 停止服務
systemctl stop mysqld
# 或 systemctl stop mysql / mariadb

# 2. 建立臨時 systemd override (最乾淨,無需改腳本)
systemctl edit mysqld
# 編輯器輸入:
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking $MYSQLD_OPTS
# 存檔離開

# 3. 重載並啟動
systemctl daemon-reload
systemctl start mysqld

# 4. 無密碼登入
mysql -u root

# 5. 重置密碼 (MySQL 8.0+)
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewStrongPass!2024';
EXIT;

# 6. 移除 override,恢復正常啟動
systemctl revert mysqld
systemctl daemon-reload
systemctl restart mysqld

# 7. 驗證
mysql -u root -p'NewStrongPass!2024' -e "SELECT USER();"

Linux (傳統 init.d / 手動) 操作

/etc/init.d/mysql stop
mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
# ... 同樣執行 ALTER USER ...
mysqladmin -u root shutdown
/etc/init.d/mysql start

macOS (Homebrew) 操作

brew services stop mysql
mysqld --skip-grant-tables --skip-networking &
mysql -u root
# ... ALTER USER ...
mysqladmin -u root shutdown
brew services start mysql

Docker 容器環境

# 1. 進入容器
docker exec -it mysql_container bash

# 2. 容器內操作 (同上 Linux 步驟)
# 或直接在宿主機掛載資料卷修改

# 3. 更簡單:重建容器時設定環境變數
docker run -d --name mysql_new \
  -e MYSQL_ROOT_PASSWORD=NewStrongPass!2024 \
  -v /my/data:/var/lib/mysql \
  mysql:8.0

MySQL 5.7 vs 8.0+ 密碼修改語法差異

常見失敗原因與對策

版本密碼欄位推薦語法
5.7authentication_stringUPDATE mysql.user SET authentication_string=PASSWORD('new') WHERE User='root'; FLUSH PRIVILEGES;
8.0+authentication_string (內部雜湊)ALTER USER 'root'@'localhost' IDENTIFIED BY 'new'; (自動處理雜湊與快取)
現象原因對策
ERROR 1290: The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement忘記執行 FLUSH PRIVILEGES;先 FLUSH PRIVILEGES; 再 ALTER USER
ERROR 1819: Your password does not satisfy the current policy requirements密碼強度不符 validate_password 政策使用強密碼 (長度、大小寫、數字、符號) 或暫時降低政策
修改後仍無法登入Host 欄位不匹配 (localhost vs 127.0.0.1 vs %)檢查 SELECT User, Host FROM mysql.user;,對應修改正確 Host
Windows 服務啟動失敗資料目錄權限、my.ini 錯誤、埠衝突查看 .err 日誌、mysqld --console 前台除錯

事後加固 (必做)

  1. mysql_secure_installation:移除匿名用戶、禁用 root 遠程、移除 test 資料庫
  2. 建立應用專用帳號,最小權限,強密碼
  3. 啟用 SSL、防火牆限制 3306 埠來源
  4. 密碼存入密碼管理器,配置輪換機制
copyright © 萬盛學電腦網 all rights reserved