Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux MySQL root 密碼遺忘重置完整步驟:skip-grant-tables 法

編輯:Linux 教學

遺忘 MySQL root 密碼時,可透過跳過權限表機制重置。操作期間資料庫無密碼保護,請先封鎖對外連線或拔網路線。

步驟

  1. 修改設定檔
    編輯 /etc/my.cnf,在 [mysqld] 區段加入:
    skip-grant-tables
  2. 重啟 MySQL
    /etc/init.d/mysqld restart 或 systemctl restart mysqld
  3. 免密登入並更新密碼
    mysql -u root
    USE mysql;
    UPDATE user SET Password = PASSWORD('new-password') WHERE User = 'root';
    FLUSH PRIVILEGES;
    QUIT;
  4. 還原設定檔
    移除 skip-grant-tables,儲存。
  5. 再次重啟 MySQL
    /etc/init.d/mysqld restart
  6. 驗證新密碼
    mysql -u root -p'new-password'

MySQL 5.7+ / 8.0 注意

  • Password 欄位改為 authentication_string:
    UPDATE user SET authentication_string = PASSWORD('new-password') WHERE User = 'root';
  • 或使用 ALTER USER 'root'@'localhost' IDENTIFIED BY 'new-password'; (需正常權限模式下執行)。

安全提醒

  • 重置完成後立即移除 skip-grant-tables 並重啟。
  • 檢查是否有匿名帳號、測試帳號,執行 mysql_secure_installation 強化安全。

此法適用於多數 MySQL/MariaDB/Percona Server 版本。

Copyright © Windows教程網 All Rights Reserved