使用PHP實(shí)現(xiàn)Mysql讀寫(xiě)分離
本代碼是從uchome的代碼修改的,是因?yàn)橐鉀Quchome的效率而處理的。這個(gè)思維其實(shí)很久就有了,只是一直沒(méi)有去做,相信也有人有同樣的想法,如果有類(lèi)似的,那真的希望提出相關(guān)的建議。
封裝的方式比較簡(jiǎn)單,增加了只讀數(shù)據(jù)庫(kù)連接的接口擴(kuò)展,不使用只讀數(shù)據(jù)庫(kù)也不影響原代碼使用。有待以后不斷完善。。
為了方便,試試建立了google的一個(gè)項(xiàng)目:
http://code.google.com/p/mysql-rw-php/
希望給有需要的朋友帶來(lái)幫助。
PHP實(shí)現(xiàn)的Mysql讀寫(xiě)分離
主要特性:
1.簡(jiǎn)單的讀寫(xiě)分離
2.一個(gè)主數(shù)據(jù)庫(kù),可以添加更多的只讀數(shù)據(jù)庫(kù)
3.讀寫(xiě)分離但不用擔(dān)心某些特性不支持
4.缺點(diǎn):同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)
英文比較爛,也寫(xiě)幾個(gè)字吧
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 {
//查詢(xún)個(gè)數(shù)
var $querynum = 0;
//當(dāng)前操作的數(shù)據(jù)庫(kù)連接
var $link = null;
//字符集
var $charset;
//當(dāng)前數(shù)據(jù)庫(kù)
var $cur_db = '';
//是否存在有效的只讀數(shù)據(jù)庫(kù)連接
var $ro_exist = false;
//只讀數(shù)據(jù)庫(kù)連接
var $link_ro = null;
//讀寫(xiě)數(shù)據(jù)庫(kù)連接
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時(shí),第一個(gè)連接作為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);
}
}
//連接一個(gè)只讀的mysql數(shù)據(jù)庫(kù)
function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){
if($this->link_rw == null)
$this->link_rw = $this->link;
$this->link = null;
//不產(chǎn)生halt錯(cuò)誤
$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){
//如果已經(jīng)選擇過(guò)數(shù)據(jù)庫(kù)則需要操作一次
@mysql_select_db($this->cur_db, $this->link_ro);
}
}else{
//連接失敗
//echo "link ro failed!<br>";
$this->link = &$this->link_rw;
}
}
//設(shè)置一系列只讀數(shù)據(jù)庫(kù)并且連接其中一個(gè)
function set_ro_list($ro_list){
if(is_array($ro_list)){
//隨機(jī)選擇其中一個(gè)
$link_ro = $ro_list[array_rand($ro_list)];
$this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro['dbpw']);
}
}
function select_db($dbname) {
//同時(shí)操作兩個(gè)數(shù)據(jù)庫(kù)連接
$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語(yǔ)句
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主從復(fù)制的原理及配置方法(比較詳細(xì))
- Mysql主從復(fù)制(master-slave)實(shí)際操作案例
- MySQL的主從復(fù)制步驟詳解及常見(jiàn)錯(cuò)誤解決方法
- 詳解MySQL實(shí)現(xiàn)主從復(fù)制過(guò)程
- MySQL數(shù)據(jù)庫(kù)InnoDB引擎主從復(fù)制同步經(jīng)驗(yàn)總結(jié)
- Mysql 5.7從節(jié)點(diǎn)配置多線程主從復(fù)制的方法詳解
- 詳解MySQL的主從復(fù)制、讀寫(xiě)分離、備份恢復(fù)
- mysql主從復(fù)制讀寫(xiě)分離的配置方法詳解
- MySQL主從同步、讀寫(xiě)分離配置步驟
- mysql 讀寫(xiě)分離(基礎(chǔ)篇)
- MySQL主從復(fù)制與讀寫(xiě)分離原理及用法詳解
相關(guān)文章
php實(shí)現(xiàn)倒計(jì)時(shí)效果
這篇文章主要介紹了php實(shí)現(xiàn)倒計(jì)時(shí)效果,寫(xiě)了一個(gè)考試系統(tǒng)剩余時(shí)間倒計(jì)時(shí)的顯示代碼和大家一起探討,需要的朋友可以參考下2015-12-12簡(jiǎn)單了解將WordPress中的工具欄移到底部的小技巧
這篇文章主要介紹了將WordPress中的工具欄移到底部的小技巧,隨著新版的Chrome和WordPress的發(fā)布,文中最后提到的顯示問(wèn)題也應(yīng)當(dāng)能夠得到解決,需要的朋友可以參考下2015-12-12php獲取網(wǎng)卡的MAC地址支持WIN/LINUX系統(tǒng)
這篇文章主要介紹了使用php獲取網(wǎng)卡的MAC地址支持WIN/LINUX系統(tǒng),需要的朋友可以參考下2014-04-04php實(shí)現(xiàn)session共享的實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于php如何實(shí)現(xiàn)session共享知識(shí)點(diǎn)內(nèi)容,有需要的朋友們跟著學(xué)習(xí)參考下。2019-09-09一個(gè)PHP驗(yàn)證碼類(lèi)代碼分享(已封裝成類(lèi))
驗(yàn)證碼的用途就不用多說(shuō)了,之前也寫(xiě)了一篇關(guān)于PHP驗(yàn)證碼的文章,PHP 驗(yàn)證碼的實(shí)現(xiàn)。但是沒(méi)有封裝成類(lèi)。下面就介紹一個(gè)PHP的一個(gè)驗(yàn)證碼類(lèi)。2011-07-07