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

MySQL Root 密碼遺忘重置完整步驟

     編輯:MySQL 資料庫相關知識

適用情境

忘記 MySQL root 密碼,且擁有作業系統最高權限(Linux root / Windows Administrator)。

重置流程(Linux 為例)

  1. 停止 MySQL 服務
    /etc/init.d/mysql stop
    # 或
    ps aux | grep mysql
    kill -TERM 進程ID
  2. 跳過權限表啟動
    mysqld_safe --skip-grant-tables &

    此模式下不載入授權表,任何人可無密碼登入。

  3. 無密碼登入並修改密碼
    mysql mysql
    UPDATE user SET Password=PASSWORD('新密碼') WHERE User='root';
    FLUSH PRIVILEGES;
  4. 正常重啟驗證
    /etc/init.d/mysql restart
    mysql -uroot -p新密碼

Windows 環境差異

  • 停止服務:net stop mysql 或工作管理員結束 mysqld.exe。
  • 跳過權限啟動:mysqld --skip-grant-tables(於安裝目錄 bin 下執行)。
  • 其餘步驟相同。

安全提醒

  • --skip-grant-tables 期間資料庫完全無保護,務必在可信網路/本機操作,完成後立即重啟正常模式。
  • MySQL 5.7+ Password 欄位改為 authentication_string,語法調整為:
    UPDATE user SET authentication_string=PASSWORD('新密碼') WHERE User='root';
  • MySQL 8.0 建議使用 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密碼';。

測試環境

MySQL 5.0 / Fedora Core 3 驗證通過。

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