【问题标题】:how to set multiple databases with same localhost, root and password如何使用相同的localhost,root和密码设置多个数据库
【发布时间】:2018-01-01 03:43:11
【问题描述】:

应用程序/配置/ 数据库.php, 如何使用相同的本地主机设置另一个数据库, 请帮忙!提前致谢

$active_group = 'default';
$active_record = TRUE;
$db['default'] = array(
 'dsn' => '',
 'hostname' => 'localhost',
 'username' => 'root',
 'password' => '',
 'database' => 'business_permit',
 'dbdriver' => 'mysqli',
 'dbprefix' => '',
 'pconnect' => FALSE,
 'db_debug' => (ENVIRONMENT !== 'production'),
 'cache_on' => FALSE,
 'cachedir' => '',
 'char_set' => 'utf8',
 'dbcollat' => 'utf8_general_ci',
 'swap_pre' => '',
 'encrypt' => FALSE,
 'compress' => FALSE,
 'stricton' => FALSE,
 'failover' => array(),
 'save_queries' => TRUE
);

【问题讨论】:

标签: php sql codeigniter


【解决方案1】:

最好的方法是使用不同的数据库组。如果您想像往常一样继续使用主数据库 ($this->db),只需关闭辅助数据库的持久连接配置选项。只有主数据库才能使用持久连接:

主数据库

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$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;

辅助数据库(注意 pconnect 设置为 false)

$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = FALSE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;

然后您可以像往常一样使用主数据库,同时使用辅助数据库作为数据库对象:

// use master dataabse
$users = $this->db->get('users');

// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$stuff = $otherdb->get('struff');
$otherdb->insert_batch('users', $users->result_array());

// keep using master database as usual, for example insert stuff from other database
$this->db->insert_batch('stuff', $stuff->result_array());

希望回答了您的问题。 在你问问题之前先谷歌... ;)

Codeigniter - multiple database connections

【讨论】:

  • 放在哪里
  • // 使用主数据库 $users = $this->db->get('users'); // 连接到辅助数据库 $otherdb = $this->load->database('otherdb', TRUE); $stuff = $otherdb->get('struff'); $otherdb->insert_batch('users', $users->result_array()); // 像往常一样继续使用主数据库,例如从其他数据库插入东西 $this->db->insert_batch('stuff', $stuff->result_array());
  • 只需转到application > config > database.php 确保您的数据库已在autoload.php 中设置
  • @J.A.Manato 您可以参考此链接了解更多详情codeigniter.com/userguide2/database/index.html
【解决方案2】:

你有使用环境来使用多个数据库

  • 在生产和开发文件夹中创建config文件夹并添加database.php文件
  • 然后根路径index.php文件设置环境

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-01
    • 2023-03-16
    • 2021-10-19
    • 2018-07-07
    • 1970-01-01
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多