zoukankan      html  css  js  c++  java
  • Haproxy+MYSQL 负载均衡 原创

    [root@monitor app1]# yum install haproxy
    Loaded plugins: security
    base                                                     | 3.7 kB     00:00     
    epel                                                     | 4.3 kB     00:00     
    epel/primary_db                                          | 5.9 MB     00:05     
    extras                                                   | 3.4 kB     00:00     
    updates                                                  | 3.4 kB     00:00     
    updates/primary_db                                       | 5.2 MB     00:04     
    Setting up Install Process
    Resolving Dependencies
    --> Running transaction check
    ---> Package haproxy.x86_64 0:1.5.4-2.el6_7.2 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package         Arch           Version                   Repository       Size
    ================================================================================
    Installing:
     haproxy         x86_64         1.5.4-2.el6_7.2           updates         792 k
    
    Transaction Summary
    ================================================================================
    Install       1 Package(s)
    
    Total download size: 792 k
    Installed size: 2.4 M
    Is this ok [y/N]: y
    Downloading Packages:
    haproxy-1.5.4-2.el6_7.2.x86_64.rpm                       | 792 kB     00:00     
    Running rpm_check_debug
    Running Transaction Test
    Transaction Test Succeeded
    Running Transaction
    Warning: RPMDB altered outside of yum.
      Installing : haproxy-1.5.4-2.el6_7.2.x86_64                               1/1 
      Verifying  : haproxy-1.5.4-2.el6_7.2.x86_64                               1/1 
    
    Installed:
      haproxy.x86_64 0:1.5.4-2.el6_7.2                                              
    
    Complete!

    装了那些东西:

    [root@monitor app1]# rpm -ql haproxy.x86_64 0:1.5.4-2.el6_7.2
    /etc/haproxy
    /etc/haproxy/haproxy.cfg
    /etc/logrotate.d/haproxy
    /etc/rc.d/init.d/haproxy
    /etc/sysconfig/haproxy
    /usr/bin/halog
    /usr/bin/iprange
    /usr/sbin/haproxy
    /usr/share/doc/haproxy-1.5.4
    /usr/share/doc/haproxy-1.5.4/CHANGELOG
    /usr/share/doc/haproxy-1.5.4/LICENSE
    /usr/share/doc/haproxy-1.5.4/README
    /usr/share/doc/haproxy-1.5.4/acl-content-sw.cfg
    /usr/share/doc/haproxy-1.5.4/acl.fig
    /usr/share/doc/haproxy-1.5.4/architecture.txt
    /usr/share/doc/haproxy-1.5.4/close-options.txt
    /usr/share/doc/haproxy-1.5.4/coding-style.txt
    /usr/share/doc/haproxy-1.5.4/configuration.txt
    /usr/share/doc/haproxy-1.5.4/content-sw-sample.cfg
    /usr/share/doc/haproxy-1.5.4/cookie-options.txt
    /usr/share/doc/haproxy-1.5.4/cttproxy-src.cfg
    /usr/share/doc/haproxy-1.5.4/design-thoughts
    /usr/share/doc/haproxy-1.5.4/design-thoughts/backends-v0.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/backends.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/be-fe-changes.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/binding-possibilities.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/buffer-redesign.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/buffers.fig
    /usr/share/doc/haproxy-1.5.4/design-thoughts/config-language.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/connection-reuse.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/cttproxy-changes.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/entities-v2.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/how-it-works.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/http_load_time.url
    /usr/share/doc/haproxy-1.5.4/design-thoughts/rate-shaping.txt
    /usr/share/doc/haproxy-1.5.4/design-thoughts/sess_par_sec.txt
    /usr/share/doc/haproxy-1.5.4/gpl.txt
    /usr/share/doc/haproxy-1.5.4/haproxy-en.txt
    /usr/share/doc/haproxy-1.5.4/haproxy-fr.txt
    /usr/share/doc/haproxy-1.5.4/haproxy.1
    /usr/share/doc/haproxy-1.5.4/haproxy.cfg
    /usr/share/doc/haproxy-1.5.4/internals
    /usr/share/doc/haproxy-1.5.4/internals/acl.txt
    /usr/share/doc/haproxy-1.5.4/internals/body-parsing.txt
    /usr/share/doc/haproxy-1.5.4/internals/buffer-operations.txt
    /usr/share/doc/haproxy-1.5.4/internals/buffer-ops.fig
    /usr/share/doc/haproxy-1.5.4/internals/connect-status.txt
    /usr/share/doc/haproxy-1.5.4/internals/connection-header.txt
    /usr/share/doc/haproxy-1.5.4/internals/connection-scale.txt
    /usr/share/doc/haproxy-1.5.4/internals/entities.fig
    /usr/share/doc/haproxy-1.5.4/internals/entities.pdf
    /usr/share/doc/haproxy-1.5.4/internals/entities.svg
    /usr/share/doc/haproxy-1.5.4/internals/entities.txt
    /usr/share/doc/haproxy-1.5.4/internals/hashing.txt
    /usr/share/doc/haproxy-1.5.4/internals/header-parser-speed.txt
    /usr/share/doc/haproxy-1.5.4/internals/header-tree.txt
    /usr/share/doc/haproxy-1.5.4/internals/http-cookies.txt
    /usr/share/doc/haproxy-1.5.4/internals/http-docs.txt
    /usr/share/doc/haproxy-1.5.4/internals/http-parsing.txt
    /usr/share/doc/haproxy-1.5.4/internals/naming.txt
    /usr/share/doc/haproxy-1.5.4/internals/pattern.dia
    /usr/share/doc/haproxy-1.5.4/internals/pattern.pdf
    /usr/share/doc/haproxy-1.5.4/internals/polling-states.fig
    /usr/share/doc/haproxy-1.5.4/internals/repartition-be-fe-fi.txt
    /usr/share/doc/haproxy-1.5.4/internals/sequence.fig
    /usr/share/doc/haproxy-1.5.4/internals/stats-v2.txt
    /usr/share/doc/haproxy-1.5.4/internals/stream-sock-states.fig
    /usr/share/doc/haproxy-1.5.4/internals/todo.cttproxy
    /usr/share/doc/haproxy-1.5.4/lgpl.txt
    /usr/share/doc/haproxy-1.5.4/proxy-protocol.txt
    /usr/share/doc/haproxy-1.5.4/queuing.fig
    /usr/share/doc/haproxy-1.5.4/tarpit.cfg
    /usr/share/doc/haproxy-1.5.4/url-switching.cfg
    /usr/share/haproxy
    /usr/share/haproxy/400.http
    /usr/share/haproxy/403.http
    /usr/share/haproxy/408.http
    /usr/share/haproxy/500.http
    /usr/share/haproxy/502.http
    /usr/share/haproxy/503.http
    /usr/share/haproxy/504.http
    /usr/share/haproxy/README
    /usr/share/man/man1/halog.1.gz
    /usr/share/man/man1/haproxy.1.gz
    /var/lib/haproxy
    package 0:1.5.4-2.el6_7.2 is not installed
    开机自动启动:

    chkconfig haproxy on

    服务启动:
    service haproxy on


    [root@monitor etc]# netstat -tunlp | grep haproxy
    tcp 0 0 0.0.0.0:8888 0.0.0.0:* LISTEN 1296/haproxy
    tcp 0 0 10.169.216.172:3306 0.0.0.0:* LISTEN 1296/haproxy
    udp 0 0 0.0.0.0:60642 0.0.0.0:* 1296/haproxy



    日志记录:

    [root@monitor etc]# cat /etc/rsyslog.d/49-haproxy.conf



    # Create an additional socket in haproxy's chroot in order to allow logging via # /dev/log to chroot'ed HAProxy processes $AddUnixListenSocket /var/lib/haproxy/dev/log $ModLoad imudp $UDPServerRun 514 local3.* /var/log/haproxy.log # Send HAProxy messages to a dedicated logfile if $programname startswith 'haproxy' then /var/log/haproxy.log &~
    [root@monitor etc]# cat /etc/haproxy/haproxy.cfg
    
    # 全局配置参数
    global
        log 127.0.0.1 local0 notice
        user haproxy
        group haproxy
    
    # 一些默认参数
    defaults
        log global
        retries 3
        option  dontlognull
        option redispatch
        maxconn 2000
        timeout connect 3000
        timeout server 5000
        timeout client 5000
    
    # 这个是我们定义的负载均衡的配置
    listen mysql-lb1
        # 绑定的IP和端口 haproxy服务器一个网卡IP
        bind 10.169.216.172:3306      
        # 模式是TCP
        mode tcp
        # 通过mysql连接去检测mysql是否可以访问 创建用户haproxy_check 密码为空,权限usage
        option mysql-check user haproxy_check   
        # 负载均衡算法是 轮询
        balance roundrobin
        # 下面两个就是后端被访问的server
        server mysql_1 10.24.220.70:3306 weight 1 check
        server mysql_2 10.169.214.33:3306 weight 1 check
    
    # 自带的监控服务器的配置
    # 监控服务的端口是 8888
    listen stats *:8888
           # 监控模式是http
           mode http
           option httpclose
           balance roundrobin
           stats uri /
           stats realm Haproxy Statistics
           # 监控的用户名和密码
           stats auth myadmin:myadmin
    后台MYSQL每台执行:

    # 通过mysql连接去检测mysql是否可以访问 创建用户haproxy_check 密码为空,权限usage

    drop user haproxy_check@'10.169.216.172'; create user haproxy_check@'10.169.216.172'; grant usage on *.* to haproxy_check@'10.169.216.172'; set password for haproxy_check@'10.169.216.172' ='';
    haproxy_check 账号测试:



    root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave2 | +------------+ [root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave1 | +------------+ [root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave2 | +------------+ [root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave1 | +------------+ [root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave2 | +------------+ [root@monitor etc]# mysql -uhaproxy_check -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave1 | +------------+
    ROOT 用户测试

    [root@monitor etc]# mysql -uroot -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave2 | +------------+ [root@monitor etc]# mysql -uroot -p -h 10.169.216.172 -e "select @@hostname;" Enter password: +------------+ | @@hostname | +------------+ | slave1 | +------------+
  • 相关阅读:
    spoj DQUERY
    省选模拟赛 爬山法
    bzoj1874 [BeiJing2009 WinterCamp]取石子游戏
    bzoj1013 [JSOI2008]球形空间产生器sphere
    省选模拟赛 让苍天知道我不认输(40分)
    省选模拟赛 厌世者打击(60分)
    省选模拟赛 至危警告
    bzoj4449 [Neerc2015]Distance on Triangulation
    省选模拟赛 cti
    数组、ArrayList、List、LinkedList的区别
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5498236.html
Copyright © 2011-2022 走看看