在codigniter中切换数据库服务器以进行读写操作。

时间:2017-04-04 09:08:47

标签: php mysql codeigniter-3

我已经为我的数据库服务器完成了主设备和从设备,因此在我的codigniter应用程序中,我想要的是在主服务器上执行所有写入选项以及在从属服务器上执行所有读取操作。

有人能告诉我如何在codigniter version3中实现这一点。

感谢。

2 个答案:

答案 0 :(得分:3)

您需要更新system / database / DB_Driver.php文件。

您必须在此文件中进行3次小更改非常简单。

1)Inside Construction添加您的读写服务器凭据。

        $this->server_details['local_server']['hostname'] = "localhost";
        $this->server_details['local_server']['username'] = "select_user";
        $this->server_details['local_server']['password'] = "password";       

        $this->server_details['live_server']['hostname'] = "192.192.223.22";  
        $this->server_details['live_server']['username'] = "write_user"; 
        $this->server_details['live_server']['password'] = "password";        

2)Create New函数将切换数据库连接以进行选择和写入查询。

 private function ebrandz_switch_db_for_read_write($sql) {               

       if( $this->hostname == $this->server_details['local_server']['hostname'] &&  $this->username == $this->server_details['local_server']['username'] &&  $this->password == $this->server_details['local_server']['password'] ) {    
                   //echo $sql.'<br/>';
         if(stristr($sql, 'SELECT')) {   
                            foreach($this->server_details['local_server'] as $index_key => $index_value ) { 
                                $this->$index_key = $index_value;  
                            }             

                              $this->conn_id = null;  //unset resource link 
                              $this->initialize();   //Reinitialize connnection with new parameters                                                                                                       

                    } else {    
                            //die('write operation is not allowed.');
                            foreach($this->server_details['live_server'] as $index_key => $index_value ) { 
                                 $this->$index_key = $index_value;  
                            }  
                            $this->conn_id = null ; //unset resource link 
                            $this->initialize();    //Reinitialize connnection with new parameters                                                           
                    }

               } else if( $this->hostname == $this->server_details['live_server']['hostname'] &&  $this->username == $this->server_details['live_server']['username']  &&  $this->password ==  $this->server_details['live_server']['password'] ) {  

                    if(stristr($sql, 'SELECT')) { 
                            foreach($this->server_details['local_server'] as $index_key => $index_value ) { 
                                 $this->$index_key = $index_value;  
                            }  

                            $this->conn_id = null ;  //unset resource link 
                            $this->initialize();     //Reinitialize connnection with new parameters      

                    } else {  
                            //die('write operation is not allowed.');
                            foreach($this->server_details['live_server'] as $index_key => $index_value ) { 
                                 $this->$index_key = $index_value;
                            } 

                            $this->conn_id = null ; //unset resource link 
                            $this->initialize();    //Reinitialize connnection with new parameters                                                           
                    }

               }

               //Code to re initialize the connection 
    }

3)此文件的内部查询功能必须调用普遍定义的函数。

// Verify table prefix and replace if necessary
    if ($this->dbprefix !== '' && $this->swap_pre !== '' && $this->dbprefix !== $this->swap_pre)
    {
        $sql = preg_replace('/(\W)'.$this->swap_pre.'(\S+?)/', '\\1'.$this->dbprefix.'\\2', $sql);
    }

     /**
     * @author Anant Waykar
     * if query is read only then load some other database
     */
            $this->ebrandz_switch_db_for_read_write($sql);                     
      //Code to re initialize the connection 

答案 1 :(得分:0)

您应该在`application / config / database.php'

中提供第二个数据库信息

通常,您可以设置默认数据库组,如下所示:

$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;

请注意,登录信息和设置在名为$db['default']的数组中提供。

然后,您可以在新数组中添加另一个数据库 - 让我们称之为'otherdb'。

$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'] = TRUE;
$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;

如果您需要同时连接到多个数据库,可以按照以下步骤操作:

$readDB = $this->load->database('otherdb', TRUE);

//For read:
$query = $readDB->select('first_name, last_name')->get('person');
var_dump($query);

//For write:
$this->db->insert('tablename', $insert_array);