安装前准备(三台虚拟机):
主机1:
172.16.24.77 安装msyql服务器端 mysql-proxy
主机2:
172.16.24.88 安装mysql服务器端(mysql主)
主机3:
172.16.24.99 安装mysql服务器端(mysql从)
(注,我这里的三台主机都是安装好了mysql的,如里你没有安装请参照前面的博客有安装过程)
一、安装mysql-proxy
所需安装包(mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz)
1)解压并创建链接:
[root@server1 ~]# tar xf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local
[root@server1 ~]# cd /usr/local
[root@server1 local]# ln -sv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit mysql-proxy
`mysql-proxy‘ -> `mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit‘
2)创建mysql-proxy用户。因为mysql-proxy要以这个用户的身份运行。
[root@server1 local]# cd mysql-proxy
[root@server1 mysql-proxy]# useradd mysql-proxy
3)为mysql-proxy提供服务服本
[root@server1 mysql-proxy]# vim /etc/rc.d/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/functions
prog="/usr/local/mysql-proxy/bin/mysql-proxy"
# Source networking configuration.
if [ -f /etc/sysconfig/network ]; then
. /etc/sysconfig/network
fi
# 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.pid
PROXY_USER="mysql-proxy"
# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ]; then
. /etc/sysconfig/mysql-proxy
fi
RETVAL=0
start() {
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 "$1" 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
;;
esac
exit $RETVAL
4)更改文件权限并设置开机启动
[root@server1 mysql-proxy]# chmod +x /etc/rc.d/init.d/mysql-proxy
[root@server1 mysql-proxy]# chkconfig --add mysql-proxy
5)为服务脚本提供配置文件/etc/sysconfig/mysql-proxy
[root@server1 mysql-proxy]# vim /etc/sysconfig/mysql-proxy
ADMIN_PASSWORD="admin"
ADMIN_ADDRESS=""
ADMIN_LUA_SCRIPT="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"
PROXY_ADDRESS="172.16.24.77:4040"
PROXY_USER="mysql-proxy"
PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=172.16.24.88:3306 --proxy-read-only-backend-addresses=172.16.24.99:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
(注:上面的配置文件就定义了172.16.24.99,为只读服务器,并指定了读写分离脚本的存放位置)
5)建立admin.lua文件
[root@server1 mysql-proxy]# vim share/doc/mysql-proxy/admin.lua
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
6)配置好了,现在就启动mysql-proxy:
[root@server1 mysql-proxy]# service mysql-proxy start
Starting /usr/local/mysql-proxy/bin/mysql-proxy: [ OK ]
[root@www ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port
LISTEN 0 128 ::1:6010
LISTEN 0 128 172.16.24.77:4040
LISTEN 0 128 *:4041
LISTEN 0 50 *:3306
7)连接mysql-proxy并验证:
[root@www ~]# mysql -uadmin -padmin -h 172.16.24.77 --port=4041
mysql> select * from backends;
+-------------+-------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+-------------------+---------+------+------+-------------------+
| 1 | 172.16.24.88:3306 | unknown | rw | NULL | 0 |
| 2 | 172.16.24.99:3306 | unknown | ro | NULL | 0 |
+-------------+-------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
本文出自 “linux” 博客,谢绝转载!
mysql的主从复制(二)mysql-proxy实现读写分离,布布扣,bubuko.com
mysql的主从复制(二)mysql-proxy实现读写分离
原文地址:http://8519539.blog.51cto.com/8509539/1405337