phpMyAdmin 是最流行的 MySQL/MariaDB Web 管理介面,以 PHP 撰寫,提供直觀的資料庫、資料表、資料操作、SQL 執行、匯入匯出、使用者權限管理等功能。
composer create-project phpmyadmin/phpmyadmin --repository-url=https://www.phpmyadmin.net/packages.json --no-dev
all-languages 版本)/var/www/html/phpmyadminconfig.sample.inc.php 為 config.inc.php# Ubuntu/Debian
apt update && apt install phpmyadmin
# 安裝過程會詢問 Web 伺服器 (apache2/lighttpd) 和資料庫設定
# CentOS/RHEL (EPEL)
yum install epel-release && yum install phpmyadmin
<?php
// 基礎設定
$cfg['blowfish_secret'] = 'your-32-char-random-string-here'; // Cookie 加密金鑰 (32 字元)
$cfg['DefaultLang'] = 'zh_TW'; // 預設語言
$cfg['ServerDefault'] = 1; // 預設伺服器索引
$cfg['UploadDir'] = ''; // 上傳目錄 (空為停用)
$cfg['SaveDir'] = ''; // 儲存目錄
// 伺服器群組設定 (支援多伺服器)
$i = 0;
$i++;
$cfg['Servers'][$i]['auth_type'] = 'cookie'; // 驗證方式:cookie/http/config/signon
$cfg['Servers'][$i]['host'] = '127.0.0.1'; // 主機
$cfg['Servers'][$i]['port'] = '3306'; // 埠號
$cfg['Servers'][$i]['socket'] = ''; // Unix socket
$cfg['Servers'][$i]['compress'] = false; // 壓縮傳輸
$cfg['Servers'][$i]['controluser'] = 'pma_control'; // 控制使用者 (進階功能)
$cfg['Servers'][$i]['controlpass'] = 'control_pass';
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin'; // phpMyAdmin 設定儲存資料庫
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
$cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
$cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
$cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';
// 安全性設定
$cfg['LoginCookieValidity'] = 1440; // Cookie 有效期 (秒)
$cfg['LoginCookieStore'] = 0; // 瀏覽器關閉即失效
$cfg['ForceSSL'] = true; // 強制 HTTPS
$cfg['CheckConfigurationPermissions'] = true; // 檢查配置檔權限
$cfg['CaptchaLoginPublicKey'] = '...'; // reCAPTCHA 公鑰
$cfg['CaptchaLoginPrivateKey'] = '...'; // reCAPTCHA 私鑰
$cfg['TwoFactor'] = 'totp'; // 兩步驟驗證 (TOTP)
?>
| 模式 | auth_type 值 | 特點 | 適用場景 |
|---|---|---|---|
| Cookie | cookie | 表單登入、Session 管理、支援登出 | 生產環境首選 |
| HTTP Basic | http | 瀏覽器原生對話框、無法登出 | 內網、簡單環境 |
| Config | config | 帳密寫在配置檔、免登入 | 極不推薦,僅本機開發 |
| Signon | signon | 整合既有認證系統 (SSO) | 企業整合 |
| 措施 | 實作方式 | 風險等級 |
|---|---|---|
| 修改預設路徑 | 將目錄改為隨機名稱,如 /admin_pma_x7k9/ | 高 |
| 強制 HTTPS | 配置檔 $cfg['ForceSSL'] = true + Web 伺服器強制導向 | 高 |
| IP 白名單限制 | Nginx/Apache allow 192.168.1.0/24; deny all; | 高 |
| HTTP Basic Auth 雙重保護 | Web 伺服器層級加一層驗證 | 中 |
| 啟用兩步驟驗證 (2FA) | 配置 $cfg['TwoFactor'] = 'totp' | 中 |
| 限制登入失敗次數 | 配合 Fail2Ban 或 phpMyAdmin 內建 $cfg['LoginCookieValidity'] | 中 |
| 定期更新版本 | Composer composer update phpmyadmin/phpmyadmin | 高 |
| 隱藏版本資訊 | $cfg['VersionCheck'] = false | 低 |
chmod 640 config.inc.php && chown www-data:www-data config.inc.phpcontroluser 權限、資料庫 phpmyadmin 是否存在、匯入 sql/create_tables.sqlblowfish_secret 設定、Session 目錄可寫php.ini:upload_max_filesize、post_max_size、max_execution_time、memory_limit;或使用命令列 mysql < dump.sqlphpMyAdmin 功能強大但攻擊面大,生產環境務必實施多層防護,或改用桌面工具透過 SSH 通道管理,將管理介面完全隔離於公網之外。