一)实验环境
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主从复制请参考
本文主要演示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-proxymysql-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/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" 指定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-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=$?echoif [ $RETVAL -eq 0 ]; thentouch /var/lock/subsys/mysql-proxyfi}stop() {echo -n $"Stopping $prog: "killproc -p $PROXY_PID -d 3 $progRETVAL=$?echoif [ $RETVAL -eq 0 ]; thenrm -f /var/lock/subsys/mysql-proxyrm -f $PROXY_PIDfi}# See how we were called.case "$1" instart)start;;stop)stop;;restart)stopstart;;condrestart|try-restart)if status -p $PROXY_PIDFILE $prog >&/dev/null; thenstopstartfi;;status)status -p $PROXY_PID $prog;;*)echo "Usage: $0 {start|stop|restart|reload|status|condrestart|try-restart}"RETVAL=1;;esacexit $RETVAL
chmod +x /etc/rc.d/init.d/mysql-proxy
chkconfig --add mysql-proxy
vim /etc/sysconfig/mysql-proxy# Options for mysql-proxyADMIN_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/ormodify it under the terms of the GNU General Public License aspublished by the Free Software Foundation; version 2 of theLicense.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-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 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
/etc/init.d/mysql-proxy start
查看端口:4041,3306已经启动
[root@localhost ~]# ss -lntState Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 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=3306Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 5Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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:
主服务器状态很快就能显示,从服务器看效果有点慢。主从分离操作完成。