CodeIgniter 3.0 Support Database read/write separation mode

Source: Internet
Author: User
Tags dsn failover pconnect codeigniter

There are some methods on the Internet that support read-write separation, but too much copy, and some only support 2.0 version, now improve one, support 3.0 version of read-write separation

The environment for this modification is:

    • CodeIgniter 3.0.3

    • MySQL 5.5+

    • PHP 5.5.9

    • Nginx 1.1.8

Step One: Modify application/config/database.php

Configuration of database Read and write connection parameters.

$active _group= ' Default ';$query _builder=TRUE;$db[' Default '] =Array(    ' DSN ' = ' = ', ' hostname ' = ' 127.0.0.1 ', ' username ' + ' root ', ' password ' + = ', ' database ' =&gt ; ' xxx ', ' dbdriver ' = ' mysqli ', ' dbprefix ' = ', ' pconnect 'FALSE, ' Db_debug ' + 1, ' cache_on ' =FALSE, ' cachedir ' = ', ' char_set ' = ' utf8 ', ' dbcollat ' = ' utf8_general_ci ', ' swap_pre ' = ', ' E Ncrypt ' =FALSE, ' compress ' =FALSE, ' Stricton ' =FALSE,//failover Configuring an alternate node' Failover ' =Array(        /* Set up multiple standby database hosts * array (' hostname ' = ' localhost1 ', ' username ' = ' = ', ' Passwo            Rd ' = = ', ' database ' = ', ' dbdriver ' = ' mysqli ', ' dbprefix ' = ', ' ' Pconnect ' = True, ' db_debug ' = = True, ' cache_on ' = FALSE, ' Cachedir ' =&G T            ', ' char_set ' = ' utf8 ', ' dbcollat ' and ' utf8_general_ci ', ' swap_pre ' = ', ' ' Encrypt ' = false, ' compress ' = False, ' Stricton ' and false), array (' hostname ' = ' localhost2 ', ' username ' and ' = ', ' password ' and ' = ', ' da            Tabase ' + ', ' dbdriver ' = ' mysqli ', ' dbprefix ' + ', ' pconnect ' and ' = TRUE ',  ' Db_debug ' + TRUE, ' cache_on ' = FALSE, ' cachedir ' = ', ' Char_set '       = ' UTF8 ',     ' Dbcollat ' = ' utf8_general_ci ', ' swap_pre ' = ', ' encrypt ' and ' = FALSE, ' com Press ' = False, ' Stricton ' = False)*/    ), ' save_queries ' =TRUE);//open $db [' write '] supports read and write separation$db [' write '] = Array (' DSN ' = = ', ' hostname ' = ' 127.0.0.1 ', ' username ' = ' root ', ' password '    = = ', ' database ' = ' xxx ', ' dbdriver ' = ' mysqli ', ' dbprefix ' + ', ' pconnect ' and ' = FALSE ', ' Db_debug ' = 1, ' cache_on ' and FALSE, ' cachedir ' = ', ' char_set ' = ' utf8 ', ' dbcollat ' + ' UT    F8_general_ci ', ' swap_pre ' = ', ' encrypt ' = false, ' compress ' = False, ' Stricton ' = False, Failover Configuration Alternate node ' failover ' = = Array (), ' save_queries ' = TRUE);
Step two, modify system/core/controller.php add the following code
Public $db _write = null;
Step three, modify the system/database/db.php

Found it

$DB->initialize ();

Add on top

//supports read and write separations    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; }
Step four, modify the system/database/db_driver.php

Add a database connection to SQL Select Routing feature.

Add an instance variable

 Public $is _single_instance false;

and replace the following method

 Public functionSimple_query ($sql)    {        if( !$this-conn_id) {            $this-Initialize (); }        if($this-is_single_instance) {            //non-read and write separation            return $this->_execute ($sql); }Else{            //read/write separation            if(Preg_match("/^\s*select/",Strtolower($sql)))            {                return $this->_execute ($sql); }            Else            {                $CI=&get_instance (); //The Main library is selected by default! This is important, and if you have an unknown function, you can keep the data intact.                $rrr=$CI->db_write->_execute ($sql); return $rrr; }        }    }

Step five: If you are using the default mysqli connection, please comment out the following method of system/database/drivers/mysqli/mysqli_driver.php

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

}

and add a new method to the system/database/db_driver.php .

/** * Insert ID * * @return int*/     Public functioninsert_id () {//return $this->conn_id->insert_id;        if($this-is_single_instance) {            return $this->conn_id->insert_id; }Else{            //read/write separation            $CI=&get_instance (); return $CI->db_write->conn_id->insert_id; }    }

Sixth step: Modify The Close method inside the system/database/db_driver.php

/** * Close DB Connection * * @return void*/     Public functionClose () {if($this-is_single_instance) {            if($this-conn_id) {                $this-_close (); $this->conn_id =FALSE; }        }Else{            //read/write separation            $CI=&get_instance (); $CI->db_write->conn_id =FALSE; $this->conn_id =FALSE; }    }

At this point, the CodeIgniter 3.0 perfectly solves the read-write separation operation

This article refers to some of the resources on the Internet

CodeIgniter 3.0 Support Database read/write separation mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.