2017-04-04 12 views
2

私は自分のデータベースサーバーのマスターとスレーブの設定を行っています。私のcodigniterアプリケーションでは、マスターサーバー上のすべての書き込みオプションとスレーブサーバー上のすべての読み取り操作を実行します。読み取りおよび書き込み操作のためのcodigniterのデータベースサーバーを切り替えます。

codigniter version3でこれをどのように達成することができますか?

ありがとうございました。

+0

次の2つのデータベース接続を持つことができ、書き込み動作用マスター・オブジェクトといつでも読ん利用軟膏OBJ –

+0

があるたびに、あなたはこのをお探しですか? http://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections – Jabaa

+0

私のアプリケーションはすでに完了しており、2つの別個の接続を作成する必要はなく、すべてのアプリケーションモデルファイルを編集する必要はありません。私はcodigiter hackを探しています。読み取りと書き込み操作のためにDBをオンザフライで切り替えることができます –

答えて

3

このファイルで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

あなたはアプリケーションの `で2番目のデータベースの情報を提供しなければならない/設定/ 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; 

同時にあなたがそうすることができます:あなたがシステム/データベース/ DB_Driver.phpファイルを更新する必要がどのような

$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

私は、DB接続を有効にして、選択または更新クエリに基づいてサーバーを切り替えるという、私の要求が真正であることを知っています。 –

+0

データベース照合を拡張するよりも、選択または更新クエリに基づいてスイッチサーバーを変更します。 – Gaurav

+0

はい、実際には私のアプリケーションはすでに実行されており、多くのモデルファイルを持っているので、このファイルをそれぞれ更新したくありません。 –

関連する問題