MySQL读写分离
场景描述:
数据库Master主服务器:192.168.203.138
数据库Slave从服务器:192.168.203.133
MySQL-Proxy调度服务器:192.168.203.134
以下操作,均是在192.168.203.134即MySQL-Proxy调度服务器上进行的。
3.1 MySQL的安装与配置
具体的安装过程与上文相同。
3.2 检查系统所需软件包
通过 rpm -qa | grep name 的方式验证以下软件包是否已全部安装。
gcc* gcc-c++* autoconf* automake* zlib*libxml* ncurses-devel* libmcrypt* libtool* flex* pkgconfig*
libevent* glib*
yum install epel* -y
若缺少相关的软件包,可通过yum -y install方式在线安装,或直接从系统安装光盘中找到并通过rpm -ivh方式安装。
3.3 编译安装lua
MySQL-Proxy的读写分离主要是通过rw-splitting.lua脚本实现的,因此需要安装lua。
lua可通过以下方式获得
从http://www.lua.org/download.html下载源码包
从rpm.pbone.net搜索相关的rpm包
download.fedora.redhat.com/pub/fedora/epel/5/i386/lua-5.1.4-4.el5.i386.rpm
download.fedora.redhat.com/pub/fedora/epel/5/x86_64/lua-5.1.4-4.el5.x86_64.rpm
上面的链接不能链接使用下面地址
wgethttp://110.96.193.10:82/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/down1.chinaunix.net/distfiles/lua-5.1.tar.gz
这里我们建议采用源码包进行安装
cd /opt/install
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zvfx lua-5.1.4.tar.gz
cd lua-5.1.4
vi src/Makefile
在 CFLAGS= -O2 -Wall $(MYCFLAGS) 这一行记录里加上-fPIC,更改为 CFLAGS=-O2 -Wall -fPIC $(MYCFLAGS) 来避免编译过程中出现错误。
make linux
出现系列错误提示解决办法
make linux
在包含自 lua.h:16 的文件中,
从 lua.c:15:
luaconf.h:275:31: 错误:readline/readline.h:没有那个文件或目录
luaconf.h:276:30: 错误:readline/history.h:没有那个文件或目录
lua.c: In function‘pushline’:
lua.c:182: 警告:隐式声明函数‘readline’
lua.c:182: 警告:赋值时将整数赋给指针,未作类型转换
lua.c: In function‘loadline’:
lua.c:210: 警告:隐式声明函数‘add_history’
make[2]: *** [lua.o] 错误 1
make[2]: Leavingdirectory `/data0/software/lua-5.1.4/src’
make[1]: *** [linux] 错误 2
make[1]: Leavingdirectory `/data0/software/lua-5.1.4/src’
make: *** [linux] 错误 2
yum install libtermcap-devel ncurses-devellibevent-devel readline-devel
make install
cp etc/lua.pc /usr/lib/pkgconfig/
exportPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig
3.4 安装配置MySQL-Proxy
MySQL-Proxy可通过以下网址获得:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/
上面地址不能获取地址的话使用下面地址
wgethttp://mirrors.sohu.com/mysql/MySQL-Proxy/mysql-proxy-0.8.4-linux-rhel5-x86-64bit.tar.gz
推荐采用已经编译好的二进制版本,因为采用源码包进行编译时,最新版的MySQL-Proxy对automake,glib以及libevent的版本都有很高的要求,而这些软件包都是系统的基础套件,不建议强行进行更新。
并且这些已经编译好的二进制版本在解压后都在统一的目录内,因此建议选择以下版本:
32位RHEL5平台:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-rhel5-x86-32bit.tar.gz
64位RHEL5平台:
http://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-rhel5-x86-64bit.tar.gz
测试平台为RHEL5 32位,因此选择32位的软件包
wgethttp://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.4-linux-rhel5-x86-64bit.tar.gz
tar xzvf mysql-proxy-0.8.4-linux-rhel5-x86-64bit.tar.gz
mv mysql-proxy-0.8.4-linux-rhel5-x86-64bit/opt/mysql-proxy
创建mysql-proxy服务管理脚本
mkdir /opt/mysql-proxy/init.d/
vim mysql-proxy
01 #!/bin/sh
02 #
03 #mysql-proxy This script starts and stops the mysql-proxy daemon
04 #
05 #chkconfig: - 78 30
06 #processname: mysql-proxy
07 #description: mysql-proxy is a proxy daemon to mysql
08
09 #Source function library.
10 ./etc/rc.d/init.d/functions
11
12 #PROXY_PATH=/usr/local/bin
13 PROXY_PATH=/opt/mysql-proxy/bin
14
15 prog="mysql-proxy"
16
17 #Source networking configuration.
18 ./etc/sysconfig/network
19
20 #Check that networking is up.
21 [${NETWORKING} = "no" ] && exit 0
22
23 #Set default mysql-proxy configuration.
24 #PROXY_OPTIONS="--daemon"
25 PROXY_OPTIONS="--proxy-read-only-backend-addresses=192.168.203.133:3306--proxy-backend-addresses=192.168.203.138:3306--proxy-lua-script=/opt/mysql-proxy/scripts/rw-splitting.lua"
26 PROXY_PID=/opt/mysql-proxy/run/mysql-proxy.pid
27
28 #Source mysql-proxy configuration.
29 if[ -f /etc/sysconfig/mysql-proxy ]; then
30 . /etc/sysconfig/mysql-proxy
31 fi
32
33 PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH
34
35 #By default it's all good
36 RETVAL=0
37
38 #See how we were called.
39 case"$1" in
40 start)
41 # Start daemon.
42 echo -n $"Starting $prog: "
43 $NICELEVEL $PROXY_PATH/mysql-proxy$PROXY_OPTIONS --daemon --pid-file=$PROXY_PID --user=mysql --log-level=warning--log-file=/opt/mysql-proxy/log/mysql-proxy.log
44 RETVAL=$?
45 echo
46 if [ $RETVAL = 0 ]; then
47 touch/var/lock/subsys/mysql-proxy
48 fi
49 ;;
50 stop)
51 # Stop daemons.
52 echo -n $"Stopping $prog: "
53 killproc $prog
54 RETVAL=$?
55 echo
56 if [ $RETVAL = 0 ]; then
57 rm -f/var/lock/subsys/mysql-proxy
58 rm -f $PROXY_PID
59 fi
60 ;;
61 restart)
62 $0 stop
63 sleep 3
64 $0 start
65 ;;
66 condrestart)
67 [ -e /var/lock/subsys/mysql-proxy ]&& $0 restart
68 ;;
69 status)
70 status mysql-proxy
71 RETVAL=$?
72 ;;
73 *)
74 echo "Usage: $0{start|stop|restart|status|condrestart}"
75 RETVAL=1
76 ;;
77 esac
78
79 exit$RETVAL
脚本参数详解:
==============================================
PROXY_PATH=/opt/mysql-proxy/bin //定义mysql-proxy服务二进制文件路径
PROXY_OPTIONS="--admin-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-proxyPID文件路径
--user=mysql \ //以mysql用户身份启动服务
--log-level=warning \ //定义log日志级别,由高到低分别有(error|warning|info|message|debug)
--log-file=/opt/mysql-proxy/log/mysql-proxy.log//定义log日志文件路径
==============================================
cp mysql-proxy /opt/mysql-proxy/init.d/
chmod +x/opt/mysql-proxy/init.d/mysql-proxy
mkdir /opt/mysql-proxy/run
mkdir /opt/mysql-proxy/log
mkdir /opt/mysql-proxy/scripts
配置并使用rw-splitting.lua读写分离脚本
最新的脚本我们可以从最新的mysql-proxy源码包中获取
cd /opt/install
wgethttp://mysql.cdpa.nsysu.edu.tw/Downloads/MySQL-Proxy/mysql-proxy-0.8.4.tar.gz
tar xzvf mysql-proxy-0.8.4.tar.gz
cd mysql-proxy-0.8.4
cp lib/rw-splitting.lua/opt/mysql-proxy/scripts
修改读写分离脚本rw-splitting.lua
修改默认连接,进行快速测试,不修改的话要达到连接数为4时才启用读写分离
vim /opt/mysql-proxy/scripts/rw-splitting.lua
=============================
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, //默认为4
max_idle_connections = 1, //默认为8
is_debug = false
}
end
=============================
修改完成后,启动mysql-proxy
/opt/mysql-proxy/init.d/mysql-proxy start