4.1 MySQL读写分离简介
MySQL-Proxy,是处在你的MySQL数据库客户和服务端之间的程序,它还支持嵌入性脚本语言Lua。这个代理可以用来分析、监控和变换(transform) 通信数据。
MySQL Proxy最强大的一项功能是实现“读 写分离(Read/Write Splitting)”。基本的原理是让主数据库处理事务性查询,而从数据库处理SELECT查询。数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库。
4.2 MySQL环境部署
实验环境:
实验环境是沿用第三章实验基础上的:
主机名 |
IP地址 |
作用 |
MySQL-Master |
192.168.140.128 |
MySQL主库服务器 |
MySQL-Slave |
192.168.140.129 |
MySQL 从库服务器 |
MySQL-Proxy |
192.168.140.130 |
MySQL 代理服务器 |
4.2.1 部署安装环境
[root@MySQL-Proxy ~]# rpm -q libevent glib2 pkgconfig libtool mysql-devel
[root@MySQL-Proxy ~]# cd /usr/local/src
[root@MySQL-Proxy src]# wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
[root@MySQL-Proxy src]#
wget http://gd.tuwien.ac.at/db/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.6.0.tar.gz
[root@MySQL-Proxy src]# wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
4.2.2 安装lua
[root@MySQL-Proxy src]# tar zxvf lua-5.1.4.tar.gz
[root@MySQL-Proxy src]# cd lua-5.1.4
[root@MySQL-Proxy lua-5.1.4]# vi Makefile
修改:
INSTALL_TOP= /usr/local/lua
[root@MySQL-Proxy lua-5.1.4]# make && make linux && make install
[root@MySQL-Proxy lua-5.1.4]# export LUA_CFLAGS="-I/usr/local/lua/include" LUA_LIBS="-L/usr/local/lua/lib -llua -ldl" LDFLAGS="-L/usr/local/libevent/lib -lm"
[root@MySQL-Proxy lua-5.1.4]# export CPPFLAGS="-I/usr/local/libevent/include"
[root@MySQL-Proxy lua-5.1.4]# export CFLAGS="-I/usr/local/libevent/include"
4.2.3 安装libevent
[root@MySQL-Proxy src]# tar zxvf libevent-1.4.13-stable.tar.gz
[root@MySQL-Proxy src]# cd libevent-1.4.13-stable
[root@MySQL-Proxy libevent-1.4.13-stable]# ./configure --prefix=/usr/local/libevent
[root@MySQL-Proxy libevent-1.4.13-stable]# make && make install
4.2.4 安装MySQL
[root@MySQL-Proxy src]# tar zxvf mysql-5.1.47.tar.gz
[root@MySQL-Proxy src]# cd mysql-5.1.47
[root@MySQL-Proxy mysql-5.1.47]# ./configure --prefix=/usr/local/mysql --without-server
4.2.5 设置环境变量
[root@MySQL-Proxy src]# export LUA_CFLAGS="-I/usr/local/lua/include" LUA_LIBS="-L/usr/local/lua/lib -llua -ldl" LDFLAGS="-L/usr/local/libevent/lib -lm"
[root@MySQL-Proxy src]# export CPPFLAGS="-I/usr/local/libevent/include"
[root@MySQL-Proxy src]# export CFLAGS="-I/usr/local/libevent/include"
4.2.6 安装MySQL-Proxy
注意:如果你安装更高版本的mysql-proxy需要升级你的glib。
[root@MySQL-Proxy src]# tar zxvf mysql-proxy-0.6.0.tar.gz
[root@MySQL-Proxy src]# cd mysql-proxy-0.6.0
[root@MySQL-Proxy mysql-proxy-0.6.0]# ./configure --prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql --with-lua
[root@MySQL-Proxy mysql-proxy-0.6.0]# make && make install
4.3 MySQL-Proxy配置
[root@MySQL-Proxy ~]# /usr/local/mysql-proxy/sbin/mysql-proxy --help-all
Usage:
mysql-proxy [OPTION...] - MySQL Proxy
Help Options:
-?, --help
--help-all
--help-admin
--help-proxy
admin module
--admin-address=
proxy-module
-proxy-address=
--proxy-read-only-backend-addresses=
--proxy-backend-addresses=
--proxy-skip-profiling
--proxy-fix-bug-25371
--proxy-lua-script=
--no-proxy
Application Options:
-V, --version
--daemon
--pid-file=
4.4 MySQL Proxy 启动
4.4.1 命令行启动
[root@MySQL-Proxy ~]# /usr/local/mysql-proxy/sbin/mysql-proxy \
> --proxy-read-only-backend-addresses=192.168.140.129:3306 \
> --proxy-backend-addresses=192.168.140.128:3306 \
> --proxy-lua-script=/usr/local/mysql-proxy/share/mysql-proxy/rw-splitting.lua &
检测是否开始监听:
[root@MySQL-Proxy ~]# netstat -ntlp | grep mysql
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 28739/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 28739/mysql-proxy
4.4.2 脚本启动
[root@MySQL-Proxy ~]# vi /etc/init.d/mysql-proxy
#!/bin/bash
PRODIR=/usr/local/mysql-proxy
LUA_PATH=$PRODIR/share/mysql-proxy
start(){
$PRODIR/sbin/mysql-proxy --proxy-read-only-backend-addresses=192.168.140.129:3306 --proxy-backend-addresses=192.168.140.128:3306 --proxy-lua-script=$LUA_PATH/rw-splitting.lua >>$PRODIR/mysql-proxy.log &
}
stop(){
kill $(pidof mysql-proxy)
if [ $? -ne 0 ];then
kill -9 $(pidof mysql-proxy)
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
Esac
[root@MySQL-Proxy ~]# chmod +x /etc/init.d/mysql-proxy
[root@MySQL-Proxy ~]# /etc/init.d/mysql-proxy start
4.5 MySQL Proxy 测试
4.5.1 测试环境部署
在MySQL-Master上创建测试数据库和用户。
mysql> create database unixhot;
mysql> GRANT ALL PRIVILEGES ON unixhot.* to proxytest@'%' identified by 'unixhot';
4.5.2 连接测试
[root@MySQL-Proxy ~]# mysql -h 192.168.140.130 -P 4040 -u proxytest -p
输入密码即可登录。
注意:在这里只做简单测试,具体请根据具体应用进行测试。由于目前MySQL Proxy技术还不够成熟,如果想在生产环境部署,请慎用。