1

I'm not needing help with failover connection, just what might be wrong with this failover array? I have tested the alternate db connection and works great. Issue is when I turn off primary, failover does not connect as it should. This is straight from Code Igniter Documentation. So any tricks or tips to help troubleshoot?

/*
 * Mysql Driver
*/
$db['mysql']['hostname'] = getenv('DB_HOST');
$db['mysql']['username'] = getenv('DB_USERNAME');
$db['mysql']['password'] = getenv('DB_PASSWORD');
$db['mysql']['database'] = getenv('DB_DATABASE');
$db['mysql']['dbdriver'] = 'mysql';
$db['mysql']['dbprefix'] = '';
$db['mysql']['pconnect'] = FALSE;
$db['mysql']['db_debug'] = FALSE;
$db['mysql']['cache_on'] = FALSE;
$db['mysql']['cachedir'] = '';
$db['mysql']['char_set'] = 'utf8';
$db['mysql']['dbcollat'] = 'utf8_general_ci';
$db['mysql']['swap_pre'] = '';
$db['mysql']['autoinit'] = TRUE;
$db['mysql']['stricton'] = FALSE;
$db['mysql']['failover'] = array(
    array(
    'hostname' => getenv('DB_HOST2'),
    'username' => getenv('DB_USERNAME2'),
    'password' => getenv('DB_PASSWORD2'),
    'database' => getenv('DB_DATABASE2'),
    'dbdriver' => 'mysql',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE
  )
);
  • mysql is deprecated and you should use mysqli instead. see php.net and https://stackoverflow.com/questions/7250356/how-to-install-mysqli and also CI docs https://www.codeigniter.com/user_guide/database/configuration.html – Vickel Apr 09 '18 at 18:37
  • using mysqli doesnt make the array work either...the docs you link to are exactly as my code above, the ones I referenced. this is CI 3.0 – user3691635 Apr 09 '18 at 18:54
  • any error messages? – Vickel Apr 09 '18 at 19:05
  • try to turn off db_debug in your failover also set pc_connect to false – Vickel Apr 09 '18 at 19:06
  • No errors, other than it just fails the querys ran. I have an AWS RDS instance I shut down to test the failover to another cluster. Either set as primary connect fine, its just when it goes to the failover it seems to drag and then times out. Trying your requested changes now and testing – user3691635 Apr 09 '18 at 19:18
  • Still no go, the failover check in CI looks correct as well. Still not sure how to force the secondary connection or view it if the primary fails. I guess I could hack CI's core but would rather use it the way its made if in fact it works. – user3691635 Apr 09 '18 at 19:31
  • see this https://github.com/bcit-ci/CodeIgniter/issues/5114 and maybe contact narfbg – Vickel Apr 09 '18 at 19:45
  • Use mysqli not mysql –  Apr 09 '18 at 21:14

1 Answers1

1

You have to set default connection try something like this :

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    .............
);

$db['default']['failover'] = array(
            array(
                    'hostname' => 'localhost1',
                    'username' => ''
                    ..........
            ),
            array(
                    'hostname' => 'localhost2',
                    'username' => ''
                    ..........
            )
);

https://codeigniter.com/user_guide/database/configuration.html

mohamedvall
  • 144
  • 2
  • 4