haproxy+mysql实现代理mysql
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了haproxy+mysql实现代理mysql相关的知识,希望对你有一定的参考价值。
我们通常会碰到这样的业务场景:
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
以上是关于haproxy+mysql实现代理mysql的主要内容,如果未能解决你的问题,请参考以下文章