Mysql Proxy Lua master-slave build read/write Splitting Process

Source: Internet
Author: User

Mysql Proxy LuaMaster-slave read/writeSeparationThe whole process is the content to be introduced in this article, mainly to understandMysql ProxyRead/writeSeparationThe whole process is explained in detail. For details, let's look at the details.

DownloadMysql ProxyThe latest version. The latest version is MySQL Proxy 0.8.1 alpha.

Follow these steps to install Mysql Proxy:

Extract

 
 
  1. Mysql Proxy-0.8.1-linux-glibc2.3-x86-32bit.gz  
  2. tar zxvf Mysql Proxy-0.8.1-linux-glibc2.3-x86-32bit.gz  
  3. cp -R Mysql Proxy-0.8.1-linux-glibc2.3-x86-32bit /usr/local/proxy-mysql 

Create or edit the/etc/Mysql Proxy. cnf file and add the following content (the specific parameters are modified according to the actual situation ):

 
 
  1. vi /etc/Mysql Proxy.cnf  
  2. [Mysql Proxy]  
  3. admin-username = zhangdongyu 
  4. admin-password = 123123 
  5. daemon = true 
  6. keepalive = true 
  7. proxy-backend-addresses = 192.168.0.88:3306  
  8. proxy-read-only-backend-addresses = 192.168.0.88:3307  
  9. proxy-lua-script = /usr/local/proxy-mysql/share/doc/Mysql Proxy/rw-splitting.lua  
  10. admin-lua-script = /usr/local/proxy-mysql/share/doc/Mysql Proxy/admin-sql.lua  
  11. log-file = /usr/local/proxy-mysql/cn.log  
  12. log-level = debug 

Main Parameter Annotations:

 
 
  1. Proxy-backend-addresses mysql master database write) Address
  2. Proxy-read-only-backend-addresses mysql read from database) Address
  3. Proxy-lua-script read/write splitting script
  4. Admin-lua-script admin script
  5. Admin-username: the same user must be created on both the master and slave databases)
  6. Admin-password Database password
  7. Daemon process running
  8. Keepalive maintains two processes for connection startup. Process 1 is used to monitor process 2. If process 2 dies and is automatically rebuilt, this parameter cannot be used in earlier versions)

Edit the profile/. bash_profile script

 
 
  1. Vi/etc/profile (or. bash_profile)
  2. LUA_PATH = "/usr/local/proxy-mysql/share/doc/Mysql Proxy /?. Lua"
  3. Export LUA_PATH
  4. Export PATH = $ PATH:/usr/local/proxy-mysql/bin
  5. : Wq
  6. Source/etc/profile

Create a Mysql Proxy. sh script for ease

 
 
  1. #!/bin/bash  
  2. mode=$1  
  3. if [ -z "$mode" ] ; then  
  4. mode="start" 
  5. fi  
  6. case $mode in  
  7. start)  
  8. Mysql Proxy --defaults-file=/etc/Mysql Proxy.cnf>/usr/local/proxy-mysql/cn.log   
  9. ;;  
  10. stop)  
  11. killall -9 Mysql Proxy  
  12. ;;  
  13. restart)  
  14. if $0 stop ; then  
  15. $0 start  
  16. else  
  17. echo "Restart failed!"  
  18. exit 1  
  19. fi  
  20. ;;  
  21. esac  
  22. exit 0 

Start Mysql Proxy

Sh script

Start:./Mysql Proxy. sh or./Mysql Proxy. sh start

Startup log

Restart:./Mysql Proxy. sh restart

Stop:./Mysql Proxy. sh stop

Test:

To achieve the test effect, modify the following two lines of the admin-sql.lua script

 
 
  1. Vi/usr/local/proxy-mysql/share/doc/Mysql Proxy/rw-splitting.lua
  2. If not proxy. global. config. rwsplit then
  3. Proxy. global. config. rwsplit = {
  4. Min_idle_connections = 1, # modify the minimum connection to 1
  5. Max_idle_connections = 2, # change the maximum connection to 2
  6. Is_debug = true # debug is true
  7. }
  8. End

Restart Mysql Proxy

Enable two terminals to connect to the proxy respectively:

 
 
  1. ONE : ./mysql -h192.168.0.88 -uzhangdongyu -p123123 -P4040  
  2. TWO : ./mysql -h192.168.0.88 -uzhangdongyu -p123123 -P4040 

You can see the connection success information:

Operate ONE client:

Execute the select statement to observe the Database log File

 
 
  1. select * from z_member_info; 

Observe the log file of the Master/Slave database and find that the query occurs in the master database?

 
 
  1. Master Database: 192.168.0.88: 3306
  2. Log File: tail-f/usr/local/mysql/localstate/mysqlexecute. log

This is because the Mysql Proxy agent detects the client connection. When the connection does not exceed the default value of min_idle_connections, read/write splitting is not performed, that is, the query operation will occur to the master database.

Operate the TWO client:

Execute the select statement to observe the Database log File

 
 
  1. select * from z_member_info; 

Observe the log files of the master and slave databases, and find that the master database does not have any records, and the query logs are stored in the slave database.

 
 
  1. SLAVE: 192.168.0.88: 3306
  2. Log File: tail-f/usr/local/mysql3307/localstate/mysqlexecute. log

The insert operation is performed on two terminals, and the write operation is always recorded by the master database 3306.

So far, the Mysql Proxy installation test is complete.

Summary:Mysql Proxy LuaMaster-slave read/writeSeparationThe entire process has been introduced. I hope this article will help you!

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.