php實現Mysql讀寫分離的代碼舉例

來源:互聯網
上載者:User
本文介紹下,php實現mysql讀寫分離的一個例子,希望正在研究mysql讀寫分離的朋友,作個參考吧。

PHP實現的Mysql讀寫分離主要特性:1.簡單的讀寫分離2.一個主要資料庫,可以添加更多的唯讀資料庫3.讀寫分離但不用擔心某些特性不支援4.缺點:同時串連兩個資料庫feature:simply rw splitone master,can add more slavessupport all mysql featurelink to the master and slave at the same time

檔案 mysql_rw_php.class.php

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時,第一個串連作為rwif($this->link_rw == null)$this->link_rw = $this->link;if($this->version() > '4.1') {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() > '5.0.1') {@Mysql_query("SET sql_mode=''", $this->link);}}if($dbname) {$this->select_db($dbname);}}//串連一個唯讀mysql資料庫function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){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!
";$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!
";$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)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);}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 "MySQL Error
Message: $message
SQL: $sql
Error: $dberror
Errno.: $dberrno
";exit();}}?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.