2017-04-04 39 views
2

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

有人可以告诉我如何在codigniter版本3中实现这一点。

谢谢。

+0

你可以有两个数据库连接,每当有写操作使用主对象和每当读取使用salve obj –

+0

您是否正在寻找? http://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections – Jabaa

+0

actully我的应用程序已经完成,我不想创建2个独立的连接,并且不想编辑所有applcation模型文件。所以我正在寻找codigniter hack哪个开关DB用于读取和写入操作 –

回答

3

你有什么更新系统/数据库/文件DB_Driver.php。

它非常简单,你必须在这个文件中做出3个小改动。

1)内部结构添加您的读写服务器凭据。

 $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)创建新函数将切换数据库连接以进行选择和写入查询。

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 
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); 
+0

我知道这种直接的方式,我的要求是保持数据库连接活动,并根据选择或更新查询切换服务器 –

+0

意味着您希望根据选择或更新查询更改切换服务器,而不是扩展数据库库。 – Gaurav

+0

是的,其实我的应用程序已经在运行,并有很多模型文件,所以我不想更新每个这个文件 –

相关问题