Mysql-proxy implementing read-Write separation scripts

Source: Internet
Author: User
Tags lua uuid

    1. Save the following script as/etc/init.d/mysql-proxy, giving permissions

      chmod 755/etc/init.d/mysql-proxy

#!/bin/bash## mysql-proxy this script starts and stops the mysql-proxy  daemon## chkconfig: - 78 30# processname: mysql-proxy# description:  mysql-proxy is a proxy daemon for mysql# Source function  Library:  /etc/rc.d/init.d/functionsprog= "/usr/local/mysql-proxy/bin/mysql-proxy" # Source networking  configuration.if [ -f /etc/sysconfig/network ]; then    .  /etc/sysconfig/networkfi# check that networking is up. [ ${networking} =  "No"  ] && exit 0# Set default  Mysql-proxy configuration. Admin_user= "admin" admin_passwd= "admin" admin_lua_script= "/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua "Proxy_options="--daemon "proxy_pid=/var/run/mysql-proxy.pidproxy_user=" Mysql-proxy "# Source  mysql-proxy configuration.if [ -f /etc/sysconfig/mysql-proxy ]; then    .  /etc/sysconfig/mysql-proxyfiretval=0start ()  {    echo -n $ "Starting   $prog:  "    daemon  $prog   $PROXY _options --pid-file= $PROXY _pid  --proxy-address= "$PROXY _address"  --user= $PROXY _user --admin-username= "$ADMIN _user"  -- admin-lua-script= "$ADMIN _lua_script"  --admin-password= "$ADMIN _password"     retval=$?     echo    if [  $RETVAL  -eq 0 ]; then         touch /var/lock/subsys/mysql-proxy     fi}stop ()  {    echo -n $ "stopping  $prog: "      killproc -p  $PROXY _pid -d 3  $prog     RETVAL=$?     echo    if [  $RETVAL  -eq 0 ]; then         rm -f /var/lock/subsys/mysql-proxy        rm -f   $PROXY _pid    fi}# see how we were called.case  "$"  in    start)         start         ;;     stop)         stop         ;;     restart)         stop         start        ;;     condrestart|try-restart)         if status  -p  $PROXY _pidfile  $prog  >&/dev/null; then            stop             start        fi        ;;     status)         status -p  $PROXY _pid   $prog         ;;     *)         echo  "usage: $0 { Start|stop|restart|reload|status|condrestart|try-restart} "         retval=1        ;; esacexit  $RETVAL



2. Provide the configuration file/etc/sysconfig/mysql-proxy for the service script, as shown in the following:

# Options for Mysql-proxy admin_user= "admin" admin_password= "admin" admin_address= "admin_lua_script="/usr/local/ Mysql-proxy/share/doc/mysql-proxy/admin.lua "proxy_address=" "proxy_user=" Mysql-proxy "PROXY_OPTIONS="--daemon-- Log-level=info--log-use-syslog "


#注意最后一行, it needs to be modified according to the actual production scene, for example;

proxy_options= "--daemon--log-level=info--log-file=/var/log/mysql-proxy.log--plugins=proxy

--plugins=admin--proxy-backend-addresses=192.168.141.101:3306--proxy-read-only-backend-ad

dresses=192.168.141.102:3306--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy

/rw-splitting.lua "


3. Copy the following to create the Admin.lua file and save it to the/usr/local/mysql-proxy/share/doc/mysql-proxy/directory:

--[[ $%beginlicense%$ copyright  (c)  2007, 2012, oracle and/or its  affiliates. All rights reserved. This program is free software;  you can redistribute it and/or modify it under the terms  of the GNU General Public License as published by the  Free software foundation; version 2 of the license. this program  is distributed in the hope that it will be useful, but  WITHOUT ANY WARRANTY; without even the implied warranty of  Merchantability or fitness for a particular purpose. see the gnu  General Public License for more details. You should have  Received a coPy of the gnu general public license along with this program ;  if not, write to the free software foundation, inc., 51  franklin st, fifth floor, boston, ma 02110-1301  usa $% Endlicense%$ --]]   function set_error (errmsg)  proxy.response = {type  = proxy. mysqld_packet_err,errmsg = errmsg or  "Error"}endfunction read_query (PACKET) if  Packet:byte ()  ~= proxy.com_query thenset_error ("[admin] we only handle  text-based queries  (com_query) ") Return proxy. Proxy_send_resultendlocal query = packet:sub (2) local rows = { }local  Fields = { }if query:lower ()  ==  "Select * from backends"   thenfields = { { name =  "BackEnd_ndx ",    type = proxy. mysql_type_long },{ name =  "Address",   type = proxy. mysql_type_string },{ name =  "state",   type = proxy. mysql_type_string },{ name =  "TYPE",   type = proxy. mysql_type_string },{ name =  "UUID",   type = proxy. mysql_type_string },{ name =  "Connected_clients",    type = proxy. mysql_type_long },}for i = 1,  #proxy. global.backends dolocal states =  {"Unknown", "Up", "Down"}local types = {"Unknown", "RW", "RO"}local b =  proxy.global.backends[i]rows[#rows  + 1] = {i,b.dst.name,           -- configured backend addressstates[b.state + 1],  -- the c-id is pushed&nbsP;down starting at 0types[b.type + 1],   -- the c-id is  pushed down starting at 0b.uuid,               -- the mysql server ' S uuid if it is  managedb.connected_clients  -- currently connected clients}endelseif query : Lower ()  ==  "Select * from help"  thenfields = { { name  =  "Command",    type = proxy. mysql_type_string },{ name =  "description",    type = proxy. mysql_type_string },}rows[#rows  + 1] = {  "Select * from help",   "Shows this help"  }rows[#rows  + 1] = {  "Select * from  backends ", " Lists the backends and their staTE " }elseset_error (" use  ' Select * from help '  to see the supported  commands ") Return proxy. Proxy_send_resultendproxy.response = {type = proxy. Mysqld_packet_ok,resultset = {fields = fields,rows = rows}}return proxy. Proxy_send_resultend




Mysql-proxy implementing read-Write separation scripts

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.