This article provides a detailed analysis of Mysql read/write splitting using PHP. For more information, see
This article provides a detailed analysis of Mysql read/write splitting using PHP. For more information, see
This code is modified from the uchome code because it needs to solve the uchome efficiency. This thinking has actually been around for a long time, but it has never been done. I believe some people have the same idea. If there are similar virtual hosts in Hong Kong, I really hope to put forward relevant suggestions.
The encapsulation method is relatively simple, and the interface extension of the read-only database connection is added. The virtual host does not use the read-only database and does not affect the use of the original code. It remains to be improved in the future ..
For convenience, I tried to build a google Project:
Hope to help friends in need.
Mysql read/write splitting implemented by PHP
Main features:
1. Simple read/write splitting
2. You can add more read-only databases to a primary database.
3. read/write splitting, but do not worry about unsupported features
4. Disadvantages: connecting two databases at the same time
Poor English. I want to write a few words in Hong Kong Space.
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 code:
Mysql_rw_php.class.php
The Code is as follows:
/*************************************** *
* ** Mysql-rw-php version 0.1
* ** Code by hqlulu # gmail.com
***
***
* ** Code modify from class_mysql.php (uchome)
****************************************/
Class mysql_rw_php {
// Number of queries
Var $ querynum = 0;
// The current database connection
Var $ link = null;
// Character Set
Var $ charset;
// Current Database
Var $ cur_db = '';
// Check whether a valid read-only database connection exists
Var $ ro_exist = false;
// Read-only database connection
Var $ link_ro = null;
// Read/write database connection
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;
// If rw is not initialized, the first connection acts as 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 );
}
}
// Connect to a read-only mysql database
Function connect_ro ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ pconnect = 0 ){
If ($ this-> link_rw = null)
$ This-> link_rw = $ this-> link;
$ This-> link = null;
// No halt Error
$ This-> connect ($ dbhost, $ dbuser, $ dbpw, $ dbname, $ pconnect, false );
If ($ this-> link ){
// Connection successful
// Echo "link ro sussess!
";
$ This-> ro_exist = true;
$ This-> link_ro = $ this-> link;
If ($ this-> cur_db ){
// If you have already selected a database, you need to perform the operation once.
@ Mysql_select_db ($ this-> cur_db, $ this-> link_ro );
}
} Else {
// Connection failed
// Echo "link ro failed!
";
$ This-> link = & $ this-> link_rw;
}
}
// Set a series of read-only databases and connect to one of them
Function set_ro_list ($ ro_list ){
If (is_array ($ ro_list )){
// Select one of them randomly
$ Link_ro = $ ro_list [array_rand ($ ro_list)];
$ This-> connect_ro ($ link_ro ['dbhost'], $ link_ro ['dbuser'], $ link_ro ['dbpw ']);
}
}
Function select_db ($ dbname ){
// Operate the connection between the two databases 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-> 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 whether a select statement is used
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"
MySQL Error
Message: $ Message
SQL: $ SQL
Error: $ Dberror
Errno.: $ Dberrno
";
Exit ();
}
}
?>