Haproxy 为 mysql 做负载均衡

tar zxvf haproxy-1.4.21.tar.gz
cd haproxy-1.4.21
uname -r

vim /etc/haproxy.cfg


global
        #日志
        log 127.0.0.1    local0
        maxconn 4096
        chroot /tmp
        uid  0
        gid  0

        daemon
        nbproc 2
        pidfile logs/haproxy.pid
        #debug   
        #quiet   
defaults
        log     127.0.0.1       local3
        mode    http
        #option  httplog   
        #option  httpclose   
        option  dontlognull
        #option  forwardfor   
        option  redispatch
        retries 2
        maxconn 2000
        contimeout 3000
        clitimeout 30000
        srvtimeout 30000
        balance roundrobin
        bind-process 1
#监控状态页配置
listen stats
        mode http
        bind 0.0.0.0:8080
        stats enable
        stats   uri     /dbstat
        stats   realm Global\ statistics
        #身份验证
        stats auth admin:hello123


listen  mysql_proxy
        #代理端口
        bind 0.0.0.0:33060
        #模式 TCP
        mode tcp
        #调度算法
        balance roundrobin
        #mysql健康检查
        option mysql-check
        #健康检查加上check
        server  s1 192.168.1.42:3306 weight 3 check port 3306 inter 1s rise 2 fall 2
        server  s3 192.168.1.43:3306 weight 3 check port 3306 inter 1s rise 2 fall 2
        
#启动服务
/usr/local/haproxy/sbin/haproxy -f /etc/haproxy.cfg
#杀掉服务
pkill haproxy

#启动脚本
vim /etc/init.d/haproxy

========================================
#! /bin/sh
set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
PROGDIR=/usr/local/haproxy
PROGNAME=haproxy
DAEMON=$PROGDIR/sbin/$PROGNAME
CONFIG=/etc/haproxy.cfg
PIDFILE=$PROGDIR/logs/$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
=================================================
chmod +x /etc/init.d/haproxy

#启动服务
/etc/init.d/haproxy start

 

参考:

http://www.2cto.com/database/201412/357835.html

http://blog.sina.com.cn/s/blog_5f66526e0100om1x.html

posted @ 2015-07-22 17:58  艾阳君  阅读(278)  评论(0编辑  收藏  举报