上一篇: 删除表后,自动更新ID
安装pkg-config:
tar zxvf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure
make
make install
确保PKG_CONFIG_PATH环境变量包含了相关的pkg-config配置文件路径:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
安装libevent:
tar zxvf libevent-1.4.10-stable.tar.gz
cd libevent-1.4.10-stable
./configure
make
make install
安装glib:
tar zxvf glib-2.20.0.tar.gz
cd glib-2.20.0
./configure
make
make install
1、 LUA的安装
[root@localhost ~]#tar zxvf lua-5.1.2.tar.gz -C /usr/local
[root@localhost ~]# cd /usr/local/
[root@localhost local]# mv lua-5.1 lua
[root@localhost lua]# cd lua
[root@localhost lua]#make local;make install;
导出环境变量:
[root@localhost lua]#export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
1. 安装
下载已经编译好的安装包,或者预编译安装包均可,在这里,使用预编译版本。
[@s1.yejr.com ~]# tar zxf mysql-proxy-0.6.0-linux-rhas4-x86.tar.gz
[@s1.yejr.com ~]# cd mysql-proxy-0.6.0-linux-rhas4-x86
#可以看到有2个目录
[@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# ls
sbin share
[@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# mv sbin/mysql-proxy /usr/local/sbin/
[@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# ls share
mysql-proxy tutorial-constants.lua tutorial-packets.lua tutorial-rewrite.lua tutorial-warnings.lua
tutorial-basic.lua tutorial-inject.lua tutorial-query-time.lua tutorial-states.lua
#将lua脚本放到/usr/local/share下,以备他用
[@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# mv share/mysql-proxy /usr/local/share/
#删除符号连接等垃圾代码
[@s1.yejr.com mysql-proxy-0.6.0-linux-rhas4-x86]# strip /usr/local/sbin/mysql-proxy
2. 启动
编译一下启动管理脚本:
[@s1.yejr.com ~]# vi /etc/init.d/mysql-proxy
#!/bin/sh
export LUA_PATH=/usr/local/share/mysql-proxy/?.lua
mode=$1
if [ -z "$mode" ] ; then
mode="start"
fi
case $mode in
'start')
mysql-proxy --daemon \
--admin-address=:4401 \
--proxy-address=:3307 \
--proxy-backend-addresses=:3306 \
--proxy-read-only-backend-addresses=192.168.133.232:3306 \
--proxy-read-only-backend-addresses=10.10.74.61:3306 \
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua
;;
'stop')
killall mysql-proxy
;;
'restart')
if $0 stop ; then
$0 start
else
echo "retart failed!!!"
exit 1
fi
;;
esac
exit 0
export LUA_PATH=/usr/local/share/mysql-proxy/?.lua
mode=$1
if [ -z "$mode" ] ; then
mode="start"
fi
case $mode in
'start')
mysql-proxy --daemon \
--admin-address=:4401 \
--proxy-address=:3307 \
--proxy-backend-addresses=:3306 \
--proxy-read-only-backend-addresses=192.168.133.232:3306 \
--proxy-read-only-backend-addresses=10.10.74.61:3306 \
--proxy-lua-script=/usr/local/share/mysql-proxy/rw-splitting.lua
;;
'stop')
killall mysql-proxy
;;
'restart')
if $0 stop ; then
$0 start
else
echo "retart failed!!!"
exit 1
fi
;;
esac
exit 0
现在解释一下启动脚本:
--daemon 采用daemon方式启动
--admin-address=:4401 指定mysql proxy的管理端口,在这里,表示本机的4401端口
--proxy-address=:3307 指定mysql proxy的监听端口,也可以用 127.0.0.1:3307 表示
--proxy-backend-addresses=:3306 指定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脚本,用于读写分离
完整的参数可以运行以下命令查看:
mysql-proxy --help-all
运行以下命令启动/停止/重启mysql proxy:
[@s1.yejr.com ~]# /etc/init.d/mysql-proxy start
[@s1.yejr.com ~]# /etc/init.d/mysql-proxy stop
[@s1.yejr.com ~]# /etc/init.d/mysql-proxy restart
3. 试用
[@s1.yejr.com ~]# mysql -h127.0.0.1 -uroot -P3307
mysql> show processlist;
+-------+------+----------------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-------+------+----------------+------+---------+------+-------+------------------+
| 30052 | root | localhost:9656 | NULL | Query | 0 | NULL | show processlist |
+-------+------+----------------+------+---------+------+-------+------------------+
可以看到,产生了一个新连接。
用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
.........
.........
Threads fairness:
events (avg/stddev): 10000.0000/0.00
execution time (avg/stddev): 23.0387/0.00
4. 其他
mysql proxy还可以实现连接池的功能,这在很多LAMP开发中是软肋,因此,有了mysql proxy,就可以不用再担心连接数超限的问题了。
如果使用rw-splitting.lua脚本的话,最好修改以下2个参数的默认值:
min_idle_connections = 1
max_idle_connections = 3