nginx 實現mysql的負載平衡

來源:互聯網
上載者:User

nginx屬於七層架構,支援的是http協議,本身對tcp協議沒有支援。所以不能代理mysql等實現負載平衡。但是lvs這個東西不熟悉,主要是公司的的負載平衡都是nginx所以決定研究一下nginx的這個功能實現,下面簡單介紹一下實現方法:

1.下載module模組

: https://nodeload.github.com/yaoweibin/nginx_tcp_proxy_module/zipball/master

        $ wget 'http://nginx.org/download/nginx-1.2.1.tar.gz'

        $ tar -xzvf nginx-1.2.1.tar.gz
        $ cd nginx-1.2.1/
        $ patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch  /path是指nginx_tcp_proxy_module路徑
        $  ./configure --add-module=/usr/local/ngx_cache_purge-1.4  --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=/path/to/nginx_tcp_proxy_module   //編譯
        $ make
        $ make install

2、修改nginx設定檔:

  http {
        server {
            listen 80;
            location /status {
                check_status;
            }
        }
    }

tcp {
      upstream mysql{
      server 10.10.10.17:3306 weight=1;
      server 10.10.10.18:3306 weight=1;
      #check interval=3000 rise=2 fall=5 timeout=1000;
      }
      server {
      listen 3306;
      proxy_pass mysql;
       }
}

這樣就完成了配置,啟動nginx進行測試,下面是測試中列印的log:

2012/09/07 18:22:32 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:31 10.10.10.18:3306 1446 4383
2012/09/07 18:22:32 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:32 10.10.10.17:3306 1446 4383
2012/09/07 18:22:33 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:32 10.10.10.18:3306 1447 4383
2012/09/07 18:22:33 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:33 10.10.10.17:3306 1445 4383
2012/09/07 18:22:34 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:33 10.10.10.18:3306 1445 4383
2012/09/07 18:22:34 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:34 10.10.10.17:3306 1446 4383
2012/09/07 18:22:35 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:34 10.10.10.18:3306 1445 4383
2012/09/07 18:22:35 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:35 10.10.10.17:3306 1445 4383
2012/09/07 18:22:36 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:35 10.10.10.18:3306 1445 4383
2012/09/07 18:22:37 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:36 10.10.10.17:3306 1446 4383
2012/09/07 18:22:37 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:37 10.10.10.18:3306 1446 4383
2012/09/07 18:22:37 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:37 10.10.10.17:3306 1445 4383
2012/09/07 18:22:38 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:37 10.10.10.18:3306 1444 4383
2012/09/07 18:22:39 [3921] 10.10.10.107 0.0.0.0:3306 2012/09/07 18:22:38 10.10.10.17:3306 1445 4383

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.