haproxy安装配置for mysql负载均衡(ubantu)
1、安装pcre
apt-get update
(apt-get install openssl libssl-dev ==>可能需要安装的依赖包)
apt-get install libpcre3 libpcre3-dev
#yum -y groupinstall "Development Tools" // 安装gcc等linux开发工具
2、解压
# tar zcvf haproxy-1.5.8.tar.gz
# cd haproxy-1.5.8
# make TARGET=linux26 PREFIX=/usr/local/haproxy #将haproxy安装到/usr/local/haproxy
# make install PREFIX=/usr/local/haproxy
cd /usr/local/haproxy
vim haproxy.conf
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
chroot /usr/local/haproxy
pidfile /usr/local/haproxy/haproxy.pid
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
retries 3
option redispatch
maxconn 2000
timeout connect 5000
timeout client 50000
timeout server 50000 #默认是毫秒,也可是直接设置为10s,10m
listen mysql_proxy 123.57.173.111:3307
mode tcp
balance roundrobin
option tcpka
option httpchk
option mysql-check user user:password
server mysqldb1 123.57.173.111:3303 weight 1
server mysqldb2 123.56.149.60:3307 weight 1
vim /etc/init.d/haproxy 加入下面的脚本,并赋予文件执行权限 +x
#! /bin/bash
set -e
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=$PROGDIR/$PROGNAME.conf
PIDFILE=$PROGDIR/$PROGNAME.pid
DESC="HAProxy daemon"
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n "Starting $DESC: $PROGNAME"
$DAEMON -f $CONFIG
echo "."
}
stop()
{
echo -n "Stopping $DESC: $PROGNAME"
haproxy_pid=cat $PIDFILE
kill $haproxy_pid
echo "."
}
restart()
{
echo -n "Restarting $DESC: $PROGNAME"
$DAEMON -f $CONFIG -p $PIDFILE -sf $(cat $PIDFILE)
echo "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
exit 1
;;
esac
exit 0