本地 phpMyAdmin 管理遠程 MySQL 伺服器,需修改配置檔並開啟遠程存取權限。本文提供完整步驟。
# 通常位於:
/usr/share/phpmyadmin/config.inc.php
# 或
/var/www/html/phpmyadmin/config.inc.php
# 或 Windows 下
C:\xampp\phpMyAdmin\config.inc.php
$cfg['Servers'][$i]['host'] = '192.168.1.100'; // 遠程 MySQL IP
$cfg['Servers'][$i]['port'] = '3306'; // 端口,預設 3306
$cfg['Servers'][$i]['auth_type'] = 'cookie'; // 認證方式:cookie/http/config/signon
mysql -u root -p -h 192.168.1.100
-- 允許任意 IP 連線 (開發/測試環境)
CREATE USER 'remote_user'@'%' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON *.* TO 'remote_user'@'%' WITH GRANT OPTION;
-- 僅允許特定 IP (生產環境推薦)
CREATE USER 'remote_user'@'192.168.1.%' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON mydb.* TO 'remote_user'@'192.168.1.%';
FLUSH PRIVILEGES;
firewall-cmd --permanent --add-port=3306/tcp
firewall-cmd --reload
ufw allow 3306/tcp
ufw reload
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
service iptables save
vim /etc/my.cnf
[mysqld]
bind-address = 0.0.0.0 # 監聽所有介面
# 或指定具體 IP
# bind-address = 192.168.1.100
skip-name-resolve # 禁用 DNS 反向解析,加速連線
systemctl restart mysqld
| 問題 | 原因 | 解決 |
|---|---|---|
| 連線超時 | 防火牆阻擋 | 開放 3306 端口 |
| Host 'x.x.x.x' not allowed | 用戶無遠程權限 | GRANT ... TO user@'%' 或具體 IP |
| Authentication plugin 錯誤 | MySQL 8.0 預設 caching_sha2_password | 用戶指定 mysql_native_password |
| 中文亂碼 | 字符集不一致 | 配置 utf8mb4、設定連線字符集 |
ALTER USER 'remote_user'@'%'
IDENTIFIED WITH mysql_native_password BY 'strong_password';
完成上述配置,本地 phpMyAdmin 即可安全管理遠程 MySQL 伺服器。