mysql 读写分离

mysql-proxy:简称mysql读写分离;主要目的是为了提高web站点的访问速度。


首先搭建mysql主从,参考:http://syklinux.blog.51cto.com/9631548/1737317


下载mysql-proxy:

cd /usr/local/src

wget http://cdn.mysql.com/archives/mysql-proxy/mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz

tar zxvf mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz

cp -r mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy

授权:

chmod 755 /usr/local/mysql-proxy

chown -R mysql:mysql /usr/local/mysql-proxy

设置环境变量:

vim /etc/profile

添加:

    LUA_PATH="/usr/local/mysql-proxy/share/doc/mysql-proxy/?.lua"   

    export LUA_PATH

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

退出后:

source /etc/profile

创建mysql用户:

mysql主从都需要创建相同的用户与密码:
grant all on *.* to 'test'@'%' identified by '123456';

新建mysql-proxy配置文件:

vim /etc/mysql-proxy.cnf

添加:

[mysql-proxy]

admin-username = test

admin-password = 123456

daemon = true

keepalive = true

proxy-backend-addresses = 192.168.1.2:3306

proxy-read-only-backend-addresses = 192.168.1.2:3307

proxy-lua-script = /usr/local/proxy-mysql/share/doc/mysql-proxy/rw-splitting.lua

admin-lua-script = /usr/local/proxy-mysql/share/doc/mysql-proxy/admin-sql.lua

log-file = /usr/local/proxy-mysql/cn.log

log-level = debug

主要参数:

proxy-backend-addresses            mysql主库(写)地址

proxy-read-only-backend-addresses     mysql从库(读)地址

proxy-lua-script                 读写分离脚本

admin-lua-script                 admin脚本

admin-username                  数据库用户名(主从上都需建立相同用户)

admin-password                  数据库密码

daemon                        daemon进程运行

keepalive                      保持连接(启动进程会有2个,一号进程用来监>视二号进行,如果二号进程死掉自动重建,此参数在旧版本中无法使用)

创建mysql-proxy启动脚本:

vim /etc/init.d/mysql-proxy

#!/bin/bash

mode=$1

if [ -z "$mode" ] ; then

mode="start"

fi

case $mode in

start)

mysql-proxy --defaults-file=/etc/mysql-proxy.cnf>/usr/local/proxy-mysql/cn.log &

;;

stop)

killall -9 mysql-proxy

;;

restart)

if $0 stop ; then

$0 start

else

echo  "Restart failed!"

exit 1

fi

;;

esac

exit 0

授权:

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

chkconfig --add mysql-proxy

启动mysql-proxy:

service mysql-proxy start

上一篇:Findbugs2 命令行使用说明


下一篇:Python基础教程笔记——条件,循环和其他语句