简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本 ,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多个proxy的连接参数即可。
典型配置案例:
1、最基础的应用,代理单个数据库服务器,这样就可以通过4040端口访问mysql数据库。
mysql-proxy –proxy-backend-addresses=192.168.18.110:3306
2、连接多个数据库服务器,假如A and B. 第一个客户端连接到 A,第二个连接到 B。
如果有一台停止服务时,代理会自动检测到,有新连接时会自动连接到一台正常的数据库。
mysql-proxy \
–proxy-backend-addresses=narcissus:3306 \
–proxy-backend-addresses=nostromo:3306
3、数据库读写分离,192.168.18.110负责写入,192.168.18.107负责读取数据,当然也可以再增加读取数据的服务器。
mysql-proxy \
–proxy-backend-addresses=192.168.18.110:3306 \
–proxy-read-only-backend-addresses=192.168.18.107:3306
这种方式常被理解为可以读写分离了,但是实际不是的。mysql-proxy不能区分哪些是发往从服务器的,还需要自己用脚本控制,见第四种方式。
4、 Lua 脚本能很好的控制连接和分布, 以及查询及返回的结果集.
使用Lua脚本时,必须使用 –proxy-lua-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
注意的问题:
1、proxy的读写分离机制是先把最初的几条查询发到master上建立连接,
当发送到master上的查询数超过连接池的最小值时开始把查询
2、LAST_INSERT_ID不能发送到主服务器上, 226 行修改为下面的就可以了
elseif not is_insert_id and token.token_name == “TK_FUNCTION” then
3、使用默认的rw-splitting.lua时,会提示找不到proxy-command,我把mysql-proxy的路径设置为系统路径,然后在 share目录下运行就一切Ok了,在运行中输入cmd,然后cd C:\tools\mysql-proxy\share。
4、字符乱码
通过proxy连上数据库之后,查到的字符串始终是乱码,即便手工执行了set names ‘utf8′也没有效果。
解决办法,mysql server必须设置
1.[mysqld]
2.skip-character-set-client-handshake
3. init-connect=’SET NAMES utf8′
4. default-character-set=utf8
- public function isReadOperation($sql) {
- return preg_match('/^\s*(SELECT|SHOW|DESC|PRAGMA)\s+/i',$sql);
- }
public function isReadOperation($sql) { return preg_match('/^\s*(SELECT|SHOW|DESC|PRAGMA)\s+/i',$sql); }
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!
"; - $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!
"; - $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 ">";
- MySQL Error
- Message: $message
- SQL: $sql
- Error: $dberror
- Errno.: $dberrno
- MySQL Error
- exit();
- }
- }
- ?>
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!
"; $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!
"; $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 "MySQL Error"; exit(); } } ?>
Message: $message
SQL: $sql
Error: $dberror
Errno.: $dberrno
调用方法
- /****************************************
- *** 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];
- }
- ?>
'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]; } ?>