本文介绍下,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();}}?>