核心原理:以 --skip-grant-tables 啟動 MySQL,繼續權限檢查,空密碼登入 root,更新 mysql.user 表密碼欄位。
net stop mysqlcd C:\mysql5\binmysqld-nt --skip-grant-tables(保持視窗開啟)mysql -u rootUSE mysql;
UPDATE user SET password=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
QUIT;
mysqld-nt.exe 進程(工作管理員)net start mysql# 1. 停止服務
systemctl stop mysqld # 或 service mysql stop
# 2. 跳過授權啟動(背景執行)
mysqld_safe --skip-grant-tables &
# 3. 空密碼登入
mysql -u root
# 4. 更新密碼 (MySQL 5.7+ 使用 authentication_string)
UPDATE mysql.user SET authentication_string=PASSWORD('newpass') WHERE User='root';
FLUSH PRIVILEGES;
QUIT;
# 5. 重啟服務
systemctl restart mysqld
-- 8.0 不再使用 password 欄位,改用 authentication_string
ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpass';
FLUSH PRIVILEGES;
mysqladmin -u root -p password 'newpass'
# 提示輸入舊密碼
mysql_secure_installationUPDATE mysql.user SET Host='localhost' WHERE User='root';緊急情況:若
mysqld_safe不存在,直接用mysqld --skip-grant-tables --user=mysql &;容器環境請修改 entrypoint 傳遞參數。