- 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必須設定
- 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
- ";
- 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];
- }
- ?>
複製代碼 |