CodeIgniter 3.0支援資料庫讀寫分離方式

來源:互聯網
上載者:User

標籤:多個   注釋   功能   mysql   drivers   讀寫分離   pre   function   oid   

網上有部分方法,支援讀寫分離,但過於複製,而且有的只支援2.0版本的,現在改善一個,支援3.0版本的讀寫分離

本次修改的環境是:

  • CodeIgniter 3.0.3

  • MySQL 5.5+

  • PHP 5.5.9

  • nginx 1.1.8

步驟一:修改application/config/database.php

資料庫讀、寫串連參數的配置。

$active_group = ‘default‘;$query_builder = TRUE;$db[‘default‘] = array(    ‘dsn‘    => ‘‘,    ‘hostname‘ => ‘127.0.0.1‘,    ‘username‘ => ‘root‘,    ‘password‘ => ‘‘,    ‘database‘ => ‘xxx‘,    ‘dbdriver‘ => ‘mysqli‘,    ‘dbprefix‘ => ‘‘,    ‘pconnect‘ => FALSE,    ‘db_debug‘ => 1,    ‘cache_on‘ => FALSE,    ‘cachedir‘ => ‘‘,    ‘char_set‘ => ‘utf8‘,    ‘dbcollat‘ => ‘utf8_general_ci‘,    ‘swap_pre‘ => ‘‘,    ‘encrypt‘ => FALSE,    ‘compress‘ => FALSE,    ‘stricton‘ => FALSE,    //failover 配置備用節點    ‘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        )*/    ),    ‘save_queries‘ => TRUE);//開啟 $db[‘write‘] 支援讀寫分離$db[‘write‘] = array(    ‘dsn‘    => ‘‘,    ‘hostname‘ => ‘127.0.0.1‘,    ‘username‘ => ‘root‘,    ‘password‘ => ‘‘,    ‘database‘ => ‘xxx‘,    ‘dbdriver‘ => ‘mysqli‘,    ‘dbprefix‘ => ‘‘,    ‘pconnect‘ => FALSE,    ‘db_debug‘ => 1,    ‘cache_on‘ => FALSE,    ‘cachedir‘ => ‘‘,    ‘char_set‘ => ‘utf8‘,    ‘dbcollat‘ => ‘utf8_general_ci‘,    ‘swap_pre‘ => ‘‘,    ‘encrypt‘ => FALSE,    ‘compress‘ => FALSE,    ‘stricton‘ => FALSE,    //failover 配置備用節點    ‘failover‘ => array(),    ‘save_queries‘ => TRUE);
步驟二、修改 system/core/Controller.php 加入下列代碼
public $db_write = null;
步驟三、修改system/database/DB.php

找到 

$DB->initialize();

在上面加入

//支援 讀寫分離    if (!empty($db[‘write‘])){        $CI =& get_instance();        $CI->db_write = new $driver($db[‘write‘]);        $DB->is_single_instance = false;        $CI->db_write->is_single_instance = false;        $CI->db_write->initialize();    }else{        $DB->is_single_instance = true;    }
步驟四、修改 system/database/DB_driver.php

添加對SQL的資料庫連接選擇路由功能。

添加執行個體變數

public $is_single_instance = false;

並替換下面的方法

public function simple_query($sql)    {        if ( ! $this->conn_id)        {            $this->initialize();        }        if($this->is_single_instance){            //不讀寫分離            return $this->_execute($sql);        }else{            //讀寫分離            if(preg_match("/^\s*select/",strtolower($sql)))            {                return $this->_execute($sql);            }            else            {                $CI =& get_instance();                //預設選擇主庫!這點很重要,如果有未知功能也可以保證資料完整                $rrr = $CI->db_write->_execute($sql);                return $rrr;            }        }    }

第五驟:如果是使用預設的 mysqli串連,請注釋掉 system/database/drivers/mysqli/mysqli_driver.php 的下面方法

public function insert_id(){
return $this->conn_id->insert_id;

}

並在  system/database/DB_driver.php  裡面新增下方法

/**     * Insert ID     *     * @return    int     */    public function insert_id()    {        //return $this->conn_id->insert_id;        if($this->is_single_instance){            return $this->conn_id->insert_id;        }else{            //讀寫分離            $CI =& get_instance();            return $CI->db_write->conn_id->insert_id;        }    }

第六步:修改 system/database/DB_driver.php 裡面的 close方法

/**     * Close DB Connection     *     * @return    void     */    public function close()    {        if($this->is_single_instance){            if ($this->conn_id)            {                $this->_close();                $this->conn_id = FALSE;            }        }else{            //讀寫分離            $CI =& get_instance();            $CI->db_write->conn_id = FALSE;            $this->conn_id = FALSE;        }    }

至此,CodeIgniter 3.0 完美的解決了 讀寫分離的操作

本文參考了部分網上的資源

 

 

 

CodeIgniter 3.0支援資料庫讀寫分離方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.