欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過nginx做mysql的負(fù)載均衡實(shí)現(xiàn)過程

 更新時(shí)間:2025年07月19日 15:42:02   作者:老驥又出發(fā)  
Nginx stream模塊用于DNS和主從MySQL的TCP/UDP代理與負(fù)載均衡,安裝需編譯啟用或通過yum安裝,配置stream塊及upstream實(shí)現(xiàn)轉(zhuǎn)發(fā)和輪詢,測(cè)試后驗(yàn)證端口監(jiān)聽狀態(tài)

使用場(chǎng)景

一般用于dns或者已經(jīng)做好主從同步的mysql服務(wù)器的轉(zhuǎn)發(fā),做負(fù)載均衡的工作。

安裝stream模塊

stream模塊一般用于tcp/UDP數(shù)據(jù)流的代理和負(fù)載均衡,可以通過stream模塊代理轉(zhuǎn)發(fā)TCP消息。 ngx_stream_core_module模塊由1.9.0版提供。 默認(rèn)情況下,沒有構(gòu)建此模塊。

第一種安裝方法: 

必須使用-with stream配置參數(shù)啟用。這個(gè)對(duì)于初學(xué)者來講稍微有些麻煩

第二種安裝方法:

首先使用yum -y install epel-release nginx 安裝 nginx模塊

然后查找nginx的模塊,如下圖

[root@hy ~]# yum list |grep nginx
nginx.x86_64                             1:1.20.1-10.el7               @epel
nginx-filesystem.noarch                  1:1.20.1-10.el7               @epel
nginx-mod-stream.x86_64                  1:1.20.1-10.el7               @epel
collectd-nginx.x86_64                    5.8.1-1.el7                   epel
munin-nginx.noarch                       2.0.69-5.el7                  epel
nginx-all-modules.noarch                 1:1.20.1-10.el7               epel
nginx-mod-devel.x86_64                   1:1.20.1-10.el7               epel
nginx-mod-http-image-filter.x86_64       1:1.20.1-10.el7               epel
nginx-mod-http-perl.x86_64               1:1.20.1-10.el7               epel
nginx-mod-http-xslt-filter.x86_64        1:1.20.1-10.el7               epel
nginx-mod-mail.x86_64                    1:1.20.1-10.el7               epel
nginx1w.x86_64                           1.12.1-1.w7                   webtatic
nginx1w-module-headers-more.x86_64       1.12.1-1.w7                   webtatic
nginx1w-module-http-geoip.x86_64         1.12.1-1.w7                   webtatic
nginx1w-module-http-image-filter.x86_64  1.12.1-1.w7                   webtatic
nginx1w-module-http-perl.x86_64          1.12.1-1.w7                   webtatic
nginx1w-module-http-xslt.x86_64          1.12.1-1.w7                   webtatic
nginx1w-module-mail.x86_64               1.12.1-1.w7                   webtatic
nginx1w-module-pagespeed.x86_64          1.12.1-1.w7                   webtatic
nginx1w-module-stream.x86_64             1.12.1-1.w7                   webtatic
pagure-web-nginx.noarch                  5.13.3-2.el7                  epel
pcp-pmda-nginx.x86_64                    4.3.2-13.el7_9                updates
python2-certbot-nginx.noarch             1.11.0-1.el7                  epel
sympa-nginx.x86_64                       6.2.68-1.el7                  epel

找到stream模塊,進(jìn)行安裝

yum -y install nginx-mod-stream.x86_64

安裝完畢。

配置nginx的轉(zhuǎn)發(fā)模塊

nginx需要配置一個(gè)stream的段(segment),就是剛才安裝的這個(gè)模塊的自帶的指令(directive),注意這個(gè)stream和http段是并列的,要寫在/etc/nginx/nginx.conf的最后。

