How to configure multiple database and using failover in Codeigniter

假装没事ソ 提交于 2019-12-24 14:53:09

问题


I've used Codeigniter3.0.4 as the document provide I can't configure multiple database and using 'failover' => array().

I found an errros You have not selected a database type to connect to. on my website if I used this options

$active_group = 'default';
$active_record = TRUE;
$db['default']['failover'] = array(
    array(
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'com',
        'dbdriver' => 'mysqli',
        '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
    ),
    array(
        'hostname' => 'localhost2',
        'username' => 'root',
        'password' => '',
        'database' => 'ABC',
        'dbdriver' => 'mysqli',
        '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
    )
);

Please help how should I do.


回答1:


You have to set the default/group configuration of database. If default/group configuration fails then only it'll check the failover

$db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => '',
        'database' => 'database_name',
        'dbdriver' => 'mysqli',
        '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,
        'failover' => array(
            array(
                    'hostname' => 'localhost1',
                    'username' => '',
                    'password' => '',
                    'database' => '',
                    'dbdriver' => 'mysqli',
                    '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
            ),
            array(
                    'hostname' => 'localhost2',
                    'username' => '',
                    'password' => '',
                    'database' => '',
                    'dbdriver' => 'mysqli',
                    '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
            )
        )
);


来源:https://stackoverflow.com/questions/35459870/how-to-configure-multiple-database-and-using-failover-in-codeigniter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!