溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

thinkphp下MySQL數據庫讀寫分離的示例分析

發布時間:2021-07-24 13:59:01 來源:億速云 閱讀:179 作者:小新 欄目:開發技術

這篇文章主要為大家展示了“thinkphp下MySQL數據庫讀寫分離的示例分析”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“thinkphp下MySQL數據庫讀寫分離的示例分析”這篇文章吧。

當采用原生態的sql語句進行寫入操作的時候,要用execute,讀操作要用query。

MySQL數據主從同步還是要靠MySQL的機制來實現,所以這個時候MySQL主從同步的延遲問題是需要優化,延遲時間太長不僅影響業務,還影響用戶體驗。

thinkphp核心類Thinkphp/library/Model.class.php 中,query 方法,調用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);
  }

調用Thinkphp/library/Think/Db/Driver/Mysql.class.php

  /**
   * 執行查詢 返回數據集
   * @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);
    // 記錄開始執行時間
    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();
    }
  }

上面初始化數據庫鏈接時,initConnect(false),調用Thinkphp/library/Think/Db/Db.class.php,注意false、true代碼實現。true表示直接調用主庫,false表示調用讀寫分離的讀庫。

  /**
   * 初始化數據庫連接
   * @access protected
   * @param boolean $master 主服務器
   * @return void
   */
  protected function initConnect($master=true) {
    if(1 == C('DB_DEPLOY_TYPE'))
      // 采用分布式數據庫
      $this->_linkID = $this->multiConnect($master);
    else
      // 默認單數據庫
      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);
    }    
    // 數據庫讀寫是否分離
    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);
  }

query方法參數為false,其他刪除、更新、增加讀主庫。這一點可以結合Thinkphp/library/Model.class.php中的delete、save、add操作,參數為true。

以上是“thinkphp下MySQL數據庫讀寫分離的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女