stream {
    log_format basic '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time';
    access_log /var/log/nginx/stream-access.log basic buffer=32k;
    include /etc/nginx/conf.d/*.stream;
}

最后的include是要是將conf.d中的stream文件作為配置的一部分。

我們將有mysql服務(wù)器的地址192.168.10.240 作為要轉(zhuǎn)發(fā)的機(jī)器,配置如下:

root@hy conf.d]# cat mysql.stream
server{
    listen 3306;
    proxy_pass 192.168.10.240:3306;
}

使用 systemctl reload nginx 重新啟動(dòng)nginx

也可以使用systemctl restart nginx啟動(dòng)

如果啟動(dòng)不了,可以使用nginx -t測(cè)試下配置文件是否有錯(cuò),如果有錯(cuò)誤的話,會(huì)有提示。

重啟服務(wù)器以后,看狀態(tài)

[root@hy conf.d]# systemctl restart nginx
[root@hy conf.d]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN
tcp        0      0 192.168.11.9:22         192.168.10.16:48540     ESTABLISHED
tcp        0      0 192.168.11.9:22         172.16.10.20:55362      ESTABLISHED
tcp6       0      0 :::80                   :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 ::1:25

看到nginx已經(jīng)偵聽了3306端口。這個(gè)就是7層轉(zhuǎn)發(fā)的特點(diǎn)。

轉(zhuǎn)發(fā)測(cè)試

我們先到240上看看機(jī)器名稱

[root@hy conf.d]# mysql -uroot -p123456 -h192.168.10.240
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.40 MySQL Community Server (GPL)

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

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

MySQL [(none)]> select @@hostname;
+------------+
| @@hostname |
+------------+
| slave1     |
+------------+
1 row in set (0.00 sec)

機(jī)器名稱是slave1

我們?cè)趎ginx機(jī)器上登錄,nginx的機(jī)器名稱是hy

[root@hy nginx]# mysql -uroot -p123456 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.40 MySQL Community Server (GPL)

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

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

MySQL [(none)]> select @@hostname;
+------------+
| @@hostname |
+------------+
| slave1     |
+------------+
1 row in set (0.00 sec)

我們看到已經(jīng)成功到slave1上去了。

配置負(fù)載均衡

配置負(fù)載均衡,對(duì)于mysql來說,所有的被轉(zhuǎn)發(fā)的服務(wù)器必須先已經(jīng)做好了同步才可,否則數(shù)據(jù)有不對(duì)。

配置很簡(jiǎn)單,就是增加upstream模塊,二臺(tái)服務(wù)器輪詢

upstream backend {
      #  hash $remote_addr consistent;

        server 192.168.10.240:3306 weight=1;
        server 192.168.10.251:3306    weight=1 max_fails=3 fail_timeout=30s;
    }
server{
    listen 3306;
    proxy_pass backend;
}

重新啟動(dòng)

[root@hy conf.d]# mysql -uroot -p123456 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.7.40 MySQL Community Server (GPL)

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

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

MySQL [(none)]> exot exit;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'exot exit' at line 1
MySQL [(none)]> exit
Bye
[root@hy conf.d]# mysql -uroot -p123456 -h127.0.0.1
ERROR 1130 (HY000): Host 'php.qq.com' is not allowed to connect to this MySQL server

可以看到在輪詢,只不過一臺(tái)機(jī)器是不能登錄而已。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx服務(wù)器下使用rewrite重寫url以實(shí)現(xiàn)偽靜態(tài)的示例

    Nginx服務(wù)器下使用rewrite重寫url以實(shí)現(xiàn)偽靜態(tài)的示例

    這篇文章主要介紹了Nginx服務(wù)器下使用rewrite重寫url以實(shí)現(xiàn)偽靜態(tài)的示例,這里舉了Discuz!和WordPress這兩個(gè)常用的PHP程序,需要的朋友可以參考下
    2015-12-12
  • Nginx 反向代理與負(fù)載均衡運(yùn)行小結(jié)

    Nginx 反向代理與負(fù)載均衡運(yùn)行小結(jié)

    Nginx還支持對(duì)后端服務(wù)器進(jìn)行健康檢查,當(dāng)某個(gè)服務(wù)器不可用時(shí),Nginx會(huì)自動(dòng)將流量重定向到其他可用的服務(wù)器,這篇文章給大家分享Nginx 反向代理與負(fù)載均衡是如何運(yùn)行的,感興趣的朋友一起看看吧
    2024-03-03
  • nginx 配置靜態(tài)緩存及靜態(tài)緩存文件沒有生成的問題及解決方案

    nginx 配置靜態(tài)緩存及靜態(tài)緩存文件沒有生成的問題及解決方案

    這篇文章主要介紹了nginx 配置靜態(tài)緩存及靜態(tài)緩存文件沒有生成的問題及解決方案,本文分步驟結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題

    Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題

    這篇文章主要介紹了Nginx捕獲并自定義proxy_pass返回的錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • nginx代理參數(shù)proxy_pass的實(shí)現(xiàn)

    nginx代理參數(shù)proxy_pass的實(shí)現(xiàn)

    proxy_pass參數(shù)用于配置反向代理,本文主要介紹了nginx代理參數(shù)proxy_pass的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-04-04
  • nginx中的健康檢查方案

    nginx中的健康檢查方案

    這篇文章主要介紹了nginx中的健康檢查方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Nginx中的root和alias指令示例詳解

    Nginx中的root和alias指令示例詳解

    Nginx是一種高性能的Web服務(wù)器軟件,其中root和alias是用于配置資源位置的兩個(gè)指令,root指令用于設(shè)置資源的根目錄,而alias指令則為特定location設(shè)置路徑別名,root適用于整體目錄結(jié)構(gòu),alias適合細(xì)粒度路徑控制,本文詳解這兩個(gè)指令的用途及區(qū)別
    2024-10-10
  • Nginx中split_clients模塊的使用

    Nginx中split_clients模塊的使用

    split_clients模塊可以輕松地實(shí)現(xiàn)A/B測(cè)試,本文主要介紹了Nginx中split_clients模塊的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 深入解析nginx路由location匹配規(guī)則及其優(yōu)先級(jí)

    深入解析nginx路由location匹配規(guī)則及其優(yōu)先級(jí)

    Nginx是一款高性能的Web服務(wù)器和反向代理服務(wù)器,它的路由功能是通過location指令來實(shí)現(xiàn)的,location指令用于匹配請(qǐng)求的URL,并將請(qǐng)求轉(zhuǎn)發(fā)到相應(yīng)的處理程序或靜態(tài)文件,需要的朋友可以參考下
    2023-10-10
  • 解讀nginx反向代理location和proxy_pass的映射關(guān)系

    解讀nginx反向代理location和proxy_pass的映射關(guān)系

    這篇文章主要介紹了解讀nginx反向代理location和proxy_pass的映射關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評(píng)論