[toc]

官方网站

https://launchpad.net/mysql-proxy

下载解压

[shell]wget http://launchpad.net/mysql-proxy/0.8/0.8.0/+download/mysql-proxy-0.8.0.tar.gz
tar xvf mysql-proxy-0.8.0.tar.gz[/shell]

安装编译工具

[shell]apt-get install gcc pkg-config[/shell]
gcc:The GNU C compiler c语言编译器
pkg-config:manage compile and link flags for libraries http://blog.csdn.net/yuntongsf/archive/2009/04/21/4097393.aspx

安装支持库

[shell]apt-get install libevent-dev libglib2.0-dev liblua5.1-0-dev libmysqlclient-dev[/shell]

编译安装

[shell]cd mysql-proxy-0.8.0
./configure
make
make install[/shell]

刷新缓存库路径

[shell]ldconfig -v[/shell]

ldconfig命令的用途,主要是在默认搜寻目录(/lib 和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态链接库(格式如前介绍,lib*.so*), 进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为/etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.
ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令.

配置脚本

启动脚本

/etc/init.d/mysql-proxy
[shell]
#! /bin/sh

### BEGIN INIT INFO
# Provides: mysql-proxy
# Required-Start: $syslog
# Required-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: MySQL Proxy
### END INIT INFO

PATH=/bin:/usr/bin:/sbin:/usr/sbin
LUA_PATH="/usr/share/mysql-proxy/?.lua"

DAEMON="/usr/sbin/mysql-proxy"
PIDFILE="/var/run/mysql-proxy.pid"

test -f $DAEMON || exit 0

test ! -r /etc/default/mysql-proxy || . /etc/default/mysql-proxy

if ! $ENABLED ; then exit 0 ; fi

. /lib/lsb/init-functions

case "$1" in
start)
log_begin_msg "Starting MySQL Proxy daemon…"
start-stop-daemon –start –quiet –pidfile $PIDFILE –make-pidfile –name mysql-proxy –startas $DAEMON -b — $OPTIONS
log_end_msg $?
;;
stop)
log_begin_msg "Stopping MySQL Proxy daemon…"
start-stop-daemon –stop –quiet –retry 3 –exec $DAEMON –pidfile $PIDFILE
log_end_msg $?
;;
status)
log_begin_msg "Checking MySQL Proxy daemon… "
status_of_proc -p $PIDFILE /usr/sbin/mysql-proxy mysql-proxy && exit 0 || exit $?
;;
restart|force-reload)
log_begin_msg "Reloading MySQL Proxy daemon…"
start-stop-daemon –stop –quiet –retry 3 –exec $DAEMON –pidfile $PIDFILE
start-stop-daemon –start –quiet –pidfile $PIDFILE –make-pidfile –name mysql-proxy –startas $DAEMON -b — $OPTIONS
log_end_msg $?
;;
*)
log_success_msg "Usage: $0 {start|stop|status|restart|force-reload}"
exit 1
esac

exit 0[/shell]

启动设置

/etc/default/mysql-proxy

[shell]ENABLED="true"

OPTIONS="

–proxy-backend-addresses=127.0.0.1:3306 \

–proxy-read-only-backend-addresses=61.164.186.241:3306 \

–proxy-read-only-backend-addresses=61.164.186.246:3306 \

–proxy-lua-script=/usr/lib/mysql-proxy/lua/rw-splitting.lua \

–log-level=info \

–log-file=/var/log/mysql/mysql-proxy.log

–keepalive

"[/shell]

执行权限

[shell]chmod a+x /etc/init.d/mysql-proxy[/shell]

开机自启动

[shell]apt-get install rcconf
rcconf[/shell]

运行rcconf会列出所有/etc/init.d/目录下的启动脚本:

  1. 使用[空格]键选择启动项,变成[*]
  2. 按[TAB]键,选中[OK]

测试连接

[shell]mysql -uroot -poP05_1ep -hlocalhost –port=4040[/shell]