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

MySQL 忘記 root 密碼重置完整教學(Windows/Linux)

     編輯:MySQL 資料庫相關知識

核心原理

以 --skip-grant-tables 參數啟動 MySQL,跳過權限驗證,以空密碼 root 登入修改密碼,再重啟正常模式。


方法一:通用步驟(Windows/Linux 通用)

  1. 停止 MySQL 服務
    # Windows
    net stop mysql
    
    # Linux
    systemctl stop mysqld
    # 或
    service mysql stop
  2. 跳過權限啟動
    # Windows(於 bin 目錄下)
    mysqld --skip-grant-tables
    
    # Linux
    mysqld_safe --skip-grant-tables &
  3. 另開終端機,空密碼登入
    mysql -u root
  4. 修改密碼並刷新權限
    USE mysql;
    UPDATE user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
    FLUSH PRIVILEGES;
    QUIT;
    MySQL 5.7+ 欄位為 authentication_string,舊版為 Password。
  5. 重啟 MySQL 正常模式
    # Windows
    net start mysql
    
    # Linux
    systemctl start mysqld

方法二:Windows 專用 — mysqladmin 指令

# 1. 跳過權限啟動 mysqld
mysqld --skip-grant-tables

# 2. 另開視窗執行
mysqladmin -u root flush-privileges password "newpassword"
mysqladmin -u root -p shutdown  # 會提示輸入新密碼

# 3. 正常啟動 MySQL 服務

方法三:修改設定檔 my.cnf / my.ini

  1. 編輯設定檔(Windows: my.ini / Linux: /etc/my.cnf),在 [mysqld] 區段加入:
    skip-grant-tables
  2. 重啟 MySQL 服務
  3. 無密碼登入修改密碼(同方法一步驟 3-4)
  4. 移除 skip-grant-tables,重啟服務

Linux 環境補充指令

# 若 mysqld 正在運行,先終止
killall -TERM mysqld

# 安全模式啟動
mysqld_safe --skip-grant-tables &

# 登入修改
mysql -u root
UPDATE mysql.user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
FLUSH PRIVILEGES;

# 結束進程並正常啟動
killall mysqld
systemctl start mysqld
✅ 完成後請務必: 移除 skip-grant-tables 設定、重啟服務、驗證新密碼可正常登入。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved