php - 在 codeigniter 中切换数据库服务器以进行读取和写入操作。

标签 php mysql codeigniter-3

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

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

谢谢。

最佳答案

你必须更新系统/数据库/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 

关于php - 在 codeigniter 中切换数据库服务器以进行读取和写入操作。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43202811/

相关文章:

PHP - 从CSS中提取图像路径

php - 这可以在不使用两个 for 循环的情况下计算吗

php - 如何使用 jquery 和 ajax 查询 mysql 数据库?

mysql - xampp mysql 和 phpmyadmin 不工作

php - 如何从多个嵌套if条件调用json

php - 用于覆盖表前缀的 Joomla 插件

php - JavaScript奇怪的故障

mysql - 在 MySQL 中为多个属性添加列

php codeigniter 插入查询

javascript - 如何使用换行符在 SweetAlert 上输出 CodeIgniter 验证错误消息?