我们通常会碰到这样的业务场景:
b 主机和 c 数据库在同一个内网, a 主机不能直接访问 c 数据库, 我们可以通过在 b 主机上搭建代理让 a 访问 c 数据库, 我们使用 haproxy 来干这个事情
centos7 环境
mysql 自行安装
haproxy 安装配置
yum install -y haproxy
配置 haproxy:
- vim /etc/haproxy/haproxy.cfg #内容如下
- global
- # to have these messages end up in /var/log/haproxy.log you will
- # need to:
- #
- # 1) configure syslog to accept network log events. This is done
- # by adding the '-r' option to the SYSLOGD_OPTIONS in
- # /etc/sysconfig/syslog
- #
- # 2) configure local2 events to go to the /var/log/haproxy.log
- # file. A line like the following can be added to
- # /etc/sysconfig/syslog
- #
- # local2.* /var/log/haproxy.log
- #
- log 127.0.0.1 local2
- chroot /var/lib/haproxy
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
- # turn on stats unix socket
- stats socket /var/lib/haproxy/stats
- defaults
- mode http
- log global
- option httplog
- option dontlognull
- option http-server-close
- option forwardfor except 127.0.0.0/8
- option redispatch
- retries 3
- timeout http-request 10s
- timeout queue 1m
- timeout connect 10s
timeout client 1m
- timeout server 1m
- timeout http-keep-alive 10s
- timeout check 10s
- maxconn 3000
- listen mysql
- bind 0.0.0.0:7306
- mode tcp
- balance roundrobin
- server mysql1 192.168.10.130:3306
- #server mysql2 192.168.10.132: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
CentOS 7 上 yum 安装的 Haproxy, 默认没有记录日志. 需要做一下配置才能记录日志
1. 创建日志文件 / var/log/haproxy/haproxy.log
- cd /var/log
- mkdir haproxy
- cd haproxy
- touch haproxy.log
- chmod a+w haproxy.log
2. 开启 rsyslog 的 haproxy 日志记录功能
编辑 vim /etc/rsyslog.conf 文件, 将
- $ModLoad imudp
- $UDPServerRun 514
两行前的 #去掉.
在
local7.* /var/log/boot.log
之后添加
- # Save haproxy log
- local2.* /var/log/haproxy/haproxy.log
修改 vim /etc/sysconfig/rsyslog 文件, 将
SYSLOGD_OPTIONS=""
改为
SYSLOGD_OPTIONS="-r -m 2 -c 2"
重启 rsyslog 和 haproxy 服务, haproxy 就能记录日志了.
- systemctl restart rsyslog
- systemctl restart haproxy
测试
mysql -h192.168.10.130 -uhyh -p -P7306
来源: http://www.bubuko.com/infodetail-2636154.html