- mysql-proxy
- –proxy-backend-addresses=narcissus:3306
- –proxy-backend-addresses=nostromo:3306
Copy code
3. Database read and write separation, 192.168.18.11 0 is responsible for writing Enter, 192.168.18.107 is responsible for reading data. Of course, you can also add a server for reading data.
- mysql-proxy
- –proxy-backend-addresses=192.168.18.110:3306
- –proxy-read-only-backend-addresses=192.168.18.107:3306
Copy code
This method does not separate reading and writing. mysql-proxy cannot distinguish which ones are sent to the slave server, and you need to use script control yourself. See the fourth method. 4. Lua script can well control the connection and distribution, as well as the query and returned result set. When using Lua scripts, you must use –proxy-lua-script to specify the name of the script. The script will not be read until a connection is generated, that is, there is no need to restart the service after modifying the script. mysql-proxy –proxy-lua-script=rw-splitting.lua –proxy-backend-addresses=192.168.18.110:3306 –proxy-read-only-backend-addresses=192.168.18.107:3306 Note: 1. The read-write separation mechanism of proxy is to first send the first few queries to the master to establish a connection. When the number of queries sent to the master exceeds the minimum value of the connection pool, the query begins 2. LAST_INSERT_ID cannot be sent to the main server. Just change line 226 to the following. elseif not is_insert_id and token.token_name == “TK_FUNCTION” then 3. When using the default rw-splitting.lua, it will prompt that the proxy-command cannot be found. I set the path of mysql-proxy to the system path, and then run it in the share directory and everything is OK. Enter cmd during operation. , then Then cd C:toolsmysql-proxyshare. 4. Garbled characters After connecting to the database through proxy, the string found is always garbled, even if set names ‘utf8’ is manually executed, it has no effect. Solution, mysql server must be set
-
class mysql_rw_php {
- //Number of queries
- var $querynum = 0;
- //Database connection for current operation
- var $link = null;
- //Character set
- var $charset ;
- //Current database
- var $cur_db = '';
- //Whether there is a valid read-only database connection
- var $ro_exist = false;
- //Read-only database connection
- var $link_ro = null;
- // Read and 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;
- //When rw is not initialized, the first connection is 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 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 the database has been selected, operation is required Once
- @mysql_select_db($this->cur_db, $this->link_ro);
- }
- }else{
- //Connection failed
- //echo "link ro failed!
";
- $this- >link = &$this->link_rw;
- }
- }
- //Set up a series of read-only databases and connect to one of them
- function set_ro_list($ro_list){
- if(is_array($ro_list)){
- / /Randomly select one of them
- $link_ro = $ro_list[array_rand($ro_list)];
- $this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro['dbpw'] ; $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;
- //Judge whether to 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 "
- MySQL Error
- Message: $message
- SQL: $sql
- Error: $dberror
- Errno.: $dberrno
";
exit();
}
}
?>
复制代码调用示例:
-
/****************************************
- *** mysql-rw-php version 0.1
- *** http://bbs.it-home.org
- *** 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'=>'bbs.it-home.org',
- 'dbuser'=>'jbxue',
- 'dbpw'=>'bbs.it-home.org',
- 'dbname'=>'test'
- );
- $db_ro = array(
- array(
- 'dbhost'=>'bbs.it-home.org:4306',
- 'dbuser'=>'jbxue',
- 'dbpw'=>'bbs.it-home.org'
- )
- );
- $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];
- }
- ?>
复制代码
|