mysql-proxy 实现读写分离

【实验环境】
平台 centos6.4
IP分配
192.168.30.101 node1 mysql-proxy
192.168.30.102 node2 mysql主节点
192.168.30.103 node3 mysql从节点

192.168.30.104 node4 client


注意:
1、所有机器上都已编译安装好mysql5.5
2、实验的时候需要确保node2和node3的数据一致

3、实验的时候需要关闭node2和node3的主从复制,以便观察效果(如果不关闭主从复制,新的数据一写到主服务器上马上就通过主从复制复制到了从服务器,这样的话难以分辨mysql-proxy是否真的实现了读写分离)


【实验拓扑】

spacer.gif184919541.png


【实验步骤】

以下步骤均在node1(mysql-proxy)机器上进行


安装所需软件包

[root@node1 ~]# yum install -y readline-devel gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig* libevent* glib*


编译安装lua脚本

由于mysql-proxy 是通过lua脚本实现读写分离的,因此需要装上lua


源码包下载:http://www.lua.org/download.html
[root@node1 src]# wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
[root@node1 src]# tar xf lua-5.1.4.tar.gz

[root@node1 src]# cd lua-5.1.4


[root@node1 lua-5.1.4]# vi src/Makefile
找到 CFLAGS= -O2 -Wall $(MYCFLAGS) 这一行加上-fPIC,如下所示

CFLAGS= -O2 -Wall -fPIC $(MYCFLAGS)


修改INSTALL_TOP,如下所示:(指定安装目录)

INSTALL_TOP= /usr/local/lua


[root@node1 lua-5.1.4]# make linux

[root@node1 lua-5.1.4]# make install


安装mysql-proxy
下载地址:http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/

[root@node1 src]# wget http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.3.tar.gz


[root@node1 src]# tar xf mysql-proxy-0.8.3.tar.gz
[root@node1 src]# cd mysql-proxy-0.8.3
[root@node1 mysql-proxy-0.8.3]# ./configure LDFLAGS="-lm -ldl" LUA_CFLAGS="/usr/local/lua/bin/lua -I/usr/local/lua/include" LUA_LIBS="/usr/local/lua/lib -llua" --prefix=/usr/local/mysql-proxy --with-lua


[root@node1 mysql-proxy-0.8.3]# make && make install


确保存在rw-spitting.lua 这个脚本

[root@node1 ~]# cp /usr/local/src/mysql-proxy-0.8.3/lib/rw-splitting.lua /usr/local/mysql-proxy/share/doc/mysql-proxy/


启动mysql-proxy
[root@node1 ~]# /usr/local/mysql-proxy/bin/mysql-proxy --proxy-backend-addresses=192.168.30.102:3306 --proxy-read-only-backend-addresses=192.168.30.103:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua &
[3] 30840
[2] Done /usr/local/mysql-proxy/bin/mysql-proxy --proxy-backend-addresses=192.168.30.102:3306 --proxy-read-only-backend-addresses=192.168.30.103:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/mysql-proxy/rw-splitting.lua

[root@node1 ~]# 2013-09-17 00:14:46: (critical) plugin proxy 0.8.3 started


启动时注意上述脚本路径别写错,否则会报文件不存在的错误


连接mysql授权
Mysql主服务器(node2)
[root@node2 src]# mysql -uroot -p
mysql> grant all on *.* to 'proxy'@'192.168.30.101' identified by 'password';

Query OK, 0 rows affected (0.09 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


Mysql从服务器(node3)
[root@node3 src]# mysql -uroot -p
mysql> grant all on *.* to 'proxy'@'192.168.30.101' identified by 'password';

Query OK, 0 rows affected (0.09 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


在Mysql1,Mysql2 上面分别测试连接mysql-proxy(确保都能连通mysql-proxy)
[root@node2 src]# mysql -uproxy -ppassword -P4040 -h192.168.30.101

[root@node3 src]# mysql -uproxy -ppassword -P4040 -h192.168.30.101


测试读写分离效果
确保node2和node3 节点的mysql 数据一致
mysql> insert into t1 values(1,100);

Query OK, 1 row affected (0.02 sec)


mysql> select * from t1;
+------+------+
| id | ip |
+------+------+
| 1 | 100 |
+------+------+

1 row in set (0.08 sec)


client机器上
[root@node4 src]# mysql -uroot -p
mysql> grant all on *.* to 'proxy'@'192.168.30.101' identified by 'password';

Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


[root@node4 src]# mysql -uproxy -ppassword -h192.168.30.101 -P4040
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| proxy |
| test |
+--------------------+

5 rows in set (0.01 sec)


mysql> use proxy;

Database changed


mysql> select * from t1;
+------+------+
| id | ip |
+------+------+
| 1 | 100 |
+------+------+

1 row in set (0.01 sec)


添加记录
mysql> insert into t1 values(2,101);

Query OK, 1 row affected (0.03 sec)


mysql> insert into t1 values(3,102);

Query OK, 1 row affected (0.04 sec)


查询记录
mysql> select * from t1;
+------+------+
| id | ip |
+------+------+
| 1 | 100 |
+------+------+

1 row in set (0.01 sec)


可以看到通过读操作,并没有读到刚更新的记录


回到node2(mysql主节点),发现已更新写入的记录
mysql> select * from t1;
+------+------+
| id | ip |
+------+------+
| 1 | 100 |
| 2 | 101 |
| 3 | 102 |
+------+------+

3 rows in set (0.00 sec)


在node3(mysql从节点),并没有发现刚才更新的记录
mysql> select * from t1;
+------+------+
| id | ip |
+------+------+
| 1 | 100 |
+------+------+

1 row in set (0.00 sec)


由此验证,我们已经实现了MySQL读写分离,目前所有的写操作都全部在Master主服务器上,用来避免数据的不同步;
另外,所有的读操作都分摊给了其它各个Slave从服务器上,用来分担数据库压力。


a


你可能感兴趣的:(mysql)