haproxy+mysql實現代理mysql

來源:互聯網
上載者:User

標籤:haproxy+mysql實現代理mys

我們通常會碰到這樣的業務情境:
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.