背景:如下图所示,主要通过mysql-proxy实现对两台mysql服务器的读写分离操作,两台mysql提供的测试数据库内容一致(这里主要通过手工同步两台mysql测试数据库的内容,没有使用mysql主从,主要是方便测试)
一、环境软件
1、 安装pkg-config
[[email protected] srv]yum install pkgconfig
2、安装glib2
[[email protected] src]yum install glib2
3、安装 libevent
[[email protected] srv]tar zxvf libevent-1.4.14b-stable.tar.gz
[[email protected] libevent-2.0.22-stable]./configure --prefix=/usr/local/libevent
[[email protected] libevent-2.0.22-stable]make
[[email protected] libevent-2.0.22-stable]make install
4、安装mysql-devel(只需安装mysql开发包)
[[email protected] src]yum install mysql-devel
5、LUA
yum install -y readline-devel ncurses-devel
[[email protected] src]wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
[[email protected] src]#tar -zxvf lua-5.3.1.tar.gz
[[email protected] lua-5.3.1]# vim Makefile
INSTALL_TOP= /usr/local/lua
[[email protected] lua-5.3.1]make linux
[[email protected] lua-5.3.1]make install
如果你的服务器是64位的,这时要调整一下Makefile:vi src/Makefile,在CFLAGS里加上-fPIC,否则会出错
6、设置mysql-proxy运行的环境变量
Vim /etc/profile
export LUA_CFLAGS="-I/usr/local/lua/include" LUA_LIBS="-L/usr/local/lua/lib -llua -ldl" LDFLAGS="-L/usr/local/libevent/lib -lm"
export CPPFLAGS="-I/usr/local/libevent/include"
export CFLAGS="-I/usr/local/libevent/include"
export PKG_CONFIG_PATH=/usr/lib/pkgconfig #rpm -ql pkgconfig查看其安装路径
然后执行 source /etc/profile
二、Mysql-proxy安装
Wget ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-Proxy/mysql-proxy-0.8.4.tar.gz
[[email protected] mysql-proxy-0.8.4]#
./configure --prefix=/usr/local/mysql-proxy --with-lua
[[email protected] mysql-proxy-0.8.4]make
[[email protected] mysql-proxy-0.8.4]make install
设置mysql-proxy环境变量
Vim /etc/profile
export PATH=$PATH:/usr/local/mysql-proxy/bin
Source /etc/profile
查看安装信息
[[email protected] mysql-proxy-0.8.4]# mysql-proxy -V
mysql-proxy 0.8.4
chassis: 0.8.4
glib2: 2.28.8
libevent: 2.0.22-stable
LUA: Lua 5.1.4
package.path: /usr/local/mysql-proxy/lib/mysql-proxy/lua/?.lua
package.cpath: /usr/local/mysql-proxy/lib/mysql-proxy/lua/?.so
-- modules
proxy: 0.8.4
三、mysql-proxy读写分离测试
1、读,写数据库信息一致
2、在192.168.1.121和192.168.1.124对mysql-proxy授权
mysql> grant all on jiang.* to ‘proxyuser‘@‘192.168.1.120‘ identified by ‘jiang‘;
3、配置mysql-proxy,实现读写分离(后台启动该程序&)
mysql-proxy --proxy-read-only-backend-addresses=192.168.1.124:3306
--proxy-backend-addresses=192.168.1.121:3306
--proxy-lua-script=/usr/local/mysql-proxy/lib/rw-splitting.lua" &
[[email protected] lib]# netstat -an | grep 4040 //确保已经启动了mysql-proxy
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN
vim /usr/local/mysql-proxy/lib/rw-splitting.lua
proxy.global.config.rwsplit = {
min_idle_connections = 1,
max_idle_connections = 2,
is_debug = false
}
说明:这是因为mysql-proxy会检测客户端连接, 当连接没有超过min_idle_connections预设值时, 不会进行读写分离, 即查询操作会发生到Master上(可通过show processlist查询连接情况)
参数说明:
--proxy-read-only-backend-addresses=192.168.1.124:3306 \ //定义后端只读从服务器地址
--proxy-backend-addresses=192.168.1.121:3306 \ //定义后端主服务器地址
--proxy-lua-script=/usr/local/mysql-proxy/lib/rw-splitting.lua" \ //定义lua读写分离脚本路径
4、远程连接mysql-proxy
[[email protected] lib]# mysql -uproxyuser -pjiang -h192.168.1.120 -P4040
5、插入数据,分析读写情况
mysql> INSERT INTO `jiang`.`personal_info` (`pi_id` ,`pi_name` ,`pi_tel` ,`pi_qq` ,`pi_email`)
VALUES (‘2‘, ‘mysqlproxy‘, ‘111111111111‘, ‘22222222‘, ‘[email protected]‘);
5.1 此次检查192.168.1.121和192.168.1.124的数据库jiang的情况。
192.168.1.121:如下数据
192.168.1.124:如下数据
说明:在192.168.1.120 mysql-proxy上,写的数据在192.168.1.121,而192.168.1.124没有写入数据。
接下来,为了实现分离状态的查看,需要重新打开客户端重新连接mysql-proxy,如下所示
[[email protected] ~]# mysql -uproxyuser -pjiang -h192.168.1.120 -P4040
如下所示,表示读取的数据库,那么该数据中并没有新插入的数据,其原因在于此时读取的数据在192.168.1.124上,而根据测试环境,192.168.1.124为只读数据库,所以插入的数据并没有写入到192.168.1.124上。