一)实验环境

Mysql-Porxy代理器 192.168.1.130 mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz
Mysql主 192.168.1.122  mysql-5.5.33-linux2.6-x86_64.tar.gz
Mysql从 192.168.1.120  mysql-5.5.33-linux2.6-x86_64.tar.gz

mysql主从复制请参考http://shunzi.blog.51cto.com/8289655/1394383

本文主要演示mysql-porxy实现读写分离的操作,在192.168.1.130上实行

yum  -y install mysql安装上mysql,不用启动。

1)解压,并创建用户

tar xf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost 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
[root@localhost local]# useradd mysql-proxy

2)为mysql-proxy提供sysv服务脚本

 
  
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"  指定lua脚本
PROXY_OPTIONS="--daemon"运行在后台
PROXY_PID=/var/run/mysql-proxy.pid指定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

chmod +x /etc/rc.d/init.d/mysql-proxy
chkconfig --add mysql-proxy

 

vim /etc/sysconfig/mysql-proxy
# 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 --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.122:3306 --proxy-read-only-backend-addresses=192.168.1.120:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"
上面的ip是主从的ip地址

创建admin管理脚本

vim share/doc/mysql-proxy/admin.lua
--[[ $%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"
}
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

 启动mysql-proxy

/etc/init.d/mysql-proxy start

查看端口:4041,3306已经启动

[root@localhost ~]# ss -lnt
State      Recv-Q Send-Q           Local Address:Port             Peer Address:Port
LISTEN     0      128                         :::111                        :::*
LISTEN     0      128                          *:111                         *:*
LISTEN     0      128                         :::22                         :::*
LISTEN     0      128                          *:22                          *:*
LISTEN     0      128                  127.0.0.1:631                         *:*
LISTEN     0      128                        ::1:631                        :::*
LISTEN     0      128                         :::36695                      :::*
LISTEN     0      100                        ::1:25                         :::*
LISTEN     0      100                  127.0.0.1:25                          *:*
LISTEN     0      128                          *:51321                       *:*
LISTEN     0      128                  127.0.0.1:6011                        *:*
LISTEN     0      128                        ::1:6011                       :::*
LISTEN     0      128                  127.0.0.1:6012                        *:*
LISTEN     0      128                        ::1:6012                       :::*
LISTEN     0      128                          *:4041                        *:*
LISTEN     0      128                          *:3306                        *:*

连接测试

[root@localhost ~]# mysql -uadmin -padmin -h192.168.1.130 --port=4041

登陆进去后发现能使用的命令很少,但是能查看你的服务器哪些是读写的

mysql> select * from backends;
+-------------+--------------------+---------+------+------+-------------------+
| backend_ndx | address            | state   | type | uuid | connected_clients |
+-------------+--------------------+---------+------+------+-------------------+
|           1 | 192.168.1.122:3306 | unknown | rw   | NULL |                 0 |
|           2 | 192.168.1.120:3306 | unknown | ro   | NULL |                 0 |
+-------------+--------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)

去主服务器创建能连接的账号

mysql> grant all on *.* to 'admin'@'192.168.%.%' identified by 'admin';
Query OK, 0 rows affected (0.07 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)

在mysql-proxy服务器连接通过在主服务创建的账号

[root@localhost mysql-proxy]# mysql -uadmin -padmin -h192.168.1.130 --port=3306
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.33-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

端口也可以加,默认就是3306.

mysql> select * from backends;
+-------------+--------------------+-------+------+------+-------------------+
| backend_ndx | address            | state | type | uuid | connected_clients |
+-------------+--------------------+-------+------+------+-------------------+
|           1 | 192.168.1.122:3306 | up    | rw   | NULL |                 0 |
|           2 | 192.168.1.120:3306 | up    | ro   | NULL |                 0 |
+-------------+--------------------+-------+------+------+-------------------+
2 rows in set (0.00 sec)

 

通过抓包看效果。
[root@station141 ~]# tcpdump -i eth0 -nn -XX dst host 192.168.1.122  and tcp dst port 3306
[root@station141 ~]# tcpdump -i eth0 -nn -XX dst host 192.168.1.120  and tcp dst port 3306

 ps:

    主服务器状态很快就能显示,从服务器看效果有点慢。主从分离操作完成。