nginx四層負(fù)載均衡

基于nginx四層負(fù)載均衡(tcp)代理mysql集群

1、安裝nginx所需依賴

[root@localhost ~]# yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

2、下載Nginx源碼包

[root@localhost ~]# wget http://nginx.org/download/nginx-1.9.9.tar.gz

3、解壓并編譯安裝nginx

[root@localhost ~]# tar -xzf nginx-1.9.9.tar.gz 
[root@localhost ~]# mv nginx-1.9.9 nginx
[root@localhost local]# cd nginx
[root@localhost nginx]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module   --with-stream
[root@localhost nginx]# make -j 2 && make install

4、編輯nginx配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在最后一行添加一下內(nèi)容:
stream {
    server {
       listen 3305;
       proxy_pass db;    
    }
    upstream db {
       server 10.3.131.22:3306;
       server 10.3.131.23:3306;
       server 10.3.131.24:3306;
       server 10.3.131.25:3306;
    }
}

5、啟動(dòng)nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx 
[root@localhost ~]# ss -lntp
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:3305                         *:*                   users:(("nginx",pid=29487,fd=7),("nginx",pid=29486,fd=7))
LISTEN     0      128              *:80                           *:*                   users:(("nginx",pid=29487,fd=6),("nginx",pid=29486,fd=6))

6、測(cè)試

[root@localhost ~]# mysql -ullf123 -p'123456' -h localhost -P 3305
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容