mysql-proxy实现负载均衡及读写分离

mysql-proxy具有代理的功能,将客户端的mysql请求轮询至后台的mysql-server。

环境介绍:

一台mysql-proxy(178)

两台mysql-server(180,122)

一台mysql-client(254)

在mysql-proxy上安装如下包:

1.安装 pkg-config:(在RHEL6上默认已经安装,不需安装)
 
tar zxvf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure
make
make install
 
确保 PKG_CONFIG_PATH 环境变量包含了相关的 pkg-config 配置文件路径:
 
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

(如果是系统自带的pkg-config 则export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig)

2.安装 libevent:
 
tar zxvf libevent-1.4.10-stable.tar.gz
cd libevent-1.4.10-stable
./configure
make
make install

3.安装 glib:
 
tar zxvf glib-2.20.0.tar.gz
cd glib-2.20.0
./configure
make
make install

4.安装 lua:
 
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
 
如果你的服务器是 64 位的,这时要调整一下 Makefile:vi src/Makefile,在 CFLAGS 里
加上-fPIC,否则会出错:

接下来不用执行常见的 configure,直接 make:
 
make linux
make install

5.安装 mysql-proxy: 

安装 pkg-config 配置文件,以便编译 mysql-proxy 时能找到 lua:
 
cp  etc/lua.pc /usr/local/lib/pkgconfig/lua.pc
 
如果没有执行此步骤的话,在编译安装 mysql-proxy 的时候,会得到类似下面的错误
信息: 
Package lua5.1 was not found in the pkg-config search path.
Perhaps you should add the directory containing `lua5.1.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lua5.1' found


tar zxvf mysql-proxy-0.7.0.tar.gz
cd mysql-proxy-0.7.0
./configure
make
make install
 
按照官方介绍做好启动脚本/etc/init.d/mysql-proxy

 

#!/bin/sh
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon to mysql

# Source function library.
. /etc/rc.d/init.d/functions

PROXY_PATH=/opt/mysql-proxy/sbin

prog="mysql-proxy"

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Set default mysql-proxy configuration.
PROXY_OPTIONS="--daemon"
PROXY_PID=/var/run/mysql-proxy.pid

# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ] ; then
        . /etc/sysconfig/mysql-proxy
fi

PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH

# By default it's all good
RETVAL=0

# See how we were called.
case "$1" in
  start)
        # Start daemon.
        echo -n $"Starting $prog: "
        daemon $NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --pid-file $PROXY_PID
        RETVAL=$?
        echo
        if [ $RETVAL = 0 ]; then
                touch /var/lock/subsys/mysql-proxy
        fi
        ;;
  stop)
        # Stop daemons.
        echo -n $"Stopping $prog: "
        killproc $prog
        RETVAL=$?
        echo
        if [ $RETVAL = 0 ]; then
                rm -f /var/lock/subsys/mysql-proxy
                rm -f $PROXY_PID
        fi
        ;;
  restart)
        $0 stop
        sleep 3
        $0 start
        ;;
  condrestart)
       [ -e /var/lock/subsys/mysql-proxy ] && $0 restart
       ;;
  status)
        status mysql-proxy
        RETVAL=$?
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status|condrestart}"
        RETVAL=1
        ;;
esac

exit $RETVAL

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

chmod +x /etc/init.d/mysql-proxy

接下来要修改参数脚本/etc/sysconfig/mysql-proxy,在其中加入如下语句: 

PROXY_OPTIONS="--proxy-backend-address=192.168.0.180:3306  --proxy-backend-addresses=192.168.0.122:3306 --daemon"

#service mysql-proxy start   会出现警告信息(ulimit -n 8192)

测试:

在 180上建数据库 DB1|t1|180

在 122上建数据库 DB1|t1|122

监听端口4040

设置grant语句

180>GRANT  ALL PRIVILEGES on *.* to root@192.168.0.178 identified by '******'

122>GRANT  ALL PRIVILEGES on *.* to root@192.168.0.178 identified by '******'

#iptables -I INPUT 1 -d 192.168.0.122/24 -p tcp --dport 3306 -j accept

#iptables -I INPUT 1 -d 192.168.0.180/24 -p tcp --dport 3306 -j accept

#mysql -uroot -p****** -P 4040 -h 192.168.0.178

测试时终端不要退,退了会看不出结果。(轮询)

 

二 读写分离

#mysql-proxy --help-all

查看得知 -b 可读写 -r 只读

在mysql-proxy源代码lib目录中 有读写分离脚本(rw-splitting.lua)

#cp  rw-splitting.lua    /usr/local/share 复制读写分离脚本到随便一个目录。

#vi  /etc/sysconfig/mysql-proxy

PROXY_OPTIONS="-P 192.168.0.178:3306 -b 192.168.0.253 -r   192.168.0.252  -r 192.168.0.251 -r 192.168.0.250 -s /usr/local/share --daemon"

测试可以实现读写分离。