PHP to achieve MySQL read and write separation

Source: Internet
Author: User
Tags array connect mysql mysql query pconnect php code split mysql database
This code is modified from Uchome code because it is handled in order to solve the efficiency of uchome. PHP implementation of MySQL read and write separation

This code is modified from Uchome code because it is handled in order to solve the efficiency of uchome.

PHP implementation of MySQL read and write separation

Main Features:

Simple Read and write separation
One master database, you can add more read-only databases
Read-Write separation but don't worry about some features not supported
Disadvantage: Connecting two databases at the same time
English sucks, write a few words.

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 in the same time

PHP Code:

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 {
Number of queries
var $querynum = 0;
Database connection for the current operation
var $link = null;
Character
var $charset;
Current database
var $cur _db = ';
Is there a valid read-only database connection
var $ro _exist = false;
Read-only database connection
var $link _ro = null;
Read and write database connections
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 ');
}
}

Read-only Connection failed
if (! $this->link &&! $halt) return false;

When RW is not initialized, the first connection as RW
if ($this-&GT;LINK_RW = null)
$this-&GT;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);
}
}
Connect a read-only MySQL database
function Connect_ro ($dbhost, $dbuser, $DBPW, $dbname = ", $pconnect = 0) {
if ($this-&GT;LINK_RW = null)
$this-&GT;LINK_RW = $this->link;
$this->link = null;
Do not generate halt error
$this->connect ($dbhost, $dbuser, $DBPW, $dbname, $pconnect, false);
if ($this->link) {
Connection Successful
echo "link ro sussess!<br>";
$this->ro_exist = true;
$this->link_ro = $this->link;
if ($this->cur_db) {
If you have already selected a database, you need to do it once
@mysql_select_db ($this->cur_db, $this->link_ro);
}
}else{
Connection failed
echo "link ro failed!<br>";
$this->link = & $this->link_rw;
}
}
Set up a series of read-only databases and connect one of them
function set_ro_list ($ro _list) {
if (Is_array ($ro _list)) {
Randomly select one of the
$link _ro = $ro _list[array_rand ($ro _list)];
$this->connect_ro ($link _ro[' dbhost '), $link _ro[' Dbuser '], $link _ro[' DBPW ']);
}
}
function select_db ($dbname) {
Operating two database connections at the same time
$this->cur_db = $dbname;
if ($this->ro_exist) {
@mysql_select_db ($dbname, $this->link_ro);
}
Return @mysql_select_db ($dbname, $this-&GT;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;
Determine if a SELECT statement
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;p adding:0. 5em;\ ">
<b>mysql error</b><br>
<b>message</b&gt: $message <br>
<b>sql</b&gt: $sql <br>
<b>error</b&gt: $dberror <br>
<b>errno.</b&gt: $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];
}
?>

Related projects:

Mysql_proxy
Mysql_proxy seems to have not recommended applications in the production environment, there may be some unresolved bugs
Mysql-master-master
Need Perl environment support, you can try it, say it is applied in production environment
Amoeba deformed worm
Performance heard is better than mysql-proxy, but some statements may not support, this need to consider whether the application of the



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.