mysql Proxy讀寫分離配置與php mysql讀寫分離類

來源:互聯網
上載者:User
  1. mysql-proxy \
  2. –proxy-backend-addresses=narcissus:3306 \
  3. –proxy-backend-addresses=nostromo:3306
複製代碼

3、資料庫讀寫分離,192.168.18.110負責寫入,192.168.18.107負責讀取資料,當然也可以再增加讀取資料的伺服器。

  1. mysql-proxy \
  2. –proxy-backend-addresses=192.168.18.110:3306 \
  3. –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. class mysql_rw_php {
  2. //查詢個數
  3. var $querynum = 0;
  4. //當前操作的資料庫連接
  5. var $link = null;
  6. //字元集
  7. var $charset;
  8. //當前資料庫
  9. var $cur_db = '';
  10. //是否存在有效唯讀資料庫連接
  11. var $ro_exist = false;
  12. //唯讀資料庫連接
  13. var $link_ro = null;
  14. //讀寫資料庫連接
  15. var $link_rw = null;
  16. function mysql_rw_php(){
  17. }
  18. function connect($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0, $halt = TRUE) {
  19. if($pconnect) {
  20. if(!$this->link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  21. $halt && $this->halt('Can not connect to MySQL server');
  22. }
  23. } else {
  24. if(!$this->link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
  25. $halt && $this->halt('Can not connect to MySQL server');
  26. }
  27. }
  28. //唯讀串連失敗
  29. if(!$this->link && !$halt) return false;
  30. //未初始化rw時,第一個串連作為rw
  31. if($this->link_rw == null)
  32. $this->link_rw = $this->link;
  33. if($this->version() > '4.1') {
  34. if($this->charset) {
  35. @mysql_query("SET character_set_connection=$this->charset, character_set_results=$this->charset, character_set_client=binary", $this->link);
  36. }
  37. if($this->version() > '5.0.1') {
  38. @mysql_query("SET sql_mode=''", $this->link);
  39. }
  40. }
  41. if($dbname) {
  42. $this->select_db($dbname);
  43. }
  44. }
  45. //串連一個唯讀mysql資料庫
  46. function connect_ro($dbhost, $dbuser, $dbpw, $dbname = '', $pconnect = 0){
  47. if($this->link_rw == null)
  48. $this->link_rw = $this->link;
  49. $this->link = null;
  50. //不產生halt錯誤
  51. $this->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, false);
  52. if($this->link){
  53. //串連成功
  54. //echo "link ro sussess!
    ";
  55. $this->ro_exist = true;
  56. $this->link_ro = $this->link;
  57. if($this->cur_db){
  58. //如果已經選擇過資料庫則需要操作一次
  59. @mysql_select_db($this->cur_db, $this->link_ro);
  60. }
  61. }else{
  62. //串連失敗
  63. //echo "link ro failed!
    ";
  64. $this->link = &$this->link_rw;
  65. }
  66. }
  67. //設定一系列唯讀資料庫並且串連其中一個
  68. function set_ro_list($ro_list){
  69. if(is_array($ro_list)){
  70. //隨機播放其中一個
  71. $link_ro = $ro_list[array_rand($ro_list)];
  72. $this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro['dbpw']);
  73. }
  74. }
  75. function select_db($dbname) {
  76. //同時操作兩個資料庫連接
  77. $this->cur_db = $dbname;
  78. if($this->ro_exist){
  79. @mysql_select_db($dbname, $this->link_ro);
  80. }
  81. return @mysql_select_db($dbname, $this->link_rw);
  82. }
  83. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  84. return mysql_fetch_array($query, $result_type);
  85. }
  86. function fetch_one_array($sql, $type = '') {
  87. $qr = $this->query($sql, $type);
  88. return $this->fetch_array($qr);
  89. }
  90. function query($sql, $type = '') {
  91. $this->link = &$this->link_rw;
  92. //判斷是否select語句
  93. if($this->ro_exist && preg_match ("/^(\s*)select/i", $sql)){
  94. $this->link = &$this->link_ro;
  95. }
  96. $func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  97. 'mysql_unbuffered_query' : 'mysql_query';
  98. if(!($query = $func($sql, $this->link)) && $type != 'SILENT') {
  99. $this->halt('MySQL Query Error', $sql);
  100. }
  101. $this->querynum++;
  102. return $query;
  103. }
  104. function affected_rows() {
  105. return mysql_affected_rows($this->link);
  106. }
  107. function error() {
  108. return (($this->link) ? mysql_error($this->link) : mysql_error());
  109. }
  110. function errno() {
  111. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  112. }
  113. function result($query, $row) {
  114. $query = @mysql_result($query, $row);
  115. return $query;
  116. }
  117. function num_rows($query) {
  118. $query = mysql_num_rows($query);
  119. return $query;
  120. }
  121. function num_fields($query) {
  122. return mysql_num_fields($query);
  123. }
  124. function free_result($query) {
  125. return mysql_free_result($query);
  126. }
  127. function insert_id() {
  128. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  129. }
  130. function fetch_row($query) {
  131. $query = mysql_fetch_row($query);
  132. return $query;
  133. }
  134. function fetch_fields($query) {
  135. return mysql_fetch_field($query);
  136. }
  137. function version() {
  138. return mysql_get_server_info($this->link);
  139. }
  140. function close() {
  141. return mysql_close($this->link);
  142. }
  143. function halt($message = '', $sql = '') {
  144. $dberror = $this->error();
  145. $dberrno = $this->errno();
  146. echo "
  147. MySQL Error
  148. Message: $message
  149. SQL: $sql
  150. Error: $dberror
  151. Errno.: $dberrno
  152. ";
  153. exit();
  154. }
  155. }
  156. ?>
複製代碼

調用樣本:

  1. /****************************************
  2. *** mysql-rw-php version 0.1
  3. *** http://bbs.it-home.org
  4. *** http://code.google.com/p/mysql-rw-php/
  5. *** code modify from class_mysql.php (uchome)
  6. ****************************************/
  7. require_once('mysql_rw_php.class.php');
  8. //rw info
  9. $db_rw = array(
  10. 'dbhost'=>'bbs.it-home.org',
  11. 'dbuser'=>'jbxue',
  12. 'dbpw'=>'bbs.it-home.org',
  13. 'dbname'=>'test'
  14. );
  15. $db_ro = array(
  16. array(
  17. 'dbhost'=>'bbs.it-home.org:4306',
  18. 'dbuser'=>'jbxue',
  19. 'dbpw'=>'bbs.it-home.org'
  20. )
  21. );
  22. $DB = new mysql_rw_php;
  23. //connect Master
  24. $DB->connect($db_rw[dbhost], $db_rw[dbuser], $db_rw[dbpw], $db_rw[dbname]);
  25. //Method 1: connect one server
  26. $DB->connect_ro($db_ro[0][dbhost], $db_ro[0][dbuser], $db_ro[0][dbpw]);
  27. //Method 2: connect one server from a list by rand
  28. $DB->set_ro_list($db_ro);
  29. //send to rw
  30. $sql = "insert into a set a='test'";
  31. $DB->query($sql);
  32. //send to ro
  33. $sql = "select * from a";
  34. $qr = $DB->query($sql);
  35. while($row = $DB->fetch_array($qr)){
  36. echo $row[a];
  37. }
  38. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.