WAMP/XAMPP 等集成環境修改 MySQL root 密碼後,phpMyAdmin 登入頁報錯 #1045 Cannot log in to the MySQL server。根因:phpMyAdmin 配置文件 config.inc.php 中的密碼未同步更新。本文提供定位與修復步驟。
瀏覽器訪問 phpMyAdmin 顯示:
#1045 - Access denied for user 'root'@'localhost' (using password: YES)
並伴隨 mysqli_real_connect() 警告。
phpMyAdmin 使用配置文件中的帳密連接 MySQL:
// config.inc.php
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = 'old_password'; // 未同步更新
$cfg['Servers'][$i]['auth_type'] = 'config'; // 配置文件認證模式
MySQL 密碼變更後,phpMyAdmin 仍嘗試舊密碼,導致認證失敗。
wamp/apps/phpmyadmin{版本}/config.inc.phpxampp/phpMyAdmin/config.inc.php/etc/phpmyadmin/config.inc.php 或 /usr/share/phpmyadmin/config.inc.php
$cfg['Servers'][$i]['password'] = 'new_mysql_root_password';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = ''; // 留空,登入頁輸入
$cfg['blowfish_secret'] = 'your_random_32chars_string'; // 必填,用於 Cookie 加密
blowfish_secret 必須設定且足夠複雜$cfg['Servers'][$i] (多實例配置時 $i 可能非 1)user 表 host 欄位是否允許 localhost/127.0.0.1/::1腳本化密碼變更流程:
#!/bin/bash
# change_mysql_pass.sh
NEW_PASS=$1
mysql -u root -p"$OLD_PASS" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '$NEW_PASS';"
sed -i "s/\$cfg\['Servers'\]\[\$i\]\['password'\] = '.*'/\$cfg['Servers'][\$i]['password'] = '$NEW_PASS'/" /path/to/config.inc.php
echo "MySQL & phpMyAdmin 密碼已同步更新"
集成環境便利但隱藏配置同步陷阱,建立「修改密碼 → 同步所有客戶端配置」肌肉記憶,避免無謂排查時間。