当前位置:首页 » Mysql技术教程

【Haproxy反向代理和负载均衡之Mysql】

2017-06-15 08:22 本站整理 浏览(36)

一、配置文件

[root@master ~]# cat  /opt/haproxy-1.5.0/mysql_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

    # turn on stats unix socket

    stats socket /var/lib/haproxy/stats

 

#---------------------------------------------------------------------

# common defaults that all the 'listen' and 'backend' sections will

# use if not designated in their block

#---------------------------------------------------------------------

defaults

        log     global

        log 127.0.0.1 local3

        mode    tcp

        #option httplog

        option  dontlognull

        retries 10

        option redispatch

        maxconn         2000

        timeout http-request    10s                  

        timeout queue           1m                   

        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.8.1:3306

        server mysql2 192.168.8.1: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 hide-version

        stats admin if TRUE

[root@master ~]# ping 192.168.8.1

PING 192.168.8.1 (192.168.8.1) 56(84) bytes of data.

64 bytes from 192.168.8.1: icmp_seq=1 ttl=64 time=0.088 ms

64 bytes from 192.168.8.1: icmp_seq=2 ttl=64 time=1.10 ms

 

--- 192.168.8.1 ping statistics ---

2 packets transmitted, 2 received, 0% packet loss, time 1000ms

rtt min/avg/max/mdev = 0.088/0.595/1.102/0.507 ms

[root@master ~]# telnet 192.168.8.1

Trying 192.168.8.1...

telnet: connect to address 192.168.8.1: Connection refused

telnet: Unable to connect to remote host: Connection refused

[root@master ~]# telnet 192.168.8.1 3306

Trying 192.168.8.1...

Connected to 192.168.8.1 (192.168.8.1).

Escape character is '^]'.

N

5.5.47-log}^?_Bkt%Vbkqtvr[NT;hmysql_native_passwordConnection closed by foreign host.

[root@master ~]# 

 

二、启动

[root@master haproxy-1.5.0]# haproxy -f mysql_haproxy.cfg  -d -V

三、验证