Install HAProxy


How to install and configure HAproxy 1.5 on Ubuntu 12.04
HAProxy - The Reliable, High Performance TCP/HTTP Load Balancer

haproxy to -mysql

HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP andHTTP-based applications. It is particularly suited for very high traffic web sites and powers quite a number of the world's most visited ones. Over the years it has become the de-facto standard opensource load balancer, is now shipped with most mainstream Linux distributions, and is often deployed by default in cloud platforms. Since it does not advertise itself, we only know it's used when the admins report it :-)

Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the net, such as below


IP -
Percona cluster Mysql server:
IP -
IP -
#for future
web servers:
IP -
IP -

server haproxy:

We need to add repo to install haproxy 1.5

$apt-get install python-software-properties
$add-apt-repository ppa:vbernat/haproxy-1.5
$sudo apt-get update
$sudo apt-get install haproxy

By default haproxy cannot start, we need edit file:
/etc/default/proxy to set 1:
$sudo service haproxy start
$haproxy -v
should :
HA-Proxy version 1.5.6 2014/10/18
Copyright 2000-2014 Willy Tarreau <[email protected]>

edit new file /etc/haproxy/haproxy.cfg

    log local0 notice
    maxconn 2000
    user haproxy
    group haproxy

    log     global
    mode    http
#    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    timeout connect  70000
    timeout client  50000
    timeout server  50000

listen appname
    mode http
    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth user:123
    balance static-rr
    option httpclose
    option forwardfor
    server lamp1 check 
    server lamp2 check 

listen mysql-cluster
    bind   #you must change this ip to   after migrate from master mysql srv
    mode tcp
    balance roundrobin
    maxconn 5200
    option mysql-check user haproxy_check
    server mysql1 check port 3306
    server mysql2 check port 3306 backup

section global:
log local0 notice
we can use rsyslog for logging, by default rsyslog runs but is not listennig, let's fixed it.

Edit or create file if it doesn't exist /etc/rsyslog.d/haproxy.conf:

# Create an additional socket in haproxy's chroot in order to allow logging via
# /dev/log to chroot'ed HAProxy processes
$AddUnixListenSocket /var/lib/haproxy/dev/log

# Send HAProxy messages to a dedicated logfile
if $programname startswith 'haproxy' then /var/log/haproxy.log

in file:
we should uncomment the following lines:

$ModLoad imudp
$UDPServerRun 514

#add line

then restart rsyslog:
$sudo service rsyslog restart


$sudo netstat -anp|grep rsyslog
udp   0   0*     1563/rsyslogd

listen appname
for load balancing web servers
we have two servers:

also we have statistic:

    stats enable
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth user:123

in browser url:
user: user
password: 123 #use complex password!!!

Load balancing for Mysql servers


listen mysql-cluster
    server mysql1 check port 3306
    server mysql2 check port 3306 backup

all connections will be in mysql1
if mysql1 doens't work for any reason haproxy will switch all traffic to mysql2

option mysql-check user haproxy_check
needs to check active mysql server
we need to create user haproxy_check in mysql server which will allow permissions from the haproxy host only

mysql -u root -p -e "INSERT INTO mysql.user (Host,User) values ('','haproxy_check'); FLUSH PRIVILEGES;"

Should be work!

Current rating: 1.5


There are currently no comments

New Comment


required (not published)