本代码是从uchome的代码修改的,是因为要解决uchome的效率而处理的。这个思维其实很久就有了,只是一直没有去做,相信也有人有同样的想法,如果有类似的,那真的希望提出相关的建议。封装的方式比较简单,增加了只读数据库连接的接口扩展,不使用只读数据库也不影响原代码使用。有待以后不断完善。。
为了方便,试试建立了google的一个项目:
http://code.google.com/p/mysql-rw-php/
希望给有需要的朋友带来帮助。
PHP实现的MysqL读写分离
主要特性:2.一个主数据库,可以添加更多的只读数据库
3.读写分离但不用担心某些特性不支持
4.缺点:同时连接两个数据库
英文比较烂,也写几个字吧
PHP code for MysqL read/write split
feature:
simply rw split
one master,can add more slaves
support all MysqL feature
link to the master and slave at the same time
PHP代码:
MysqL_rw_PHP.class.PHP 代码如下:
PHP
/****************************************
*** MysqL-rw-PHP version 0.1
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/MysqL-rw-PHP/
*** code modify from class_MysqL.PHP (uchome)
****************************************/
class MysqL_rw_PHP {
//查询个数
var $querynum = 0;
//当前操作的数据库连接
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 = 0,$halt = TRUE) {
if($pconnect) {
if(!$this->link = @MysqL_pconnect($dbhost,$dbpw)) {
$halt && $this->halt('Can not connect to MysqL server');
}
} else {
if(!$this->link = @MysqL_connect($dbhost,$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() > '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,$pconnect = 0){
if($this->link_rw == null)
$this->link_rw = $this->link;
$this->link = null;
//不产生halt错误
$this->connect($dbhost,$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 "";
exit();
}
}
?>
原文链接:https://www.f2er.com/php/25950.html/****************************************
*** MysqL-rw-PHP version 0.1
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/MysqL-rw-PHP/
*** code modify from class_MysqL.PHP (uchome)
****************************************/
class MysqL_rw_PHP {
//查询个数
var $querynum = 0;
//当前操作的数据库连接
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 = 0,$halt = TRUE) {
if($pconnect) {
if(!$this->link = @MysqL_pconnect($dbhost,$dbpw)) {
$halt && $this->halt('Can not connect to MysqL server');
}
} else {
if(!$this->link = @MysqL_connect($dbhost,$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() > '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,$pconnect = 0){
if($this->link_rw == null)
$this->link_rw = $this->link;
$this->link = null;
//不产生halt错误
$this->connect($dbhost,$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 "";
exit();
}
}
?>