這篇文章主要介紹thinkphp下數(shù)據(jù)庫讀寫分離的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
為雙江等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及雙江網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營銷網(wǎng)站建設、雙江網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
當采用原生態(tài)的sql語句進行寫入操作的時候,要用execute,讀操作要用query。
MySQL數(shù)據(jù)主從同步還是要靠MySQL的機制來實現(xiàn),所以這個時候MySQL主從同步的延遲問題是需要優(yōu)化,延遲時間太長不僅影響業(yè)務,還影響用戶體驗。
thinkphp核心類Thinkphp/library/Model.class.php 中,query 方法,
調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/** * SQL查詢 * @access public * @param string $sql SQL * @param mixed $parse 是否需要解析SQL * @return mixed */ public function query($sql,$parse=false) { if(!is_bool($parse) && !is_array($parse)) { $parse = func_get_args(); array_shift($parse); } $sql = $this->parseSql($sql,$parse); return $this->db->query($sql); }
調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/** * 執(zhí)行查詢 返回數(shù)據(jù)集 * @access public * @param string $str sql指令 * @return mixed */ public function query($str) { if(0===stripos($str, 'call')){ // 存儲過程查詢支持 $this->close(); $this->connected = false; } $this->initConnect(false); if ( !$this->_linkID ) return false; $this->queryStr = $str; //釋放前次的查詢結果 if ( $this->queryID ) { $this->free(); } N('db_query',1); // 記錄開始執(zhí)行時間 G('queryStartTime'); $this->queryID = mysql_query($str, $this->_linkID); $this->debug(); if ( false === $this->queryID ) { $this->error(); return false; } else { $this->numRows = mysql_num_rows($this->queryID); return $this->getAll(); } }
上面初始化數(shù)據(jù)庫鏈接時,initConnect(false),調(diào)用Thinkphp/library/Think/Db/Db.class.php,注意false、true代碼實現(xiàn)。true表示直接調(diào)用主庫,false表示調(diào)用讀寫分離的讀庫。
/** * 初始化數(shù)據(jù)庫連接 * @access protected * @param boolean $master 主服務器 * @return void */ protected function initConnect($master=true) { if(1 == C('DB_DEPLOY_TYPE')) // 采用分布式數(shù)據(jù)庫 $this->_linkID = $this->multiConnect($master); else // 默認單數(shù)據(jù)庫 if ( !$this->connected ) $this->_linkID = $this->connect(); } /** * 連接分布式服務器 * @access protected * @param boolean $master 主服務器 * @return void */ protected function multiConnect($master=false) { foreach ($this->config as $key=>$val){ $_config[$key] = explode(',',$val); } // 數(shù)據(jù)庫讀寫是否分離 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)); // 每次隨機連接的數(shù)據(jù)庫 } } }else{ // 讀寫操作不區(qū)分服務器 $r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次隨機連接的數(shù)據(jù)庫 } $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); }
query方法參數(shù)為false,其他刪除、更新、增加讀主庫。這一點可以結合Thinkphp/library/Model.class.php中的delete、save、add操作,參數(shù)為true。
以上是“thinkphp下數(shù)據(jù)庫讀寫分離的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
標題名稱:thinkphp下數(shù)據(jù)庫讀寫分離的示例分析
轉載源于:http://muchs.cn/article30/jchepo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、靜態(tài)網(wǎng)站、微信小程序、App設計、服務器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)