8.12.1.1 Mysql Proxy的安装
安装需要的基础组件,基本系统都可以满足lua的组件版本要求
安装lua(确定是否需要安装)
yum -y install gcc gcc-c++ autoconf automake zlib libxml ncurses-devel libmc rypt libtool flex
pkgconfig glib* readline-devel libevent-devel
? 安装glib
http://ftp.gnome.org/pub/gnome/sources/glib/
? 安装lua
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zvfx lua-5.1.4.tar.gz
注意
mkdir -p /usr/local/lua
cd lua-5.1.4;vi Makefile
sed -i ‘s#INSTALL_TOP=.#INSTALL_TOP=/usr/local/lua#g‘ Makefile 改安装路径
如果是64位,需改如下
sed -i ‘s#^CFLAGS=.#CFLAGS= -O2 -fPIC -Wall $(MYCFLAGS) #g‘ src/Makefile
make linux && make install
mkdir -p /usr/local/lua-5.1.4/lib/pkgconfig
cp ./etc/lua.pc /usr/local/lua-5.1.4/lib/pkgconfig
sed -i ‘s#prefix=.*#prefix= /usr/local/lua-5.1.4/#g‘ /usr/local/lua-5.1.4/lib/pkgconfig/lua.pc
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/glib/lib/pkgconfig/:/usr/local/lua-5.1.4/lib/pkgconfig
? 安装mysql-proxy
http://mirrors.sohu.com/mysql/MySQL-Proxy/
源码安装
wget http://launchpad.net/mysql-proxy/0.8/0.8.0/+download/mysql-proxy-0.8.0.tar.gz
tar -zxvf mysql-proxy-0.8.0.tar.gz
cd mysql-proxy-0.8.0/
./configure --pre --with-lua= /usr/local/lua-5.1.4 --with-mysql= /usr/lib64/mysql/mysql_config
make
make install
验证一下 :
mysql-proxy -V
二进制源码解压安装
cd /usr/local/software
tar -zxvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy
useradd mysql-proxy -s /sbin/nologin
chmod -R 775 /usr/local/mysql-proxy
chown -R mysql-proxy. /usr/local/mysql-proxy
? 配置环境变量
vim /etc/profile
LUA_PATH="/usr/local/mysql-proxy/share/doc/mysql-proxy/?.lua"
export LUA_PATH
export PATH=$PATH:/usr/local/mysql-proxy/bin
保存并退出后执行下面命令
source /etc/profile // 使变量立即生效
8.12.1.2 Mysql-proxy配置
修改mysql proxy的读写分离脚本的配置
vim /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
默认最小4个(最大8个)以上的客户端连接才会实现读写分离, 现改为最小1个最大2个,便于读写分离的测试
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, // 改为1
max_idle_connections = 2, // 改为2
is_debug = false
}
end
这是因为mysql-proxy会检测客户端连接, 当连接没有超过min_idle_connections预设值时,
不会进行读写分离, 即查询操作会发生到Master上.
8.12.1.3 Mysql-proxy 防火墙配置
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4040 -j ACCEPT
8.12.1.4 Mysql 主从授权
grant select,insert,update,delete on . to ‘test‘@‘192.168.1.202‘ identified by ‘321321‘; // 创建登录用户并授权
8.12.1.5 mysql-proxy启动
vim /etc/init.d/mysql-proxy // 新建一个shell文件,加入以下内容
#!/bin/sh
LUA_PATH=/usr/local/mysql-proxy/share/doc/mysql-proxy/?.lua:/usr/local/mysql-proxy/lib/mysql-proxy/lua/?.lua
mode=$1
if [ -z "$mode" ] ; then
mode="start"
fi
case $mode in
‘start‘)
mysql-proxy --daemon \
--log-level=debug \
--user=mysql-proxy \
--keepalive \
--log-file=/var/log/mysql-proxy.log \
--plugins="proxy" \
--proxy-backend-addresses=10.204.3.9:3306 \
--proxy-read-only-backend-addresses=10.204.3.10:3306 \
--proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua \
--plugins="admin" \
--admin-username="admin" \
--admin-password="admin" \
--admin-lua-script="/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua" ;;
‘stop‘)
killall mysql-proxy
;;
‘restart‘)
if $0 stop ; then
$0 start
else
echo "retart failed!!!"
exit 1
fi
;;
esac
exit 0
保存并退出
chmod 775 /etc/init.d/mysql-proxy // 授予执行权限
./mysql-proxy.sh // 启动mysql-proxy
配置参数详解
--daemon 采用daemon方式启动
--admin-address=:4401 指定mysql proxy的管理端口,不加参数,默认4041
--proxy-address=:3307 指定mysql proxy的监听端口,不加默认4040 也可以用 127.0.0.1:3307 表示
--proxy-backend-addresses=192.168.1.3:3306 指定master 写的mysql主机的端口
--proxy-read-only-backend-addresses=192.168.1.1:3306 指定只读的mysql主机端口
--proxy-read-only-backend-addresses=192.168.1.2:3306 指定另一个只读的mysql主机端口
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua 指定lua脚本,在这里,使用的是rw-splitting脚本,用于读写分离
--keepalive :在mysql-proxy崩溃时尝试重启之;
--plugins=plugin:在mysql-proxy启动时加载的插件;
--user=user_name :运行mysql-proxy进程的用户;
--defaults-file=/path/to/conf_file_name : 默认使用的配置文件路径;其配置段使用[mysql-proxy]标识;
--proxy-skip-profiling : 禁用profile;
--pid-file=/path/to/pid_file_name :进程文件名;
--plugins=proxy -b 192.168.1.219:3306 -r 192.168.1.177:3306
8.12.1.6 mysql-proxy日志
监控启动日志
tail -f /var/log/mysql-proxy.log
8.12.1.7 mysql-proxy测试连接
mysql -uadmin -padmin -h 10.204.3.8 -P4040 # 管理模式,用于查询状态
mysql -uadmin -padmin -h 10.204.3.8 -P4041 # 监听模式,用于连接操作
默认只能以下操作
mysql> show databases;
ERROR 1105 (07000): use ‘SELECT * FROM help‘ to see the supported commands
mysql> select * from backends;
+-------------+------------------+---------+------+------+-------------------+
| backend_ndx | address | state | type | uuid | connected_clients |
+-------------+------------------+---------+------+------+-------------------+
| 1 | 10.204.3.9:3306 | unknown | rw | NULL | 0 |
| 2 | 10.204.3.10:3306 | unknown | ro | NULL | 0 |
+-------------+------------------+---------+------+------+-------------------+
2 rows in set (0.00 sec)
注:因为rw-splitting.lua脚本默认有4个链接才启用分离;所以多开启几个终端;多测试几下;你也可以去修改里面的相关值;
8.12.1.8 mysql-proxy压力测试
用sysbench压力测试
[@s1.yejr.com ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=1000000 \
--mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-db=test prepare
[@s1.yejr.com ~]# sysbench --test=oltp --mysql-table-engine=innodb --oltp-table-size=1000000 \
--mysql-socket=/tmp/mysql.sock --mysql-user=root --mysql-db=test run
8.12.1.9 Mysql-proxy报错
gcc -O2 -fPIC -Wall -DLUA_USE_LINUX -c -o lua.o lua.c
在包含自 lua.h:16 的文件中,
从 lua.c:15:
luaconf.h:275:31: 错误:readline/readline.h:没有那个文件或目录
luaconf.h:276:30: 错误:readline/history.h:没有那个文件或目录
解决方法:yum install -y readline-devel
checking for LUA... no
checking for LUA... configure: error: Package requirements (lua5.1 >= 5.1) were not met:
No package ‘lua5.1‘ found
解决方法:vi /etc/profile
export LUA_CFLAGS="-I /usr/local/lua-5.1.4/include/"
export LUA_LIBS="-L /usr/local/lua-5.1.4/lib/ -llua -ldl"
checking for event_init in -levent... no
configure: error: libevent is required
解决方法:yum install libevent-devel
原文地址:http://blog.51cto.com/zhongliang/2152918