1.主从服务器配置(略)
master:188.188.1.144
slave:188.188.1.175
在Master和slave 上配置代理的访问用户:
grant all privileges on *.* to 'root'@'188.188.1.1%' identified by '123456';
2.mysql-proxy配置(188.188.1.138)
2.1 安装前配置:
安装mysql客户端
检查如下包是否安装:
rpm -qa | grep name
gcc* gcc-c++* autoconf* automake* zlib* libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig* libevent* glib*
安装libmcrypt
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
安装 ncurses-devel
rpm -ivh ncurses-devel-5.5-24.20060715.i386.rpm
2.1 安装LUA
[root@localhost soft]# tar -zxvf lua-5.2.0.tar.gz
[root@localhost soft]# cd lua-5.2.0
[root@localhost lua-5.2.0]# ls
doc Makefile README src
[root@localhost lua-5.2.0]# make linux
[root@localhost lua-5.2.0]# make install
导出环境变量:
[root@localhost lua-5.2.0]# export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
2.2 安装mysql-proxy
32位RHEL5平台:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz
64位RHEL5平台:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.1-linux-rhel5-x86-64bit.tar.gz
解压安装:
[root@localhost soft]# tar -zxvf mysql-proxy-0.8.1-linux-rhel5-x86-32bit.tar.gz
[root@localhost soft]# mv mysql-proxy-0.8.1-linux-rhel5-x86-32bit /usr/local/mysql-proxy
[root@localhost soft]# cd /usr/local/mysql-proxy/
[root@localhost mysql-proxy]# ls
bin include lib libexec share
设置路径变量:
[root@localhost mysql-proxy]# vi /etc/profile
export PATH=$PATH:/usr/local/mysql-proxy/bin
[root@localhost mysql-proxy]# source /etc/profile
2.3 查看mysql-proxy选项
PROXY_PATH=/opt/mysql-proxy/bin //定义mysql-proxy服务二进制文件路径
PROXY_OPTIONS=”�Cadmin-username=root \ //定义内部管理服务器账号
--admin-password=password \ //定义内部管理服务器密码
--proxy-read-only-backend-addresses=192.168.10.131:3306 \ //定义后端只读从服务器地址
--proxy-backend-addresses=192.168.10.130:3306 \ //定义后端主服务器地址
--admin-lua-script=/opt/mysql-proxy/lib/mysql-proxy/lua/admin.lua \ //定义lua管理脚本路径
--proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua” \ //定义lua读写分离脚本路径
PROXY_PID=/opt/mysql-proxy/run/mysql-proxy.pid //定义mysql-proxy PID文件路径
$NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS \
--daemon \ //定义以守护进程模式启动
--keepalive \ //使进程在异常关闭后能够自动恢复
--pid-file=$PROXY_PID \ //定义mysql-proxy PID文件路径
--user=mysql \ //以mysql用户身份启动服务
--log-level=debug \ //定义log日志级别,由高到低分别有(error|warning|info|message|debug)
--log-file=/opt/mysql-proxy/log/mysql-proxy.log //定义log日志文件路径
2.4 修改读写分离脚本
修改读写分离脚本rw-splitting.lua
修改默认连接,进行快速测试,不修改的话要达到连接数为4时才启用读写分离
[root@localhost mysql-proxy]# vi rw-splitting.lua
....
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, #修改为1
max_idle_connections = 1, #修改为1
is_debug = true
}
end
2.5 启动服务:
[root@localhost ~]# mysql-proxy --admin-username=root --admin-password=123456 --admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua --proxy-read-only-backend-addresses=188.188.1.175:3306 --proxy-backend-addresses=188.188.1.144:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --log-level=info --log-file=/usr/local/mysql-proxy/logs/mysq-proxy.log &
[1] 30332
查看服务运行端口:
[root@localhost ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:4040 0.0.0.0:* LISTEN 30332/mysql-proxy
tcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN 30332/mysql-proxy
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 2660/portmap
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 3013/cupsd
tcp 0 0 0.0.0.0:759 0.0.0.0:* LISTEN 2697/rpc.statd
tcp 0 0 :::22 :::* LISTEN 2999/sshd
查看服务:
[root@localhost ~]# ps -ef | grep mysql-proxy
root 30332 3351 0 21:10 pts/0 00:00:00 /usr/local/mysql-proxy/libexec/mysql-proxy --admin-username=root --admin-password=123456 --admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua --proxy-read-only-backend-addresses=188.188.1.175:3306 --proxy-backend-addresses=188.188.1.144:3306 --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua --log-level=info --log-file=/usr/local/mysql-proxy/logs/mysq-proxy.log
root 30345 3351 0 21:12 pts/0 00:00:00 grep mysql-proxy
3.测试:
为了测试读写分离,先停掉从服务
[root@localhost logs]# mysql -uroot -p123456 -h188.188.1.138 -P4040
mysql> use ckl
查看表:
mysql> select * from qinglv;
[read_query] 188.188.1.138:56958
current backend = 0
client default db = ckl
client username = root
query = select * from qinglv
sending to backend : 188.188.1.175:3306
is_slave : true
server default db: ckl
server username : root
in_trans : false
in_calc_found : false
COM_QUERY : true
+-----------+------+
| name | age |
+-----------+------+
| ckl | 26 |
| xiong | 24 |
| ckl | 26 |
| xiong | 24 |
| kangle | 0 |
| kangkang | 26 |
| xiaoxiong | 24 |
| haha | 33 |
| ceshi | 34 |
| wukaka | 30 |
| heixiu | 28 |
| zengjia | 22 |
| mama | 23 |
| meko | 32 |
| kkk | 89 |
| jkl | 20 |
| jia | 23 |
+-----------+------+
17 rows in set (0.00 sec)
插入数据:
mysql> insert into qinglv values('wuba',25);
[read_query] 188.188.1.138:56958
current backend = 0
client default db = ckl
client username = root
query = insert into qinglv values('wuba',25)
sending to backend : 188.188.1.144:3306
is_slave : false
server default db: ckl
server username : root
in_trans : false
in_calc_found : false
COM_QUERY : true
Query OK, 1 row affected (0.01 sec)
查看表:
mysql> select * from qinglv;
[read_query] 188.188.1.138:56958
current backend = 0
client default db = ckl
client username = root
query = select * from qinglv
server default db:
client default db: ckl
syncronizing
sending to backend : 188.188.1.175:3306
is_slave : true
server default db:
server username : root
in_trans : false
in_calc_found : false
COM_QUERY : true
+-----------+------+
| name | age |
+-----------+------+
| ckl | 26 |
| xiong | 24 |
| ckl | 26 |
| xiong | 24 |
| kangle | 0 |
| kangkang | 26 |
| xiaoxiong | 24 |
| haha | 33 |
| ceshi | 34 |
| wukaka | 30 |
| heixiu | 28 |
| zengjia | 22 |
| mama | 23 |
| meko | 32 |
| kkk | 89 |
| jkl | 20 |
| jia | 23 |
+-----------+------+
17 rows in set (0.00 sec)
没有数据
启动主从同步,再查看:
mysql> select * from qinglv;
[read_query] 188.188.1.138:56958
current backend = 0
client default db = ckl
client username = root
query = select * from qinglv
sending to backend : 188.188.1.175:3306
is_slave : true
server default db: ckl
server username : root
in_trans : false
in_calc_found : false
COM_QUERY : true
+-----------+------+
| name | age |
+-----------+------+
| ckl | 26 |
| xiong | 24 |
| ckl | 26 |
| xiong | 24 |
| kangle | 0 |
| kangkang | 26 |
| xiaoxiong | 24 |
| haha | 33 |
| ceshi | 34 |
| wukaka | 30 |
| heixiu | 28 |
| zengjia | 22 |
| mama | 23 |
| meko | 32 |
| kkk | 89 |
| jkl | 20 |
| jia | 23 |
| bba | 12 |
| wuba | 25 |
+-----------+------+
19 rows in set (0.00 sec)
查看日志:
[root@localhost logs]# cat /usr/local/mysql-proxy/logs/mysq-proxy.log
2012-03-29 21:10:48: (message) mysql-proxy 0.8.1 started
2012-03-29 21:10:48: (message) proxy listening on port :4040
2012-03-29 21:10:48: (message) added read/write backend: 188.188.1.144:3306
2012-03-29 21:10:48: (message) added read-only backend: 188.188.1.175:3306
2012-03-29 21:14:45: (message) mysql-proxy 0.8.1 started
2012-03-29 21:14:45: (message) proxy listening on port :4040
2012-03-29 21:14:45: (message) added read/write backend: 188.188.1.144:3306
2012-03-29 21:14:45: (message) added read-only backend: 188.188.1.175:3306
至此,读写分离完成。