在mysql主从基础上
利用mysql-proxy实现mysql读写分离
说明
master:192.168.235.128 slave:192.168.235.129 mysql-proxy:192.168.235.130:4040
原理
准备
#安装mysql-proxy需要依赖 yum -y install gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig* libevent* glib* readline*
编译安装lua
mysql-proxy的读写分离主要是通过rw-splitting.lua脚本实现的
安装mysql-proxy
wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
获取包,具体版本可用 uname -a
了解, mysql-proxy地址
tar xzvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz #解压 mv mysql-proxy-0.8.5-linux-el6-x86-64bit /opt/mysql-proxy #移动到自己创建目录 #/opt/mysql-proxy/bin目录下 ./mysql-proxy --help-all #查看命令
--proxy-read-only-backend-addresses=<host:port> #只读
--proxy-backend-addresses=<host:port> #写
--proxy-lua-script=<file> #指定脚本
cp /opt/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua /opt/mysql-proxy/scripts/rw-splitting.lua #复制
修改/opt/mysql-proxy/scripts/rw-splitting.lua
37 -- connection pool 38 if not proxy.global.config.rwsplit then 39 proxy.global.config.rwsplit = { 40 min_idle_connections = 1,#生效读写分离连接数 41 max_idle_connections = 8,
运行
./mysql-proxy --proxy-read-only-backend-addresses=192.168.235.129 --proxy-backend-addresses=192.168.235.128 --proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua --keepalive
测试
连接192.168.235.130:4040
本文来源: zef:http://daytxt.cn/blog/index.php/archives/227/
文章评论(0)