1. 程式人生 > 實用技巧 >phpmyadmin 同時連線多個伺服器的資料庫

phpmyadmin 同時連線多個伺服器的資料庫

一、使用場景

一般來說,我們開發都會有測試環境和正式環境之分。當然,資料庫也是要分開的。如果能用phpmyadmin直接訪問兩臺伺服器上的mysql就好了。這就是需求。

二、解決方案

1、找到phpmyadmin資料夾下面的config.sample.inc.php,重新命名為config.inc.php。

2、開啟config.inc.php,我們會發現裡面有一些基本配置,是連線到localhost的。

3、如何連線單個遠端伺服器

//測試伺服器資料庫配置檔案
$cfg['Servers'][$i]['auth_type'] = 'config';
$cfg['Servers'][$i]['host'] = '遠端ip';
$cfg['Servers'][$i]['user'] = 'username'; $cfg['Servers'][$i]['password'] = 'pwd'; $cfg['Servers'][$i]['extension'] = 'mysqli'; $cfg['Servers'][$i]['AllowNoPassword'] = true; $cfg['Lang'] = '';

我是通過在配置檔案中直接寫入遠端ip等,實現了本地連線遠端的目的。

4、如何連線多個遠端伺服器

//這裡把遠端伺服器的使用者名稱,密碼等,拼接為一個多維陣列
$connect_hosts = array(
  '1'=>array
( "host" => "localhost", //本地伺服器 "user" => "root", "password" => "" ), '2' => array( "host" => "伺服器IP", //伺服器1 "user" => "username", "password" => "pwd" ), '3' => array( "host" => "伺服器ip", //伺服器2 "user" => "username", "password" => "pwd" ) );
for ($i=1;$i<=count($connect_hosts);$i++) { /* Authentication type */ $cfg['Servers'][$i]['auth_type'] = 'cookie'; /* Server parameters */ $cfg['Servers'][$i]['host'] = $connect_hosts[$i]['host']; //修改host $cfg['Servers'][$i]['connect_type'] = 'tcp'; $cfg['Servers'][$i]['compress'] = false; /* Select mysqli if your server has it */ $cfg['Servers'][$i]['extension'] = 'mysql'; $cfg['Servers'][$i]['AllowNoPassword'] = true; $cfg['Servers'][$i]['user'] = $connect_hosts[$i]['user']; //修改使用者名稱 $cfg['Servers'][$i]['password'] = $connect_hosts[$i]['password']; //密碼 /* rajk - for blobstreaming */ $cfg['Servers'][$i]['bs_garbage_threshold'] = 50; $cfg['Servers'][$i]['bs_repository_threshold'] = '32M'; $cfg['Servers'][$i]['bs_temp_blob_timeout'] = 600; $cfg['Servers'][$i]['bs_temp_log_threshold'] = '32M'; }

這裡需要注意:
(1)for迴圈的時候,i102i要從1開始,不要從0開始(2)配置檔案中有個′i = 0’,記得註釋掉。也就是註釋掉無關的程式碼

三、結果展示

1、本地輸入:localhost/phpmyadmin

這裡我們選擇自己要連線的伺服器即可。