XAMPP 遠端資料庫管理

XAMPP 的資料庫管理介面為 phpMyAdmin,但預設只能接受 localhost 連線,並且也只能連 localhost 的資料庫。想要遠端連進資料庫有兩種方式,一種是設定遠端的 Apache Web Server 讓 phpMyAdmin 可以接受遠端連線,這種作法本地端不需要安裝 XAMPP;另外一種是設定本地端的 phpMyAdmin 可以連線遠端資料庫,這種作法本地端就需要安裝 XAMPP,而遠端只需要安裝 MySQL 或 MariaDB。兩種作法都會產生資安問題,教學上因分組需要而開啟此功能,正式上線系統請注意資安,最好不要開放遠端登入

第一種作法:讓本地端瀏覽器連遠端 phpMyAdmin

遠端電腦找到 httpd-xampp.conf,此檔案在 macOS 是位於 /Applications/XAMPP/xamppfiles/etc/extra 資料夾,Windows 在 C:\xampp 資料夾中請自行找找。將此檔案中下面紅色部分換成藍色,然後 Apache 重新啟動即可。

<Directory "/Applications/XAMPP/xamppfiles/phpmyadmin">
    AllowOverride AuthConfig Limit
    Require local
    ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>

改為

<Directory "/Applications/XAMPP/xamppfiles/phpmyadmin">
    AllowOverride AuthConfig Limit
    Require all granted
    ErrorDocument 403 /error/XAMPP_FORBIDDEN.html.var
</Directory>

第二種作法:讓本地端 phpMyAdmin 連線遠端資料庫

首先遠端資料庫必須有可以允許遠端登入的帳號,root 帳號預設不允許遠端登入,所以要嘛修改 root 權限,不然就新增一個可以遠端登入的帳號,當然建議後者。另外防火牆需要開啟埠號 3306。

資料庫權限開啟後,找到本地端 XAMPP 中的 phpMyAdmin 資料夾下的 config.inc.php 檔案,macOS 位於 /Applications/XAMPP/xamppfiles/phpmyadmin 資料夾,Windows 在 C:\xampp 資料夾中請自行找找。開啟此檔後找到下面這幾行,首先將綠色的三行註解起來,然後藍色該行註解拿掉。

$i++;
/* Authentication type */
// $cfg['Servers'][$i]['auth_type'] = 'config';
// $cfg['Servers'][$i]['user'] = 'root';
// $cfg['Servers'][$i]['password'] = ''; 
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;

此時只要將藍色該行後面的 localhost 換成想要連線資料庫所在的電腦 IP 即可。但這裡可以在 $i++ 前面再加一個區段,這樣在 phpMyAdmin 的第一個畫面就可以選擇要連線遠端資料庫還是本地資料庫,操作上比較方便,建議改成如下,注意有兩個 $i++。

$i++;
$cfg['Servers'][$i]['host'] = '192.168.0.20';
$cfg['Servers'][$i]['compress'] = false;

$i++;
/* Authentication type */
// $cfg['Servers'][$i]['auth_type'] = 'config';
// $cfg['Servers'][$i]['user'] = 'root';
// $cfg['Servers'][$i]['password'] = ''; 
/* Server parameters */
$cfg['Servers'][$i]['host'] = 'localhost';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = true;

現在瀏覽器連到本地端 phpMyAdmin 的第一個畫面就會跳出需要輸入帳號密碼,以及選擇連線哪一個資料庫了,畫面如下。

截圖 2023-02-01 21.44.05

資料庫備份指令

開啟命令提示字元或終端機,執行下面 mysqldump 指令,備份檔名稱可以任意,例如 backup.sql。還原時將此檔案內容複製貼上到 phpMyAdmin 的 SQL 頁籤中去執行即可。

Windows

C:\xampp\mysql\bin\mysqldump -u root  -B AddressBook -R -r backup.sql

Mac

/Applications/XAMPP/bin/mysqldump -u root  -B AddressBook -R -r backup.sql

One thought on “XAMPP 遠端資料庫管理

  1. Pingback: Google Cloud Shell 與 XAMPP 的完美結合 – 研蘋果

發表迴響