本代碼是從uchome的代碼修改的,是因為要解決uchome的效率而處理的。
這個思維其實很久就有了,只是一直沒有去做,相信也有人有同樣的想法,如果有類似的,那真的希望提出相關的建議。
封裝的方式比較簡單,增加了只讀數據庫連接的接口擴展,不使用只讀數據庫也不影響原代碼使用。
有待以後不斷完善。。
為了方便,試試建立了google的一個項目:
http://code.google.com/p/MySQL-rw-PHP/
希望給有需要的朋友帶來幫助。
PHP實現的MySQL讀寫分離
主要特性:
英文比較爛,也寫幾個字吧
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();
}
}
?>
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];
}
?>
相關項目: