CodeIgniter Check the connection to multi database

2019-02-28 12:09发布

问题:

I try to connect to multi MySQL database in CI
if the connection to the first DB fails, I try to connect to the second,
I use this code from CI forum: http://ellislab.com/forums/viewthread/224522/#1030449

$db_obj = $this->database->load('xxx',TRUE);
  $connected = $db_obj->initialize();
  if (!$connected) {
     $db_obj = $this->database->load('yyy',TRUE);
  }

it works fine on local host, it tries to connect to 1st DB and fails, then directly connect to the 2nd DB (without any long time -- transparency)
but
when I moved my code to online server, it took a long time in trying connect to 1st DB, then (after this timeout) it tried to connect 2nd DB and succeeded
how can I remove this waiting time? I need it directly tries to connect to other DB in fail case

my DB config looks like:

$db['DB1']['hostname'] = '1.1.1.1';
$db['DB1']['username'] = 'test';
$db['DB1']['password'] = '123456';
$db['DB1']['database'] = 'db1';
$db['DB1']['dbdriver'] = 'mysql';
$db['DB1']['dbprefix'] = '';
$db['DB1']['pconnect'] = TRUE;
$db['DB1']['db_debug'] = FALSE;
$db['DB1']['cache_on'] = FALSE;
$db['DB1']['cachedir'] = '';
$db['DB1']['char_set'] = 'utf8';
$db['DB1']['dbcollat'] = 'utf8_general_ci';
$db['DB1']['swap_pre'] = '';
$db['DB1']['autoinit'] = FALSE;
$db['DB1']['stricton'] = FALSE;


$db['DB2']['hostname'] = '1.1.1.1';
$db['DB2']['username'] = 'test';
$db['DB2']['password'] = '123456';
$db['DB2']['database'] = 'db2';
$db['DB2']['dbdriver'] = 'mysql';
$db['DB2']['dbprefix'] = '';
$db['DB2']['pconnect'] = TRUE;
$db['DB2']['db_debug'] = FALSE;
$db['DB2']['cache_on'] = FALSE;
$db['DB2']['cachedir'] = '';
$db['DB2']['char_set'] = 'utf8';
$db['DB2']['dbcollat'] = 'utf8_general_ci';
$db['DB2']['swap_pre'] = '';
$db['DB2']['autoinit'] = FALSE;
$db['DB2']['stricton'] = FALSE;


Many thanks!