一、安装haproxy
1.找到haproxy的包
http://pkgs.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.7.9.tar.gz
也可以直接用yum安装
yum install -y haproxy
2.解压
tar zxf haproxy-1.7.9.tar.gz
3.编译安装
cd haproxy-1.7.9
make TARGET=linux26 ARCH=x86_64 (查看自己系统是什么)
make install SBINDIR=/usr/sbin/ MANDIR=/usr/share/man/ DOCDIR=/usr/share/doc/
4.编辑启动脚本
vi /etc/init.d/haproxy
#!/bin/sh # # haproxy # # chkconfig: - 85 15 # description: HAProxy is a free, very fast and reliable solution \ # offering high availability, load balancing, and \ # proxying for TCP and HTTP-based applications # processname: haproxy # config: /etc/haproxy/haproxy.cfg # pidfile: /var/run/haproxy.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 exec="/usr/sbin/haproxy" prog=$(basename $exec) [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog cfgfile=/etc/haproxy/haproxy.cfg pidfile=/var/run/haproxy.pid lockfile=/var/lock/subsys/haproxy check() { $exec -c -V -f $cfgfile $OPTIONS } start() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Starting $prog: " # start it up here, usually something like "daemon $exec" daemon $exec -D -f $cfgfile -p $pidfile $OPTIONS retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " # stop it here, often "killproc $prog" killproc $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi stop start } reload() { $exec -c -q -f $cfgfile $OPTIONS if [ $? -ne 0 ]; then echo "Errors in configuration file, check with $prog check." return 1 fi echo -n $"Reloading $prog: " $exec -D -f $cfgfile -p $pidfile $OPTIONS -sf $(cat $pidfile) retval=$? echo return $retval } force_reload() { restart } fdr_status() { status $prog } case "$1" in start|stop|restart|reload) $1 ;; force-reload) force_reload ;; check) check ;; status) fdr_status ;; condrestart|try-restart) [ ! -f $lockfile ] || restart ;; *) echo $"Usage: $0 {start|stop|status|restart|try-restart|reload|force-reload}" exit 2 esac
5.加入自启动
chkconfig --add haproxy
chkconfig haproxy on
chmod +x /etc/init.d/haproxy
二、配置haproxy
1.新建目录和用户
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd -r haproxy
2.编辑配置文件
vi /etc/haproxy/haproxy.cfg
global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon stats socket /var/lib/haproxy/stats defaults log global log 127.0.0.1 local3 mode http option tcplog option dontlognull retries 10 option redispatch maxconn 2000 timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s listen mysql bind 0.0.0.0:7306 mode tcp balance roundrobin server mysql1 192.168.1.78:3306 server mysql2 192.168.1.77:3306 listen stats bind 0.0.0.0:1080 mode http option httplog maxconn 10 stats refresh 30s stats uri /stats stats realm XingCloud\ Haproxy stats auth admin:admin #用这个账号登录,可以自己设置 stats auth Frank:Frank stats hide-version stats admin if TRUE
3.配置日志
(1).vim /etc/rsyslog.conf
# Provides UDP syslog reception #去掉下面两行注释,开启UDP监听
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log #添加日志
(2).vi /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS=""
改为 SYSLOGD_OPTIONS="-r -m 2 -c 2"
(3).创建日志文件
touch /var/log/haproxy.log
4.启动日志和haproxy
service haproxy start
service rsyslog restart
netstat -plantu | grep 7306 --查看端口7306
三、测试haproxy是否成功
1.在两台数据库添加权限
GRANT ALL ON *.* TO 'haproxy'@'192.168.1.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
2.在haproxy这台服务器远程登录到mysql服务器,测试
yum install -y mysql (若没有mysql客户端,安装)
mysql -uhaproxy -p123456 -h 192.168.1.78
mysql -uhaproxy -p123456 -h 192.168.1.77
3.测试haproxy
在其他的服务器上输入(haproxy的服务器地址192.168.88):
mysql -uhaproxy -p123456 -h 192.168.88 -P 7306
是否能连接到数据库
7306是在配置文件中设置的端口,通过haproxy的7306端口访问mysql的3306端口
4.页面访问
浏览器输入http://192.168.1.88:1080/stats
出现下图所示:
证明成功。