萬盛學電腦網

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

Linux/Windows 下 MySQL Root 密碼找回進階:--skip-networking 安全模式與 init-file 優雅重置

為什麼單獨用 --skip-grant-tables 危險?

啟動後任何人均可無密碼連接數據庫(除非服務器屏蔽外網)。雖然重置時間短,但「不怕賊偷,就怕賊惦記」。

正確姿勢:雙參數啟動

mysqld_safe --skip-grant-tables --skip-networking &

--skip-networking 禁用 TCP/IP 連接,僅允許本地 Unix Socket/管道,極大降低風險。

更優雅:init-file 一次性重置(僅需重啟一次)

  1. 建立 SQL 文件 /path/to/init.sql:
    UPDATE mysql.user SET Password=PASSWORD('newpass') WHERE User='root' AND Host='localhost';
    FLUSH PRIVILEGES;
    
  2. 停止服務並啟動:
    /etc/init.d/mysql stop
    mysqld_safe --init-file=/path/to/init.sql &
  3. 密碼已重置,立即刪除 init.sql 防洩露。

Windows 具體操作

  1. 管理員 CMD:net stop mysql
  2. 進入 D:\mysql5\bin(根據實際路徑):
    mysqld-nt --skip-grant-tables --skip-networking
  3. 新開 CMD:
    mysqladmin -uroot flush-privileges password "newpassword"
    mysqladmin -u root -p shutdown(輸入新密碼)
  4. net start mysql

參考資料

官方文檔:How to Reset the Root Password。參數亦可寫入 my.cnf [mysqld] 區段。

copyright © 萬盛學電腦網 all rights reserved