mysql-proxy 实现mysql读写分离 (存档)

OS:centos6.8

Package:  mysql-5.6.40-linux-glibc2.12-x86_64

                  mysql-proxy-0.8.5-linux-glibc2.3-x86_64


节点准备:write.test.com      ipaddress: 192.168.1.1

                read.test.com       ipaddress: 192.168.1.2

                proxy1.test.com    ipaddress: 192.168.1.10


1.    在两个节点中安装好mysql,并设置主从复制。

1.1 master配置

[mysqld]
binlog-format=ROW
log-bin=/mydata/binlog/master-bin
log-slave-updates=true
gtid-mode=on 
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=1
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=192.168.1.1

1.2 slave配置

[mysqld]
binlog-format=ROW
log-bin=/mydata/binlog/slave-bin
log-slave-updates=true
gtid-mode=on 
enforce-gtid-consistency=true
master-info-repository=TABLE
relay-log-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log_events=1
server-id=11
report-port=3306
port=3306
datadir=/mydata/data
socket=/tmp/mysql.sock
report-host=192.168.1.2


1.3 创建用于mysql复制的用户

mysql> GRANT REPLICATION SLAVE ON *.* TO replayuser@'192.168.1.2' IDENTIFIED BY '123456';

mysql> FLUSH PRIVILEGES;

PS:如果不是使用GTID,需要在主节点查询 show master status 来确认二进制日志文件名称,以便启用slave节点时使用。 


1.4 设置slave节点的复制

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.1',MASTER_USER='replayuser',

         > MASTER_PASSWORD='123456', 

         > MASTER_AUTO_POSITION=1;

mysql> FLUSH PRIVILEGES;

PS1:没有启用GTID需要配置以下:

mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.1',MASTER_USER='replayuser',

         > MASTER_PASSWORD='123456' ,MASTER_LOG_FILE='master-bin.000001',

         > MASTER_LOG_POS=401;


PS2:如使用 show slave status\G 发现slave同步错误,可以检查GTID的问题(Retrieved_Gtid_Set:)

mysql> STOP SLAVE;

mysql> RESET MASTER;

mysql> RESET SLAVE;

mysql> SET GLOBAL GTID_PURGED="1aede835-75ed-11e8-9e80-000c2955603f:1-11"

mysql> START SLAVE;

mysql> SHOW SLAVE STATUS\G


1.5 其他功能

1.5.1 使用半同步复制功能

# 在主从节点中分别安装相关的插件

mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

1.5.2 启动半同步复制

#在master的配置中添加: rpl_semi_sync_master_enabled=ON

#在至少一个slave的配置中添加: rpl_semi_sync_slave_enabled=ON

#重启所有节点的mysql服务生效

#查看半同步功能是否启动

mysql> SHOW STATUS LIKE 'Rpl_semi_sync_master_yes_tx';


2.    安装mysql-proxy (通用二进制包)

# groupadd -r -g 309 mysql-proxy && useradd -r -u 309 -g 309 mysql-proxy  

# tar -xvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz -C /usr/local/

# ln -sv mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit mysql-proxy 

#添加mysql-proxy的PATH

# vim /etc/profile.d/mysql-proxy.sh

export PATH=$PATH:/usr/local/mysql-proxy/bin

#mysql-proxy命令使用

# mysql-proxy --help

#启动mysql-proxy

# mysql-proxy --daemon --log-level=error --log-file=/var/log/mysql-proxy.log --plugins="proxy" --proxy-backend-addresses="192.168.1.1:3306" --proxy-read-only-backend-addresses="192.168.1.2:3306" --proxy-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua" --plugins=admin --admin-username="admin" --admin-password="123456" --admin-lua-script="/usr/local/mysql-proxy/share/doc/mysql-proxy/admin.lua"

3.    使用SysV服务脚本来启动

# vim /etc/init.d/mysql-proxy

#!/bin/bash
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
# 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="123456"
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 u+x /etc/init.d/mysql-proxy

# chkconfig --add mysql-proxy

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

# vim /etc/sysconfig/mysql-proxy

# Options for mysql-proxy 
ADMIN_USER="admin"
ADMIN_PASSWORD="123456"
ADMIN_ADDRESS="0.0.0.0:3306"
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"

其中PROXY_OPTIONS="" 需要按照实际情况修改。proxy-backend-addresses和proxy-read-only-backend-addresses可重复使用多次,以实现指定多个读写服务器或只读服务器。例如:

PROXY_OPTIONS="--daemon --log-level=info --log-use-syslog --plugins=proxy --plugins=admin --proxy-backend-addresses=192.168.1.1:3306 --proxy-read-only-backend-addresses=192.168.1.2:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua"

PS:mysql-proxy的配置选项

mysql-proxy的配置选项大致可分为帮助选项、管理选项、代理选项及应用程序选项几类,

--help
--help-admin
--help-proxy
--help-all ———— 以上四个选项均用于获取帮助信息;

--proxy-address=host:port ———— 代理服务监听的地址和端口;
--admin-address=host:port ———— 管理模块监听的地址和端口;
--proxy-backend-addresses=host:port ———— 后端mysql服务器的地址和端口;
--proxy-read-only-backend-addresses=host:port ———— 后端只读mysql服务器的地址和端口;
--proxy-lua-script=file_name ———— 完成mysql代理功能的Lua脚本;
--daemon ———— 以守护进程模式启动mysql-proxy;
--keepalive ———— 在mysql-proxy崩溃时尝试重启之;
--log-file=/path/to/log_file_name ———— 日志文件名称;
--log-level=level ———— 日志级别;
--log-use-syslog ———— 基于syslog记录日志;
--plugins=plugin ———— 在mysql-proxy启动时加载的插件;
--user=user_name ———— 运行mysql-proxy进程的用户;
--defaults-file=/path/to/conf_file_name ———— 默认使用的配置文件路径;其配置段使用[mysql-proxy]标识;
--proxy-skip-profiling ———— 禁用profile;

--pid-file=/path/to/pid_file_name ———— 进程文件名;


5.    使用mysql-proxy管理功能

# server mysql-proxy start

# netstat -tnlp |grep mysql-proxy


# mysql -uadmin -p123456 -h192.168.1.10 --port=4041

mysql> SELECT * FROM backends;
+-------------+---------------------+-------+------+------+-------------------+
| backend_ndx | address                  | state | type   | uuid  | connected_clients |
+-------------+---------------------+-------+------+------+-------------------+
|           1        | 192.168.1.1:3306     | unknown  | rw     | NULL  |          0         |
|           2        | 192.168.1.2:3306     | unknown  | ro      | NULL  |          0        |
+-------------+---------------------+-------+------+------+-------------------+

2 rows in set (0.00 sec)


6.    测试读写分离

# mysql -uroot -p -h192.168.1.10 -e "create database testdb1;"

# mysql -uroot -p -h192.168.1.10 -e "show databases;"

# mysql -uadmin -p123456 -h192.168.1.10 --port=4041

mysql> SELECT * FROM backends;
+-------------+---------------------+-------+------+------+-------------------+
| backend_ndx | address                  | state | type   | uuid  | connected_clients |
+-------------+---------------------+-------+------+------+-------------------+
|           1        | 192.168.1.1:3306     | up      | rw     | NULL  |                 0         |
|           2        | 192.168.1.2:3306     | up      | ro      | NULL  |                 0        |
+-------------+---------------------+-------+------+------+-------------------+


你可能感兴趣的:(mysql)