代码Igniter Failover Array

时间:2018-04-09 17:21:25

标签: php mysql codeigniter

我在故障转移连接方面不需要帮助,这个故障转移阵列可能出现了什么问题?我测试了备用数据库连接,效果很好。问题是当我关闭主服务器时,故障转移没有按预期连接。这直接来自Code Igniter文档。那么任何帮助排除故障的技巧或提示?

/*
 * 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
  )
);

1 个答案:

答案 0 :(得分:1)

你必须设置默认连接尝试这样的事情:

$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