使用codeigniter读取副本实现

时间:2014-09-07 02:29:11

标签: codeigniter amazon-rds

我正在使用codigniter框架开发PHP应用程序。并使用亚马逊阅读副本设置。我需要一种方法来处理所有选择查询通过只读数据库和所有更新相关的查询通过主数据库。以下是我在脚本中使用的当前用法。

数据库配置文件

$db['default']['hostname'] = 'master_db_host';
$db['default']['username'] = 'dbuser';
$db['default']['password'] = 'dbpassword';
$db['default']['database'] = 'db_name';
$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;

模型脚本

class replica_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }

    public function write($data)
    {
        $this->db->insert('dumy', $data);
        return $this->db->insert_id();
    }

    public function read()
    {
        $query = $this->db->get("dumy");
        return $query->result();
    }

}

任何人都可以帮我改变脚本以基于读写来访问主副本吗?

2 个答案:

答案 0 :(得分:2)

我们可以使用codeigniter处理多个数据库。首先,我们可以看到如何设置配置文件。

$active_group = 'default';
$active_record = TRUE;

//Master DB config values
$db['default']['hostname'] = 'master_db_host';
$db['default']['username'] = 'db_user';
$db['default']['password'] = 'db_pass';
$db['default']['database'] = 'db_name';
$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;


//Replica DB config values
$db['read_replica']['hostname'] = 'replica_db_host';
$db['read_replica']['username'] = 'db_user';
$db['read_replica']['password'] = 'db_password';
$db['read_replica']['database'] = 'db_name';
$db['read_replica']['dbdriver'] = 'mysql';
$db['read_replica']['dbprefix'] = '';
$db['read_replica']['pconnect'] = FALSE;
$db['read_replica']['db_debug'] = TRUE;
$db['read_replica']['cache_on'] = FALSE;
$db['read_replica']['cachedir'] = '';
$db['read_replica']['char_set'] = 'utf8';
$db['read_replica']['dbcollat'] = 'utf8_general_ci';
$db['read_replica']['swap_pre'] = '';
$db['read_replica']['autoinit'] = TRUE;
$db['read_replica']['stricton'] = FALSE;

参数“$ db ['read_replica'] ['pconnect']”在master db config上应为true,在replica config上应为false。其他明智的做法是我们启用replica db master config也会被副本配置覆盖。

然后模型用法如下。

class Replica_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->db_replica = $this->load->database('read_replica',TRUE);
    }

    public function write($data)
    {
        $this->db->insert('dumy', $data);
        return $this->db->insert_id();
    }

    public function read()
    {
        $query = $this->db_replica->get("dumy");
        return $query->result();
    }

}

答案 1 :(得分:-1)

CI允许您通过组连接到多个数据库。

这是直接来自他们的网站:

https://ellislab.com/codeigniter/user-guide/database/connecting.html

$DB1 = $this->load->database('read', TRUE);
$DB2 = $this->load->database('write', TRUE);
如果您以这种方式连接,您将使用您的对象名称来发出命令,而不是本指南中使用的语法。换句话说,而不是发出命令:

$this->db->query();
$this->db->result();

等...

您将使用:

$DB1->query();
$DB1->result();

等...

在你的db中有一个多维数组

 $db['read']['hostname'] = ...

 $db['write']['hostname'] = ...

所以当你阅读时,你会使用$ DB1,当你写的时候,你会使用$ DB2 ......

我没有使用过这个确切的设置。但这就是它的要点。