1. 安裝 [localhost ~]# wget http://mysql.stu.edu.tw/Downloads/MySQL-Proxy/MySQL-proxy-0.7.2-Linux-rhel4-x86-64bit.tar.gz
[localhost ~]# tar zxf MySQL-proxy-0.7.2-Linux-rhel4-x86-64bit.tar.gz
[localhost ~]# cd MySQL-proxy-0.7.2-Linux-rhel4-x86-64bit
#可以看到有2個目錄
[localhost MySQL-proxy-0.7.2-Linux-rhel4-x86-64bit]# ls
sbin share
[localhost mysql-proxy-0.7.2-Linux-rhel4-x86-64bit]# mv sbin/MySQL-proxy /usr/local/sbin/
[localhost MySQL-proxy-0.7.2-Linux-rhel4-x86-64bit]# 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下,以備他用
[localhost mysql-proxy-0.7.2-Linux-rhel4-x86-64bit]# mv share/MySQL-proxy /usr/local/share/
#刪除符號連接等垃圾代碼
[localhost mysql-proxy-0.7.2-Linux-rhel4-x86-64bit]# strip /usr/local/sbin/MySQL-proxy
2. 啟動
編譯一下啟動管理腳本:
[localhost ~]# 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=10.0.0.60:3306 \
--proxy-read-only-backend-addresses=10.0.0.61:3306 \
--proxy-read-only-backend-addresses=10.0.0.62: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=10.0.0.60:3306 指定MySQL主機的端口
--proxy-read-only-backend-addresses=10.0.0.61:3306 指定只讀的MySQL主機端口
--proxy-read-only-backend-addresses=10.0.0.62:3306 指定另一個只讀的MySQL主機端口
--proxy-lua-script=/usr/local/share/MySQL-proxy/rw-splitting.lua 指定lua腳本,在這裡,使用的是rw-splitting腳本,用於讀寫分離
rw-splitting.lua腳本下載(MySQL Proxy 0.7.0+)
http://bazaar.launchpad.Net/~dIEgo-fmpwizard/MySQL-proxy/bug-43424/download/head%3A/rwsplitting.lua-20090112150705-l9v35osiopsn0nz0-10/rw-splitting.lua
完整的參數可以運行以下命令查看:
MySQL-proxy --help-all
運行以下命令啟動/停止/重啟MySQL proxy:
[localhost ~]# /etc/init.d/MySQL-proxy start
[localhost ~]# /etc/init.d/MySQL-proxy stop
[localhost ~]# /etc/init.d/MySQL-proxy restart
3. 試用
[localhost ~]# 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測試一下,看會不會掛掉:
[localhost ~]# sysbench --test=oltp --MySQL-table-engine=innodb --oltp-table-size=1000000 \
--mysql-socket=/tmp/mysql.sock --mysql-user=root --MySQL-db=test prepare
[localhost ~]# 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