遺忘 MySQL root 密碼時,通過 --skip-grant-tables 跳過權限驗證,即可無密碼登錄修改密碼。本文提供標準化 5 步操作流程,配合關鍵命令截圖說明。
pkill mysqld
# 或 systemctl stop mysql

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

mysql -u root
-- mysql> 內部執行:
USE mysql;
UPDATE user SET authentication_string=PASSWORD('NewPass123!') WHERE User='root';
FLUSH PRIVILEGES;
EXIT;

mysqladmin -u root -p'NewPass123!' shutdown

systemctl start mysql
mysql -u root -p'NewPass123!' -e "SELECT VERSION();"

--skip-networking 僅允許本地 Socket 連接,防止無授權訪問authentication_string;5.6 為 PasswordFLUSH PRIVILEGES 刷新內存權限表mysql_secure_installation 加固