mysql Proxy read-write separation configuration and php mysql read-write separation class

WBOY
Release: 2016-07-25 08:56:03
Original
1122 people have browsed it
  1. mysql-proxy
  2. –proxy-backend-addresses=narcissus:3306
  3. –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.

  1. mysql-proxy
  2. –proxy-backend-addresses=192.168.18.110:3306
  3. –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

  1. class mysql_rw_php {
  2. //Number of queries
  3. var $querynum = 0;
  4. //Database connection for current operation
  5. var $link = null;
  6. //Character set
  7. var $charset ;
  8. //Current database
  9. var $cur_db = '';
  10. //Whether there is a valid read-only database connection
  11. var $ro_exist = false;
  12. //Read-only database connection
  13. var $link_ro = null;
  14. // Read and write database connection
  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. //Read-only connection failed
  29. if(!$this->link && !$halt) return false;
  30. //When rw is not initialized, the first connection is as 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. //Connect a read-only mysql database
  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. //No halt error
  51. $this- >connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, false);
  52. if($this->link){
  53. //Connection successful
  54. //echo "link ro sussess!
    ;";
  55. $this->ro_exist = true;
    >
  56. $this->link_ro = $this->link;
  57. if($this->cur_db){
  58. //If the database has been selected, operation is required Once
  59. @mysql_select_db($this->cur_db, $this->link_ro);
  60. }
  61. }else{
  62. //Connection failed
  63. //echo "link ro failed!
    ";
  64. $this- >link = &$this->link_rw;
  65. }
  66. }
  67. //Set up a series of read-only databases and connect to one of them
  68. function set_ro_list($ro_list){
  69. if(is_array($ro_list)){
  70. / /Randomly select one of them
  71. $link_ro = $ro_list[array_rand($ro_list)];
  72. $this->connect_ro($link_ro['dbhost'], $link_ro['dbuser'], $link_ro['dbpw'] ; $dbname, $this->link_ro);
  73. }
  74. return @mysql_select_db($dbname, $this->link_rw);
  75. }
  76. function fetch_array($query, $result_type = MYSQL_ASSOC) {
  77. return mysql_fetch_array($ query, $result_type);
  78. }
  79. function fetch_one_array($sql, $type = '') {
  80. $qr = $this->query($sql, $type);
  81. return $this->fetch_array( $qr);
  82. }
  83. function query($sql, $type = '') {
  84. $this->link = &$this->link_rw;
  85. //Judge whether to select statement
  86. if($this- >ro_exist && preg_match ("/^(s*)select/i", $sql)){
  87. $this->link = &$this->link_ro;
  88. }
  89. $func = $type == ' UNBUFFERED' && @function_exists('mysql_unbuffered_query') ?
  90. 'mysql_unbuffered_query' : 'mysql_query';
  91. if(!($query = $func($sql, $this->link)) && $type != 'SILENT' ) {
  92. $this->halt('MySQL Query Error', $sql);
  93. }
  94. $this->querynum++;
  95. return $query;
  96. }
  97. function affected_rows() {
  98. return mysql_affected_rows($this->link);
  99. }
  100. function error() {
  101. return (($this->link) ? mysql_error($this->link) : mysql_error());
  102. }
  103. function errno() {
  104. return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
  105. }
  106. function result($query, $row) {
  107. $query = @mysql_result($query, $row);
  108. return $query;
  109. }
  110. function num_rows($query) {
  111. $query = mysql_num_rows($query);
  112. return $query;
  113. }
  114. function num_fields($query) {
  115. return mysql_num_fields($query);
  116. }
  117. function free_result($query) {
  118. return mysql_free_result($query);
  119. }
  120. function insert_id() {
  121. return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
  122. }
  123. function fetch_row($query) {
  124. $query = mysql_fetch_row($query);
  125. return $query;
  126. }
  127. function fetch_fields($query) {
  128. return mysql_fetch_field($query);
  129. }
  130. function version() {
  131. return mysql_get_server_info($this->link);
  132. }
  133. function close() {
  134. return mysql_close($this->link);
  135. }
  136. function halt($message = '', $sql = '') {
  137. $dberror = $this->error();
  138. $dberrno = $this->errno();
  139. echo "
  140. MySQL Error
  141. Message: $message
  142. SQL: $sql
  143. Error: $dberror
  144. Errno.: $dberrno
";
  • exit();
  • }
  • }
  • ?>
  • 复制代码

    调用示例:

    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. ?>
    复制代码


    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!