Use PHP to implement Mysql read/write splitting. This code is modified from the uchome code because it needs to solve the uchome efficiency. This thinking has actually existed for a long time, but it has never been done. I believe some people have the same idea that this code was modified from uchome's code, this is because uchome is efficient. This thinking has actually existed for a long time, but it has never been done. I believe some people have the same idea. if there is a similar idea, I really hope to put forward relevant suggestions.
The encapsulation method is simple, and the interface extension of the read-only database connection is added. The use of the original code is not affected if the read-only database is not used. It remains to be improved in the future ..
For convenience, I tried to build a google project:
Http://code.google.com/p/mysql-rw-php/
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. just 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 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
* ** 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;
// 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 ();
}
}
?>
Bytes. This thinking has actually existed for a long time, but it has never been done. I believe some people think the same way...