首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

phpMyAdmin 安裝配置全攻略:空密碼登入、Cookie 認證與 Blowfish 密鑰設定

     編輯:MySQL 綜合教學

安裝前準備

  1. 下載:phpMyAdmin 官網 選擇最新穩定版(如 5.2.x)
  2. 解壓至 Web 目錄:/var/www/html/phpmyadmin 或自定義路徑
  3. 確保 PHP 擴展:mysqli、mbstring、zip、gd、json、session

核心配置檔:config.inc.php

複製 config.sample.inc.php 為 config.inc.php,編輯關鍵項:

1. 絕對 URI(生產環境建議設定)


$cfg['PmaAbsoluteUri'] = 'https://your-domain.com/phpmyadmin/';

2. 伺服器連線設定


$i = 0;
$i++;
$cfg['Servers'][$i]['host'] = 'localhost';        // 或 127.0.0.1
$cfg['Servers'][$i]['port'] = '3306';
$cfg['Servers'][$i]['socket'] = '';
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['auth_type'] = 'cookie';      // 推薦:Cookie 認證
$cfg['Servers'][$i]['user'] = 'root';             // 預設用戶
$cfg['Servers'][$i]['password'] = '';             // 空密碼由登入頁輸入
$cfg['Servers'][$i]['only_db'] = '';              // 空 = 顯示所有資料庫
$cfg['Servers'][$i]['AllowNoPassword'] = false;   // **生產必須 false**

3. 空密碼登入設定(僅開發/測試環境)


# 修改 libraries/config.default.php(舊版)或 config.inc.php
$cfg['Servers'][$i]['AllowNoPassword'] = true;   // 允許無密碼登入

⚠️ 警告:生產環境**嚴禁**啟用,極大安全風險!

4. Blowfish 密鑰(Cookie 認證必填)


$cfg['blowfish_secret'] = 'your-32-chars-random-string-here!'; // 32 字元以上隨機字串

用途:加密 Cookie 中的認證資訊。未設定會報錯:「配置文件現在需要絕密的短語密碼」。

生成方式:


# Linux 命令列
openssl rand -base64 32
# 或 PHP
php -r "echo base64_encode(random_bytes(32));"

5. 語言與介面


$cfg['DefaultLang'] = 'zh_TW';  // 繁體中文
$cfg['ThemeDefault'] = 'pmahomme';

多伺服器配置範例


$i = 0;
// 伺服器 1:本地主庫
$i++;
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['verbose'] = '本地主庫';

// 伺服器 2:遠端從庫
$i++;
$cfg['Servers'][$i]['host'] = '192.168.1.100';
$cfg['Servers'][$i]['auth_type'] = 'cookie';
$cfg['Servers'][$i]['verbose'] = '遠端從庫';

常見問題排查

現象原因解決
「無法連線 MySQL 伺服器」host/socket/port 錯誤、MySQL 未啟動、防火牆檢查 netstat -tlnp | grep 3306、systemctl status mysqld
「配置文件需要 blowfish_secret」未設定 $cfg['blowfish_secret']設定 32+ 字元隨機字串
登入後空白/報錯PHP session 目錄不可寫、mbstring 未裝chmod 777 /var/lib/php/session、安裝擴展
匯入大 SQL 失敗upload_max_filesize/post_max_size/max_execution_time 過小調整 php.ini、或用命令列 mysql < file.sql
中文亂碼連線字符集未設定配置 $cfg['DefaultCharset'] = 'utf8';、MySQL 端設定 utf8mb4

安全加固清單

  • ✅ AllowNoPassword = false
  • ✅ auth_type = 'cookie'(而非 config/http)
  • ✅ 設定強 blowfish_secret
  • ✅ 限制存取 IP:Nginx/Apache `allow 10.0.0.0/8; deny all;`
  • ✅ HTTPS 強制加密傳輸
  • ✅ 定期更新 phpMyAdmin 版本修補安全漏洞
  • ✅ 考慮雙因子認證(2FA)外掛

替代方案:Adminer(單檔案、輕量)


# 下載單一 PHP 檔案即可使用
wget https://github.com/vrana/adminer/releases/download/v4.8.1/adminer-4.8.1.php
mv adminer-4.8.1.php /var/www/html/adminer.php

功能相近、無需配置、記憶體佔用極低、適合臨時除錯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved