解析PHP实现的Mysql读写分离_PHP教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:浅谈PHP企业级应用之WebService篇来段企业级应用吧,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用) 国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件
PHP实现的Mysql读写分离
主要特性:
1 简单的读写分离
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 @ 2009-4-16
*** 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, $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() > '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!<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)) >= 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 "<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
<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();
}
}
?>
/****************************************
*** mysql-rw-php version 0.1 @ 2009-4-16
*** 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, $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() > '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!<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)) >= 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 "<div style=\"position:absolute;font-size:11px;font-family:verdana,arial;background:#EBEBEB;padding:0.5em;\">
<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();
}
}
?>
example.php
<?php
/****************************************
*** mysql-rw-php version 0.1 @ 2009-4-16
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/mysql-rw-php/
*** code modify from class_mysql.php (uchome)
****************************************/
require_once('mysql_rw_php.class.php');
//rw info
$db_rw = array(
'dbhost'=>'www.aslibra.com',
'dbuser'=>'aslibra',
'dbpw'=>'www.aslibra.com',
'dbname'=>'test'
);
$db_ro = array(
array(
'dbhost'=>'www.aslibra.com:4306',
'dbuser'=>'aslibra',
'dbpw'=>'www.aslibra.com'
)
);
$DB = new mysql_rw_php;
//connect Master
$DB->connect($db_rw[dbhost], $db_rw[dbuser], $db_rw[dbpw], $db_rw[dbname]);
//Method 1: connect one server
$DB->connect_ro($db_ro[0][dbhost], $db_ro[0][dbuser], $db_ro[0][dbpw]);
//Method 2: 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];
}
?>
/****************************************
*** mysql-rw-php version 0.1 @ 2009-4-16
*** code by hqlulu#gmail.com
*** http://www.aslibra.com
*** http://code.google.com/p/mysql-rw-php/
*** code modify from class_mysql.php (uchome)
****************************************/
require_once('mysql_rw_php.class.php');
//rw info
$db_rw = array(
'dbhost'=>'www.aslibra.com',
'dbuser'=>'aslibra',
'dbpw'=>'www.aslibra.com',
'dbname'=>'test'
);
$db_ro = array(
array(
'dbhost'=>'www.aslibra.com:4306',
'dbuser'=>'aslibra',
'dbpw'=>'www.aslibra.com'
)
);
$DB = new mysql_rw_php;
//connect Master
$DB->connect($db_rw[dbhost], $db_rw[dbuser], $db_rw[dbpw], $db_rw[dbname]);
//Method 1: connect one server
$DB->connect_ro($db_ro[0][dbhost], $db_ro[0][dbuser], $db_ro[0][dbpw]);
//Method 2: 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];
}
?>
分享:浅谈PHP企业级应用之常见缓存技术篇别每天OO,这个配置怎么改,这段代码哪错了,没劲,好的程序不光是代码写的好,整体架构很重要,多了解程序之外的东西,这篇不再单单是程序方面的东西了,会写到从程序到服务器的设置,会比较多,但个人的经验有限,知道的就这么多,不知道的我也没法写呀,
相关PHP教程:
- 相关链接:
- 教程说明:
PHP教程-解析PHP实现的Mysql读写分离。