使用mysql-proxy实现mysql的读写分离

时间:2022-03-23 07:15:47

 前言:

  MySQL读写分离是指让master处理写操作,让slave处理读操作,非常适用于读操作量比较大的场景,可减轻master的压力。使用mysql-proxy实现mysql的读写分离,mysql-proxy实际上是作为后端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,判断出是读操作还是写操作,然后分发至对应的mysql服务器上。mysql-proxy是官方提供的mysql中间件产品可以实现负载平衡,读写分离,failover等MySQL Proxy就是这么一个中间层代理,简单的说,MySQL Proxy就是一个连接池,负责将前台应用的连接请求转发给后台的数据库,并且通过使用lua脚本,可以实现复杂的连接控制和过滤,从而实现读写分离和负载平衡。对于应用来说,MySQL Proxy是完全透明的,应用则只需要连接到MySQL Proxy的监听端口即可。当然,这样proxy机器可能成为单点失效,但完全可以使用多个proxy机器做为冗余,在应用服务器的连接池配置中配置到多 个proxy的连接参数即可。

实验系统:CentOS 6.6_x86_64

实验前提:防火墙和selinux都关闭

实验说明:本实验共有4台主机,IP分配如拓扑

实验软件:mariadb-10.0.20  mysql-proxy-0.8.5-linux-el6-x86-64bit

下载地址:http://pan.baidu.com/s/1i3F5Pop

实验拓扑:

    使用mysql-proxy实现mysql的读写分离

一、准备工作:

  1.将主机名称改为如下所示:

    使用mysql-proxy实现mysql的读写分离

  2.将hosts文件添加如下内容:

    使用mysql-proxy实现mysql的读写分离

  3.master、slave1和slave2安装mariadb:

使用mysql-proxy实现mysql的读写分离
tar xf mariadb-10.0.20-linux-x86_64.tar.gz  -C /usr/local/
cd /usr/local/
ln -sv mariadb-10.0.20-linux-x86_64 mysql
useradd -r mysql
mkdir -pv /mydata/data
chown -R mysql.mysql /mydata/data/
cd mysql/
chown -R root.mysql .
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
cp support-files/my-large.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
使用mysql-proxy实现mysql的读写分离

二、配置主从复制

  1.master上配置my.cnf:

使用mysql-proxy实现mysql的读写分离
[mysqld]
server-id       = 1
datadir = /mydata/data
log-bin = /mydata/data/master-bin
binlog_format = ROW
sync_binlog = 1                        //确保每次事务提交之前都能将二进制日志同步磁盘上
使用mysql-proxy实现mysql的读写分离

  2.slave1上配置my.cnf:

使用mysql-proxy实现mysql的读写分离
[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id       = 2
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only = 1
sync_master_info = 1                   //及时同步master文件
sync_relay_log = 1                     //及时同步relay-log文件
sync_relay_log_info = 1                //及时同步relay-log-info文件
使用mysql-proxy实现mysql的读写分离

  3.slave2上配置my.cnf:

使用mysql-proxy实现mysql的读写分离
[mysqld]
#log-bin=mysql-bin
#binlog_format=mixed
server-id       = 3
datadir = /mydata/data
relay_log = /mydata/data/relay-log
read_only = 1
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
使用mysql-proxy实现mysql的读写分离

  4.在master上创建复制用户:

service mysqld start
/usr/local/mysql/bin/mysql
------------------------------------------->
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO slave@192.168.19.% IDENTIFIED BY 123456;
FLUSH PRIVILEGES;

  5.在master上查看二进制日志位置:

SHOW MASTER LOGS;

    使用mysql-proxy实现mysql的读写分离

  6.两台slave上操作:

CHANGE MASTER TO MASTER_HOST=master,MASTER_USER=slave,MASTER_PASSWORD=123456,MASTER_LOG_FILE=master-bin.000001,MASTER_LOG_POS=637;
START SLAVE;
SHOW SLAVE STATUSG

    使用mysql-proxy实现mysql的读写分离

三、安装mysql-proxy

  1.此实验中19.79为mysql-proxy服务器,所以软件安装在此主机上:

tar xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local/
cd /usr/local/
ln -sv mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy
useradd -r mysql-proxy

  2.提供服务脚本:

使用mysql-proxy实现mysql的读写分离
vim /etc/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
<---------------------------------------------------------
chmod x /etc/init.d/mysql-proxy
chkconfig --add mysql-proxy
使用mysql-proxy实现mysql的读写分离

  3.为服务脚本提供配置文件:

使用mysql-proxy实现mysql的读写分离
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.19.66:3306 --proxy-read-only-backend-addresses=192.168.19.74:3306
 --proxy-read-only-backend-addresses=192.168.19.76:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"

//--daemon:以守护进程模式启动mysql-proxy
//--proxy-backend-addresses:后端可读写的mysql服务器的地址和端口

//--proxy-read-only-backend-addresses:后端只读mysql服务器的地址和端口
//--proxy-lua-script:完成mysql代理功能的Lua脚本
使用mysql-proxy实现mysql的读写分离

   4.提供admin.lua文件:

使用mysql-proxy实现mysql的读写分离
vim /usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua
------------------------------------------------------------------------------>
--[[ $