services.msc),停止 MySQL 服務C:\mysql-init.txt,單行內容:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPassword');
--init-file 啟動服務器:
# 若為 Zip 解壓安裝
C:\mysql\bin\mysqld --init-file=C:\mysql-init.txt --console
# 若為 MSI 安裝(需指定 defaults-file)
"C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqld.exe"
--defaults-file="C:\Program Files\MySQL\MySQL Server 5.7\my.ini"
--init-file=C:\mysql-init.txt --console
Ctrl+C 停止,刪除 mysql-init.txtsystemctl stop mysqld 或 service mysql stop--skip-grant-tables --skip-networking 啟動:
mysqld_safe --skip-grant-tables --skip-networking &
mysql -u root
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPassword';
-- 或 5.7.5 以前
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('MyNewPassword') WHERE User='root' AND Host='localhost';
mysql> FLUSH PRIVILEGES;
mysql> EXIT;
systemctl restart mysqldPASSWORD() 函數已移除,必須用 ALTER USER ... IDENTIFIED BYmysql.user 表結構變更,authentication_string 取代 password 列caching_sha2_password 插件,客戶端需 8.0+ 版本mysql -u root -p'MyNewPassword' -e "SELECT VERSION();"
安全提醒: 重置後務必檢查是否存在匿名用戶(
User='')、root@'%'遠程帳號、測試庫test,執行mysql_secure_installation清理。