啟動 MySQL 時加入 --skip-grant-tables 參數,暫時跳過權限驗證,以免密碼登入修改 mysql.user 表,最後重啟生效。
net stop mysqlbin 資料夾,執行 mysqld-nt --skip-grant-tables(視版本可能為 mysqld.exe),視窗會保持執行狀態mysql -u rootUSE mysql;
UPDATE user SET authentication_string = PASSWORD('新密碼') WHERE User = 'root';
FLUSH PRIVILEGES;
EXIT;
mysqld-nt 進程),執行 net start mysqlmysql -u root -p新密碼# 1. 停止服務
systemctl stop mysqld # 或 service mysqld stop
# 2. 安全模式啟動(背景執行)
mysqld_safe --skip-grant-tables &
# 3. 免密登入並改密
mysql -u root
USE mysql;
UPDATE user SET authentication_string = PASSWORD('新密碼') WHERE User = 'root';
FLUSH PRIVILEGES;
EXIT;
# 4. 重啟正常服務
pkill mysqld
systemctl start mysqld
authentication_string,舊版為 Password,請先 DESC user; 確認PASSWORD() 函數在 8.0 已移除,建議改用 ALTER USER 'root'@'localhost' IDENTIFIED BY '新密碼';Access denied,檢查 user.Host 是否包含 localhost、127.0.0.1、%#!/bin/bash
systemctl stop mysqld
mysqld_safe --skip-grant-tables &
sleep 3
mysql -u root -e "USE mysql; ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPass123!'; FLUSH PRIVILEGES;"
pkill mysqld
systemctl start mysqld
echo "Root 密碼已重置為 NewPass123!"