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

忘記 MySQL Root 密碼?使用 skip-grant-tables 快速重置

     編輯:MySQL 資料庫相關知識

適用場景

MySQL Root 密碼遺失,無法正常登入管理資料庫。

四步重置流程

1. 編輯配置檔啟用跳過權限表

  • Windows:%MySQL安裝目录%\my.ini
  • Linux:/etc/my.cnf

在 [mysqld] 區段新增:

[mysqld]
skip-grant-tables

2. 重啟 MySQL 服務

# Windows
net stop MySQL
net start MySQL

# Linux
/etc/init.d/mysqld restart
# 或 systemctl restart mysqld

3. 無密碼登入並更新密碼

mysql -u root -p mysql
# 直接按 Enter 跳過密碼輸入
UPDATE user SET password=PASSWORD('123456') WHERE user='root';
FLUSH PRIVILEGES;
QUIT;

注意:MySQL 5.7+ user 表無 password 欄位,改為 authentication_string:

UPDATE user SET authentication_string=PASSWORD('123456') WHERE user='root';
FLUSH PRIVILEGES;

4. 恢復配置並重啟

  • 刪除 skip-grant-tables 行
  • 重啟 MySQL 服務
  • 使用新密碼 123456 登入驗證

操作總結圖解

  1. 殺掉 mysqld 進程
  2. 啟用 skip-grant-tables 啟動 MySQL
  3. 登入修改密碼
  4. 關閉 MySQL
  5. 移除 skip-grant-tables 並正常啟動

安全提醒

  • skip-grant-tables 期間任何人無需密碼即可完全控制資料庫,請在安全網路環境下操作,並儘快完成重置
  • 生產環境建議配置防火牆限制 3306 端口僅允許信任 IP 訪問
  • 重置後建議建立專用管理帳號,禁用 Root 遠程登入

此方法適用於 MySQL 5.0 至 8.0 所有版本,是 DBA 必備的急救技能之一。

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