CI多个数据库连接到不同的服务器


CI multiple database connection with different server

我有一个应用程序需要连接到2个不同的数据库和不同的服务器。除了在database。php中设置,还有其他需要设置的吗?我在我的代码中写了这个来连接到两个db:

$provinsi_db = $this->load->database('provinsi', true); //this is from another server
$local = $this->load->database('default', true); //this one is in my localhost

但是当我试图从服务器db中选择数据时,什么都没有发生。从本地数据库选择数据是没有问题的。有人能帮我吗?

这是我的数据库。php:

$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username_local';
$db['default']['password'] = 'password_local';
$db['default']['database'] = 'db_local';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['provinsi']['hostname'] = 'xxx.xxx.xxx.xxx';
$db['provinsi']['username'] = 'username_foreign';
$db['provinsi']['password'] = 'password_foreign';
$db['provinsi']['database'] = 'db_foreign';
$db['provinsi']['dbdriver'] = 'mysql';
$db['provinsi']['dbprefix'] = '';
$db['provinsi']['pconnect'] = TRUE;
$db['provinsi']['db_debug'] = TRUE;
$db['provinsi']['cache_on'] = FALSE;
$db['provinsi']['cachedir'] = '';
$db['provinsi']['char_set'] = 'utf8';
$db['provinsi']['dbcollat'] = 'utf8_general_ci';
$db['provinsi']['swap_pre'] = '';
$db['provinsi']['autoinit'] = TRUE;
$db['provinsi']['stricton'] = FALSE;

我必须在服务器的'主机名'中包含端口吗?

我的整个数据库设置示例

//My database.php

/* API Database Connection */
$active_group = 'apidb';
$active_record = TRUE;
$db['apidb']['hostname'] = 'localhost';
$db['apidb']['username'] = 'user_name';
$db['apidb']['password'] = 'pass_word';
$db['apidb']['database'] = 'db_name';
$db['apidb']['dbdriver'] = 'mysql';
$db['apidb']['dbprefix'] = '';
$db['apidb']['pconnect'] = FALSE;
$db['apidb']['db_debug'] = TRUE;
$db['apidb']['cache_on'] = FALSE;
$db['apidb']['cachedir'] = '';
$db['apidb']['char_set'] = 'utf8';
$db['apidb']['dbcollat'] = 'utf8_general_ci';
$db['apidb']['swap_pre'] = '';
$db['apidb']['autoinit'] = TRUE;
$db['apidb']['stricton'] = FALSE;

/* Site Database Connection */
$active_group = 'sitedb';
$active_record = TRUE;
$db['sitedb']['hostname'] = 'localhost';
$db['sitedb']['username'] = 'user_name';
$db['sitedb']['password'] = 'pass_word';
$db['sitedb']['database'] = 'db_name';
$db['sitedb']['dbdriver'] = 'mysql';
$db['sitedb']['dbprefix'] = '';
$db['sitedb']['pconnect'] = FALSE;
$db['sitedb']['db_debug'] = TRUE;
$db['sitedb']['cache_on'] = FALSE;
$db['sitedb']['cachedir'] = '';
$db['sitedb']['char_set'] = 'utf8';
$db['sitedb']['dbcollat'] = 'utf8_general_ci';
$db['sitedb']['swap_pre'] = '';
$db['sitedb']['autoinit'] = TRUE;
$db['sitedb']['stricton'] = FALSE;

在控制器中,加载数据库

$this->sitedb = $this->load->database('sitedb', TRUE);
$this->apidb = $this->load->database('apidb', TRUE);
在model中,你可以调用
$this->apidb->query('your query');

$this->sitedb->query('your query');

尝试在您的配置database.php

$db['provinsi']['pconnect'] = FALSE;
$db['default']['pconnect'] = FALSE;