啟動 MySQL 時添加 --skip-grant-tables 參數,跳過權限表驗證,以無密碼 root 身份登錄,修改 mysql.user 表密碼後重啟生效。
service mysql stop
# 或
systemctl stop mysqld
# 若提示 unrecognized service,查找腳本位置
find / -name "mysql.server"
# 典型路徑
/usr/local/mysql/share/mysql/mysql.server stop
# 查找 mysqld_safe
find / -name "mysqld_safe"
# 後台啟動
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables &
# 或指定數據目錄
/usr/local/mysql/bin/mysqld_safe --skip-grant-tables --datadir=/data/mysql &
mysql -u root
# 進入 mysql> 提示符
USE mysql;
UPDATE user SET authentication_string = PASSWORD('NewPass123!') WHERE User = 'root';
-- 5.7.6+ 使用 authentication_string,舊版用 Password
FLUSH PRIVILEGES;
QUIT;
# 結束跳過授權的進程
killall mysqld
# 或
pkill -f mysqld
# 正常啟動
service mysql start
# 驗證新密碼
mysql -u root -p
# 輸入 NewPass123!
services.msc) 停止 MySQL 服務bin 目錄mysqld --skip-grant-tables --consolemysql -u root# config.inc.php
$cfg['Servers'][$i]['auth_type'] = 'cookie'; // 或 config
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'NewPass123!'; // auth_type=config 時需填
mysql_secure_installation:移除匿名用戶、禁止遠程 root、刪除 test 庫validate_password 策略:長度、大小寫、數字、特殊字符--skip-grant-tables,或修改宿主機 /etc/hosts 映射