萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 資料庫 >> MySQL 教學

MySQL 修改密碼後 phpMyAdmin 無法登入?配置文件同步更新指南

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 仍嘗試舊密碼,導致認證失敗。

修復步驟

  1. 定位配置文件:
    WAMP:wamp/apps/phpmyadmin{版本}/config.inc.php
    XAMPP:xampp/phpMyAdmin/config.inc.php
    Linux 手動安裝:/etc/phpmyadmin/config.inc.php 或 /usr/share/phpmyadmin/config.inc.php
  2. 編輯文件,同步更新密碼:
$cfg['Servers'][$i]['password'] = 'new_mysql_root_password';
  1. 保存文件,瀏覽器重新整理 phpMyAdmin,即可正常登入

進階配置:Cookie 認證模式 (推薦生產環境)

$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)
  • 密碼含特殊字符需正確轉義 (單引號字串中單引號雙寫或用雙引號)
  • 瀏覽器清除快取/Cookie 後重試
  • 檢查 MySQL 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 密碼已同步更新"

集成環境便利但隱藏配置同步陷阱,建立「修改密碼 → 同步所有客戶端配置」肌肉記憶,避免無謂排查時間。

copyright © 萬盛學電腦網 all rights reserved