douza9835 2017-04-04 09:08
浏览 48
已采纳

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

I have done master and slave setup for my database server, so In my codigniter application what I want is to execute all write options on master server and all read operation on slave server.

Can anybody tell me how can I achieve this in codigniter version3.

Thanks.

  • 写回答

2条回答 默认 最新

  • douchun1961 2017-04-06 14:17
    关注

    What you have to update system/database/DB_Driver.php file.

    Its very simple what you have to make 3 small changes in this file.

    1) Inside Construction add your Read and Write server credentials.

            $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 function will switch the database connection for select and write query.

     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) Inside Query function of this file you have to call the prevous defined function.

    // 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条)

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