#!/bin/bash# #chkconfig: 2345 94 06#. /etc/rc.d/init.d/functions# Load Script configuration file, provide variable value if [ -e /usr/mysql-proxy.conf ] ; then. /usr/mysql-proxy.conffi# Check to see if Mysql-proxy software is installed if ! ' cd /usr/local/mysql-proxy > /dev/null ' ; thentar xf /root/$VER -c /usr/local/ln -s /usr/local/ Mysql-proxy-0.8.5-linux-glibc2.3-x86-32bit /usr/local/mysql-proxyfistart () {/usr/local/mysql-proxy/ bin/mysql-proxy --daemon --log-level= $LOGLEVEL --log-file= "$LOGFILE" --plugins= ' proxy ' --proxy-backend-addresses= "$MASTERADDR" --proxy-read-only-backend-addresses= "$SLAVEADDR" -- proxy-lua-script= ' $PROXYLUA ' --plugins= ' admin ' --admin-username= "$ADMINUSER" --admin-password= " $PASSWD " --admin-lua-script=" $ADMINLUA "}stop () {killall mysql-proxy} #查看代理运行状态时, Be sure to have the MySQL client program status ()  {MYSQL -U$ADMINUSER -H$PROXYADDR -P$PASSWD&Nbsp;--port= $ADMINPORT -e "Select * from backends"}test () {if ' Netstat -ntpl | grep ' mysql-proxy ' > /dev/null ' ; thenstat=okelsestat=faultfi}case $1 instart) starttestecho "Start mysql-proxy ... [$STAT] ";; Stop) stopecho "Stop mysql-proxy ... [OK] ";; Restart) stopecho "Stop mysql-proxy ... [OK] "starttestecho " Start mysql-proxy ... [$STAT] ";; status) status; *|HELP) echo "usage:$0 {start|stop|restart|status|help]";; Esac
The configuration file that needs to be loaded, providing the variable value
Ver= "Mysql-proxy-0.8.5-linux-glibc2.3-x86-32bit.tar.gz"
Loglevel= "Info"
Logfile= "/var/log/mysql-proxy.log"
Masteraddr= "192.168.1.51:3306"
Slaveaddr= "192.168.1.53:3306"
Proxylua= "/usr/local/mysql-proxy/share/doc/mysql-proxy/rm-splitting.lua"
Adminuser= "Admin"
passwd= "Admin"
Adminlua= "/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
Proxyaddr= ' 192.168.1.54 '
proxyport=4040
adminport=4041
Admin.lua Script
function Set_error (errmsg)
Proxy.response = {
Type = Proxy. Mysqld_packet_err,
ErrMsg = errmsg or "error"
}
End
function Read_query (packet)
If Packet:byte () ~= Proxy.com_query Then
Set_error ("[admin] we only handle text-based queries (com_query)")
Return proxy. Proxy_send_result
End
Local query = Packet:sub (2)
Local rows = {}
Local fields = {}
If query:lower () = = "SELECT * from Backends" then
Fields = {
{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 do
Local states = {
"Unknown",
"Up",
"Down"
}
Local types = {
"Unknown",
"RW",
"Ro"
}
Local B = Proxy.global.backends[i]
rows[#rows + 1] = {
I
B.dst.name,--configured backend address
States[b.state + 1],--the C-id is pushed down starting at 0
Types[b.type + 1],--the C-id is pushed down starting at 0
B.uuid,--the MySQL Server ' s UUID if it is managed
B.connected_clients--Currently connected clients
}
End
ElseIf query:lower () = = "SELECT * from Help" then
Fields = {
{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"}
Else
Set_error ("Use ' SELECT * from Help ' to see the supported commands")
Return proxy. Proxy_send_result
End
Proxy.response = {
Type = Proxy. MYSQLD_PACKET_OK,
ResultSet = {
Fields = Fields,
rows = Rows
}
}
Return proxy. Proxy_send_result
End
Start-up script for read-write separation based on Mysql-proxy