首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

忘記 MySQL root 密碼?Windows/Linux 通用跳過權限表重置教學

     編輯:MySQL 綜合教學

核心原理

以 --skip-grant-tables 參數啟動 MySQL,暫時繞過權限驗證,以空密碼登入 root,更新 mysql.user 表的 password 欄位,再重啟服務生效。

Windows 環境操作步驟

  1. 以系統管理員開啟 CMD,停止服務:
    net stop mysql
  2. 切換到 MySQL 安裝目錄的 bin 資料夾(如 C:\MySQL\bin),啟動跳過權限的執行檔:
    mysqld-nt.exe --skip-grant-tables
    (或 mysqld.exe --skip-grant-tables,視版本而定)
  3. 另開一個 CMD 視窗,直接輸入 mysql 免密登入。
  4. 執行重置語句:
USE mysql;
UPDATE user SET password=PASSWORD('新密碼') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;
  1. 回到第一個視窗,用 Ctrl+C 或工作管理員結束 mysqld-nt.exe 進程。
  2. 正常啟動服務:net start mysql
  3. 用新密碼登入驗證:mysql -u root -p新密碼

Linux 環境操作步驟

  1. 停止 MySQL:
    service mysql stop 或 systemctl stop mysqld 或 killall -TERM mysqld
  2. 跳過權限啟動(背景執行):
    mysqld_safe --skip-grant-tables &
    (或 /usr/bin/mysqld --skip-grant-tables &)
  3. 免密登入:mysql -u root
  4. 同樣執行:
USE mysql;
UPDATE user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
-- MySQL 5.7+ 欄位名為 authentication_string
FLUSH PRIVILEGES;
EXIT;
  1. 停止臨時進程:killall mysqld 或 service mysql stop
  2. 正常啟動:service mysql start

注意事項

  • MySQL 5.7/8.0 密碼欄位改為 authentication_string,舊版為 password,請先 DESC mysql.user; 確認。
  • 生產環境建議重置後立即檢查 SELECT User, Host, plugin FROM mysql.user; 確認無匿名帳號。
  • 若啟用 skip-grant-tables 仍無法登入,檢查 my.cnf 是否有 skip-networking 或 bind-address=127.0.0.1 限制。

MySQL 重置密碼流程圖

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