首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

MySQL root 密碼重置全流程:skip-grant-tables 實戰指南

     編輯:程式設計綜合問答

錯誤 `Access denied for user 'root'@'192.168.1.102'` 通常因密碼錯誤、權限表損壞或主機匹配失敗導致。本文提供通用的命令行重置方案。

前置條件

  • 擁有服務器作業系統管理員權限(Windows/Linux 均適用)。
  • 能夠重啟 MySQL 服務。

步驟詳解

  1. 修改配置跳過權限檢查
    # Windows: my.ini | Linux: /etc/my.cnf
    [mysqld]
    skip-grant-tables
  2. 重啟 MySQL 服務
    # Windows
    net stop mysql && net start mysql
    # Linux
    systemctl restart mysqld
  3. 免密登錄並修改密碼
    mysql -u root -p
    # 直接回車進入
    USE mysql;
    UPDATE user SET authentication_string=PASSWORD('new_password') WHERE User='root';
    FLUSH PRIVILEGES;
    EXIT;
  4. 恢復配置並重啟
    # 刪除 skip-grant-tables 行
    systemctl restart mysqld
  5. 驗證新密碼
    mysql -u root -pnew_password

注意事項

  • MySQL 5.7+ 密碼列為 `authentication_string`,舊版為 `password`。
  • 若報錯 `Table 'mysql.user' doesn't exist`,需執行 `mysql_upgrade`。
  • 生產環境建議創建專用業務賬號,禁用 root 遠程登錄。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved