阿里雲rds疑問,一個主rds,多個唯讀rds,網站怎麼設定資料庫連接?

來源:互聯網
上載者:User
是需要配置多個串連,還是只配置主rds就可以?

回複內容:

是需要配置多個串連,還是只配置主rds就可以?

通常寫走主rds,讀走唯讀rds。
如果是php,通常mvc的架構只要配置一下就行了。
比如thinkphp,讀的時候可以指定使用某個rds,也可以隨機一個rds
以下是代碼(看不懂就看注釋吧):

    /**     * 串連分布式伺服器     * @access protected     * @param boolean $master 主伺服器     * @return void     */    protected function multiConnect($master=false) {        foreach ($this->config as $key=>$val){            $_config[$key]      =   explode(',',$val);        }                // 資料庫讀寫是否分離        if(C('DB_RW_SEPARATE')){            // 主從式採用讀寫分離            if($master)                // 主伺服器寫入                $r  =   floor(mt_rand(0,C('DB_MASTER_NUM')-1));            else{                if(is_numeric(C('DB_SLAVE_NO'))) {// 指定伺服器讀                    $r = C('DB_SLAVE_NO');                }else{                    // 讀操作串連從伺服器                    $r = floor(mt_rand(C('DB_MASTER_NUM'),count($_config['hostname'])-1));   // 每次隨機串連的資料庫                }            }        }else{            // 讀寫操作不區分伺服器            $r = floor(mt_rand(0,count($_config['hostname'])-1));   // 每次隨機串連的資料庫        }        $db_config = array(            'username'  =>  isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],            'password'  =>  isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],            'hostname'  =>  isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],            'hostport'  =>  isset($_config['hostport'][$r])?$_config['hostport'][$r]:$_config['hostport'][0],            'database'  =>  isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],            'dsn'       =>  isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],            'params'    =>  isset($_config['params'][$r])?$_config['params'][$r]:$_config['params'][0],            'charset'   =>  isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0],                    );        return $this->connect($db_config,$r);    }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.