1.下载安装包
wget https://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
2.解压
tar -xzvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz
cp -a mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit/ /usr/local/myapps/mysql-proxy
3.创建lua脚本目录和日志目录
mkdir /usr/local/myapps/mysql-proxy/lua
mkdir /usr/local/myapps/mysql-proxy/logs
4.拷贝读写分离配置文件和管理脚本到lua脚本目录
cp /usr/local/myapps/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua /usr/local/myapps/mysql-proxy/lua
cp /usr/local/myapps/mysql-proxy/share/doc/mysql-proxy/admin-sql.lua /usr/local/myapps/mysql-proxy/lua
5.默认超过4个连接数时,才开始读写分离,改为1
sed -i "s/min_idle_connections = 4/min_idle_connections = 1/g" /usr/local/myapps/mysql-proxy/lua/rw-splitting.lua
sed -i "s/max_idle_connections = 8/max_idle_connections = 1/g" /usr/local/myapps/mysql-proxy/lua/rw-splitting.lua
6.创建配置文件
cat << EOF > /etc/mysql-proxy.cnf
[mysql-proxy]
user = root
admin-username=root
admin-password=123456
proxy-address=192.168.73.128
proxy-lua-script = /usr/local/myapps/mysql-proxy/lua/rw-splitting.lua
admin-lua-script = /usr/local/myapps/mysql-proxy/lua/admin-sql.lua
proxy-backend-addresses = 172.16.100.12
proxy-read-only-backend-addresses = 172.16.100.13
log-file=/usr/local/myapps/mysql-proxy/logs/mysql-proxy.log
log-level=debug
EOF
7.启动mysql-proxy服务
/usr/local/myapps/mysql-proxy/bin/mysql-proxy --daemon --keepalive --defaults-file=/etc/mysql-proxy.cnf
8.停止mysql-proxy服务
ps -ef | grep mysql-proxy | grep -v grep | awk '{print $2}' |xargs kill -9