最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

通過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反向代理配置及優(yōu)化

    詳解nginx反向代理配置及優(yōu)化

    本篇文章主要介紹了詳解nginx反向代理配置及優(yōu)化,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例

    Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例

    Nginx?不支持傳統(tǒng)的四層負(fù)載均衡,但可以通過stream模塊配合TCP實(shí)現(xiàn)類似的功能,本文主要介紹了Nginx四層負(fù)載均衡的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • Nginx配置移動(dòng)端和PC端自動(dòng)跳轉(zhuǎn)方式

    Nginx配置移動(dòng)端和PC端自動(dòng)跳轉(zhuǎn)方式

    文章介紹了如何通過Nginx配置PC端和移動(dòng)端自動(dòng)跳轉(zhuǎn),PC端和移動(dòng)端各有獨(dú)立的域名,PC端訪問任何域名時(shí)會(huì)跳轉(zhuǎn)到www.yxf.com,移動(dòng)端訪問任何域名時(shí)會(huì)跳轉(zhuǎn)到m.yxf.com,配置時(shí)需要修改Nginx的conf文件,使用302或301重定向
    2025-11-11
  • nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解

    nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解

    這篇文章主要介紹了nginx設(shè)置資源緩存實(shí)戰(zhàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 淺談Nginx七層反向代理和負(fù)載均衡

    淺談Nginx七層反向代理和負(fù)載均衡

    這篇文章主要介紹了淺談Nginx七層反向代理和負(fù)載均衡,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • 一次說清Nginx反向代理及參數(shù)配置

    一次說清Nginx反向代理及參數(shù)配置

    我們?cè)谂渲梅?wù)時(shí)常常會(huì)用到Nginx來設(shè)置反向代理,雖然常用,但是我們真的了解各個(gè)參數(shù)的意思嗎,本文就給大家一次說清Nginx反向代理及參數(shù)配置,需要的朋友可以參考下
    2023-05-05
  • 一文讀懂Ingress-Nginx的實(shí)踐

    一文讀懂Ingress-Nginx的實(shí)踐

    Ingress-Nginx是Kubernetes中管理HTTP和HTTPS流量的重要工具,本文深入探討Ingress-Nginx工作原理、配置及最佳實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯(cuò)誤解決

    Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Requ

    HTTP 413 Request Entity Too Large錯(cuò)誤常常出現(xiàn)在客戶端發(fā)送的請(qǐng)求體超過服務(wù)器允許的大小限制時(shí),本文主要介紹了Nginx上傳文件出現(xiàn)“ 413 (499 502 404) Request Entity Too Large錯(cuò)誤解決,感興趣的可以了解一下
    2024-07-07
  • 使用nginx正向代理實(shí)現(xiàn)訪問外網(wǎng)

    使用nginx正向代理實(shí)現(xiàn)訪問外網(wǎng)

    這篇文章主要介紹了使用nginx正向代理實(shí)現(xiàn)讓內(nèi)網(wǎng)主機(jī)通過外網(wǎng)主機(jī)訪問外網(wǎng),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • Linux下用Nginx作Perl程序服務(wù)器及其中Perl模塊的配置

    Linux下用Nginx作Perl程序服務(wù)器及其中Perl模塊的配置

    這篇文章主要介紹了Linux下用Nginx作Perl程序服務(wù)器及其中Perl模塊的配置,文中使用到了FastCGI中間件進(jìn)行連接,需要的朋友可以參考下
    2016-02-02

最新評(píng)論

唐海县| 望都县| 定兴县| 道孚县| 抚远县| 休宁县| 同心县| 尉氏县| 股票| 东丽区| 海城市| 漳州市| 焉耆| 和静县| 扬中市| 南雄市| 通河县| 潍坊市| 临沭县| 德安县| 平泉县| 闵行区| 闵行区| 吴忠市| 海兴县| 林州市| 太仆寺旗| 沙坪坝区| 湄潭县| 云和县| 新邵县| 芜湖市| 嘉兴市| 香格里拉县| 团风县| 舒城县| 龙井市| 苍南县| 平遥县| 华坪县| 湄潭县|