Read/write Splitting Operation example using MySQL Proxy (1)

Source: Internet
Author: User

MySQL ProxyOne of the most powerful features is the end of"Read/write splitting(Read/Write Splitting )". The principle is to allow the primary database to process transactional queries, but to process SELECT queries from the database. Database Replication is used to synchronize changes caused by transactional queries to the slave database in the cluster.

 
 
  1. max_conns = s.connected_clients   
  2.  
  3. max_conns_ndx = i   
  4.  
  5. end   
  6.  
  7. end   
  8.  
  9. end  

So far, we have found a database with easy connection

 
 
  1. if max_conns_ndx > 0 then   
  2.  
  3. proxy.connection.backend_ndx = max_conns_ndx   
  4.  
  5. end   
  6.  
  7. else  

Send to primary database

 
 
  1. end   
  2.  
  3. return proxy.PROXY_SEND_QUERY  

I always want to wait until the BETA version comes out and try again, but I still can't help but seduce myself. In the afternoon, I finally had time to test it.

See blog.chinaunix.net/u/8111/showart.php? Id = 451420)

I. necessary software:

1. LUA

You can download dpa.nsysu.edu.tw/downloads/mysql-proxy/from LUA /.

You may download the source code from the MYSQL official website.

I downloaded it here:

Mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.6.0.tar.gz

3. REPLICATION of B and C was abolished during the test. In this way, the SQL statement shows where it comes from.

In case the M-S is able to hold stop slave on SLAVE first)

Ii. Test host address:

1. MySQL Proxy installation address: 192.168.0.234 ()

2. MySQL server address: 192.168.0.235 (B)/236 (C)

Iii. installation experience:

Skip this step if the installation is based on the binary package.

1. LUA Installation

 
 
  1. [root@localhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local  
  2.  
  3. [root@localhost ~]# cd /usr/local/  
  4.  
  5. [root@localhost local]# mv lua-5.1 lua  
  6.  
  7. [root@localhost lua]# cd lua  
  8.  
  9. [root@localhost lua]#make local;make install; 

Export environment variables:

 
 
  1. [root@localhost lua]#export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm" 

2. Install MySQL Proxy

 
 
  1. [root@localhost ~]#tar -zxvf mysql-proxy-0.6.1-linux-rhel4-x86-32bit.tar.gz -C /usr/local/mysql/  
  2.  
  3. [root@localhost ~]#cd /usr/local/mysql  
  4.  
  5. [root@localhost mysql]#mv mysql-proxy-0.6.1-linux-rhel4-x86-32bit/ mysql-proxy  
  6.  
  7. [root@localhost sbin]# export PATH=$PATH:/usr/local/mysql/mysql-proxy/sbin/ 

4. Use MySQL Proxy

1. View help options

 
 
  1. [root@localhost ~]# mysql-proxy --help-all 

2. monopoly on MySQL

The MySQL server was installed. (This is not part of the installation process)

The initial structure and data of the tables on the two machines are the same, and both have the user t_girl_user.

 
 
  1. mysql> desc t;  
  2.  
  3. +-------+----------+------+-----+---------+----------------+  
  4.  
  5. | Field | Type | Null | Key | Default | Extra |  
  6.  
  7. +-------+----------+------+-----+---------+----------------+  
  8.  
  9. | id | int(11) | NO | PRI | NULL | auto_increment |   
  10.  
  11. | c_str | char(64) | NO | | | |   
  12.  
  13. +-------+----------+------+-----+---------+----------------+  
  14.  
  15. 2 rows in set (0.00 sec)  
  16.  
  17. 2 rows in set (0.00 sec) 

I inserted a registration entry on B.

 
 
  1. mysql> insert into t(c_str) values('B');  
  2.  
  3. Query OK, 1 row affected (0.00 sec) 

Insert a record on C.

 
 
  1. mysql> insert into t(c_str) value('C');  
  2.  
  3. Query OK, 1 row affected (0.00 sec)  
  4.  
  5. mysql>  

3. Enable MySQL-Proxy test for read/write splitting)

 
 
  1. [root@localhost sbin]# mysql-proxy --proxy-read-only-backend-addresses=192.168.0.236:3306 --proxy-backend-addresses=192.168.0.235:3306   
  2.  
  3. --proxy-lua-script=/usr/local/mysql/mysql-proxy/share/mysql-proxy/rw-splitting.lua &  
  4.  
  5. [1] 32554 

Easily enable MYSQL PROXY

 
 
  1. #!/bin/sh  
  2.  
  3. # export PATH=$PATH:/usr/local/mysql-proxy  
  4.  
  5. cd /usr/local/mysql-proxy  
  6.  
  7. ./mysql-proxy --proxy-read-only-backend-addresses=192.168.0.236:3306 --proxy-backend-addresses=192.168.0.235:3306 --proxy-lua-script=rw-splitting.lua >> /tmp/log 

In this example, only 192.168.0.236 is read-only and 192.168.0.235 is writable.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.