熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> PHP編程 >> 正文

PHP實現的MySQL讀寫分離

2013-11-15 12:43:53  來源: PHP編程 

  主要特性

  簡單的讀寫分離
  一個主數據庫可以添加更多的只讀數據庫
  讀寫分離但不用擔心某些特性不支持
  缺點同時連接兩個數據庫
  英文比較爛也寫幾個字吧

php code for mysql read/write split
feature:
simply rw split
one mastercan add more slaves
support all mysql feature
link to the master and slave at the same time

  PHP代碼

mysql_rw_phpclassphp

<?php
/****************************************
*** mysqlrwphp version @
*** code by hqlulu#gmailcom
*** http://wwwaslibracom
*** http://codegooglecom/p/mysqlrwphp/
*** code modify from class_mysqlphp (uchome)
****************************************/
class mysql_rw_php {
  //查詢個數
  var $querynum = ;
  //當前操作的數據庫連接
  var $link = null;
  //字符集
  var $charset;
  //當前數據庫
  var $cur_db = ;
  //是否存在有效的只讀數據庫連接
  var $ro_exist = false;
  //只讀數據庫連接
  var $link_ro = null;
  //讀寫數據庫連接
  var $link_rw = null;
  function mysql_rw_php(){
  }
  function connect($dbhost $dbuser $dbpw $dbname = $pconnect = $halt = TRUE) {
    if($pconnect) {
      if(!$this>link = @mysql_pconnect($dbhost $dbuser $dbpw)) {
        $halt && $this>halt(Can not connect to MySQL server);
      }
    } else {
      if(!$this>link = @mysql_connect($dbhost $dbuser $dbpw)) {
        $halt && $this>halt(Can not connect to MySQL server);
      }
    }
   
    //只讀連接失敗
    if(!$this>link && !$halt) return false;
   
    //未初始化rw時第一個連接作為rw
    if($this>link_rw == null)
      $this>link_rw = $this>link;
    if($this>version() > ) {
      if($this>charset) {
        @mysql_query(SET character_set_connection=$this>charset character_set_results=$this>charset character_set_client=binary $this>link);
      }
      if($this>version() > ) {
        @mysql_query(SET sql_mode= $this>link);
      }
    }
    if($dbname) {
      $this>select_db($dbname);
    }
  }
  //連接一個只讀的mysql數據庫
  function connect_ro($dbhost $dbuser $dbpw $dbname = $pconnect = ){
    if($this>link_rw == null)
      $this>link_rw = $this>link;
    $this>link = null;
    //不產生halt錯誤
    $this>connect($dbhost $dbuser $dbpw $dbname $pconnect false);
    if($this>link){
      //連接成功
      //echo link ro sussess!<br>;
      $this>ro_exist = true;
      $this>link_ro = $this>link;
      if($this>cur_db){
        //如果已經選擇過數據庫則需要操作一次
        @mysql_select_db($this>cur_db $this>link_ro);
      }
    }else{
      //連接失敗
      //echo link ro failed!<br>;
      $this>link = &$this>link_rw;
    }
  }
  //設置一系列只讀數據庫並且連接其中一個
  function set_ro_list($ro_list){
    if(is_array($ro_list)){
      //隨機選擇其中一個
      $link_ro = $ro_list[array_rand($ro_list)];
      $this>connect_ro($link_ro[dbhost] $link_ro[dbuser] $link_ro[dbpw]);
    }
  }
  function select_db($dbname) {
    //同時操作兩個數據庫連接
    $this>cur_db = $dbname;
    if($this>ro_exist){
      @mysql_select_db($dbname $this>link_ro);
    }
    return @mysql_select_db($dbname $this>link_rw);
  }
  function fetch_array($query $result_type = MYSQL_ASSOC) {
    return mysql_fetch_array($query $result_type);
  }
  function fetch_one_array($sql $type = ) {
    $qr = $this>query($sql $type);
    return $this>fetch_array($qr);
  }
  function query($sql $type = ) {
    $this>link = &$this>link_rw;
    //判斷是否select語句
    if($this>ro_exist && preg_match (/^(\s*)select/i $sql)){
      $this>link = &$this>link_ro;
    }
    $func = $type == UNBUFFERED && @function_exists(mysql_unbuffered_query) ?
      mysql_unbuffered_query : mysql_query;
    if(!($query = $func($sql $this>link)) && $type != SILENT) {
      $this>halt(MySQL Query Error $sql);
    }
    $this>querynum++;
    return $query;
  }
  function affected_rows() {
    return mysql_affected_rows($this>link);
  }
  function error() {
    return (($this>link) ? mysql_error($this>link) : mysql_error());
  }
  function errno() {
    return intval(($this>link) ? mysql_errno($this>link) : mysql_errno());
  }
  function result($query $row) {
    $query = @mysql_result($query $row);
    return $query;
  }
  function num_rows($query) {
    $query = mysql_num_rows($query);
    return $query;
  }
  function num_fields($query) {
    return mysql_num_fields($query);
  }
  function free_result($query) {
    return mysql_free_result($query);
  }
  function insert_id() {
    return ($id = mysql_insert_id($this>link)) >= ? $id : $this>result($this>query(SELECT last_insert_id()) );
  }
  function fetch_row($query) {
    $query = mysql_fetch_row($query);
    return $query;
  }
  function fetch_fields($query) {
    return mysql_fetch_field($query);
  }
  function version() {
    return mysql_get_server_info($this>link);
  }
  function close() {
    return mysql_close($this>link);
  }
  function halt($message = $sql = ) {
    $dberror = $this>error();
    $dberrno = $this>errno();
    echo <div style=\position:absolute;fontsize:px;fontfamily:verdanaarial;background:#EBEBEB;padding:em;\>
        <b>MySQL Error</b><br>
        <b>Message</b>: $message<br>
        <b>SQL</b>: $sql<br>
        <b>Error</b>: $dberror<br>
        <b>Errno</b>: $dberrno<br>
        </div>;
    exit();
  }
}
?>

  examplephp

<?php
/****************************************
*** mysqlrwphp version @
*** code by hqlulu#gmailcom
*** http://wwwaslibracom
*** http://codegooglecom/p/mysqlrwphp/
*** code modify from class_mysqlphp (uchome)
****************************************/
require_once(mysql_rw_phpclassphp);
//rw info
$db_rw = array(
  dbhost=>wwwaslibracom
  dbuser=>aslibra
  dbpw=>wwwaslibracom
  dbname=>test
);
$db_ro = array(
  array(
    dbhost=>wwwaslibracom:
    dbuser=>aslibra
    dbpw=>wwwaslibracom
  )
);
$DB = new mysql_rw_php;
//connect Master
$DB>connect($db_rw[dbhost] $db_rw[dbuser] $db_rw[dbpw] $db_rw[dbname]);
//Method : connect one server
$DB>connect_ro($db_ro[][dbhost] $db_ro[][dbuser] $db_ro[][dbpw]);
//Method : connect one server from a list by rand
$DB>set_ro_list($db_ro);
//send to rw
$sql = insert into a set a=test;
$DB>query($sql);
//send to ro
$sql = select * from a;
$qr = $DB>query($sql);
while($row = $DB>fetch_array($qr)){
  echo $row[a];
}
?>


From:http://tw.wingwit.com/Article/program/PHP/201311/21488.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.