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

Linux MySQL root 密碼找回:skip-grant-tables 4 步走標準流程

     編輯:MySQL 資料庫相關知識

通用、無需舊密碼的 root 密碼重置標準流程(適用 MySQL 5.6/5.7/8.0、MariaDB、Percona)。

步驟

  1. 停服務:systemctl stop mysqld 或 /etc/init.d/mysql stop。
  2. 跳過權限啟動:
    mysqld_safe --skip-grant-tables --skip-networking &
    --skip-networking 禁止遠程連入,安全第一。
  3. 無密登入修改:
    mysql -u root mysql
    UPDATE user SET authentication_string=PASSWORD('newpwd') WHERE User='root';
    MySQL 8.0 用 authentication_string,5.7 用 Password;
    或通用:ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpwd';
    FLUSH PRIVILEGES;
  4. 正常重啟:
    systemctl restart mysqld
    驗證:mysql -uroot -pnewpwd。

常見坑

  • 忘記 --skip-networking 導致裸奔 3306 端口。
  • MySQL 8.0 PASSWORD() 已移除,必須用 ALTER USER 或直接更新 authentication_string(需 FLUSH PRIVILEGES)。
  • SELinux/AppArmor 阻擋 mysqld_safe 寫入 socket/pid,查看 /var/log/mysqld.log 或 ausearch -m avc。
  • 多實例環境指定 --defaults-file=/etc/my.cnf.xxx。

備選:mysql_config_editor

若僅忘記應用賬號密碼,root 正常,可用 mysql_config_editor print --login-path=app 查看明文(需 root 權限)。

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