mysql-proxy具有代理的功能,將客戶端的mysql請求輪詢至后台的mysql-server。
環境介紹:
一台mysql-proxy(178)
兩台mysql-server(180,122)
一台mysql-client(254)
在mysql-proxy上安裝如下包:
1.安裝 pkg-config:(在RHEL6上默認已經安裝,不需安裝)
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
(如果是系統自帶的pkg-config 則export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/lib/pkgconfig)
2.安裝 libevent:
tar zxvf libevent-1.4.10-stable.tar.gz
cd libevent-1.4.10-stable
./configure
make
make install
3.安裝 glib:
tar zxvf glib-2.20.0.tar.gz
cd glib-2.20.0
./configure
make
make install
4.安裝 lua:
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
如果你的服務器是 64 位的,這時要調整一下 Makefile:vi src/Makefile,在 CFLAGS 里
加上-fPIC,否則會出錯:
接下來不用執行常見的 configure,直接 make:
make linux
make install
5.安裝 mysql-proxy:
安裝 pkg-config 配置文件,以便編譯 mysql-proxy 時能找到 lua:
cp etc/lua.pc /usr/local/lib/pkgconfig/lua.pc
如果沒有執行此步驟的話,在編譯安裝 mysql-proxy 的時候,會得到類似下面的錯誤
信息:
Package lua5.1 was not found in the pkg-config search path.
Perhaps you should add the directory containing `lua5.1.pc'
to the PKG_CONFIG_PATH environment variable
No package 'lua5.1' found
tar zxvf mysql-proxy-0.7.0.tar.gz
cd mysql-proxy-0.7.0
./configure
make
make install
按照官方介紹做好啟動腳本/etc/init.d/mysql-proxy
#!/bin/sh
#
# mysql-proxy This script starts and stops the mysql-proxy daemon
#
# chkconfig: - 78 30
# processname: mysql-proxy
# description: mysql-proxy is a proxy daemon to mysql
# Source function library.
. /etc/rc.d/init.d/functions
PROXY_PATH=/opt/mysql-proxy/sbin
prog="mysql-proxy"
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
# Set default mysql-proxy configuration.
PROXY_OPTIONS="--daemon"
PROXY_PID=/var/run/mysql-proxy.pid
# Source mysql-proxy configuration.
if [ -f /etc/sysconfig/mysql-proxy ] ; then
. /etc/sysconfig/mysql-proxy
fi
PATH=$PATH:/usr/bin:/usr/local/bin:$PROXY_PATH
# By default it's all good
RETVAL=0
# See how we were called.
case "$1" in
start)
# Start daemon.
echo -n $"Starting $prog: "
daemon $NICELEVEL $PROXY_PATH/mysql-proxy $PROXY_OPTIONS --pid-file $PROXY_PID
RETVAL=$?
echo
if [ $RETVAL = 0 ]; then
touch /var/lock/subsys/mysql-proxy
fi
;;
stop)
# Stop daemons.
echo -n $"Stopping $prog: "
killproc $prog
RETVAL=$?
echo
if [ $RETVAL = 0 ]; then
rm -f /var/lock/subsys/mysql-proxy
rm -f $PROXY_PID
fi
;;
restart)
$0 stop
sleep 3
$0 start
;;
condrestart)
[ -e /var/lock/subsys/mysql-proxy ] && $0 restart
;;
status)
status mysql-proxy
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart|status|condrestart}"
RETVAL=1
;;
esac
exit $RETVAL
chmod 755 /etc/init.d/mysql-proxy
chmod +x /etc/init.d/mysql-proxy
接下來要修改參數腳本/etc/sysconfig/mysql-proxy,在其中加入如下語句:
PROXY_OPTIONS="--proxy-backend-address=192.168.0.180:3306 --proxy-backend-addresses=192.168.0.122:3306 --daemon"
#service mysql-proxy start 會出現警告信息(ulimit -n 8192)
測試:
在 180上建數據庫 DB1|t1|180
在 122上建數據庫 DB1|t1|122
監聽端口4040
設置grant語句
180>GRANT ALL PRIVILEGES on *.* to root@192.168.0.178 identified by '******'
122>GRANT ALL PRIVILEGES on *.* to root@192.168.0.178 identified by '******'
#iptables -I INPUT 1 -d 192.168.0.122/24 -p tcp --dport 3306 -j accept
#iptables -I INPUT 1 -d 192.168.0.180/24 -p tcp --dport 3306 -j accept
#mysql -uroot -p****** -P 4040 -h 192.168.0.178
測試時終端不要退,退了會看不出結果。(輪詢)
二 讀寫分離
#mysql-proxy --help-all
查看得知 -b 可讀寫 -r 只讀
在mysql-proxy源代碼lib目錄中 有讀寫分離腳本(rw-splitting.lua)
#cp rw-splitting.lua /usr/local/share 復制讀寫分離腳本到隨便一個目錄。
#vi /etc/sysconfig/mysql-proxy
PROXY_OPTIONS="-P 192.168.0.178:3306 -b 192.168.0.253 -r 192.168.0.252 -r 192.168.0.251 -r 192.168.0.250 -s /usr/local/share --daemon"
測試可以實現讀寫分離。
本文出自 “linux集群技術博客” 博客,請務必保留此出處http://keygen.blog.51cto.com/6412723/1125896
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。