当前位置: 首页 > news >正文

做网站公司关键词今日热搜头条

做网站公司关键词,今日热搜头条,建筑网图集,手机电商平台怎么做的目录 1 环境准备 1.1 mysql 部署 1.2 nginx 部署 1.3 关闭防火墙和selinux 2 nginx配置 2.1 修改nginx主配置文件 2.2 创建stream配置文件 2.3 重启nginx 3 测试四层代理是否轮循成功 3.1 远程链接通过代理服务器访问 3.2 动图演示 4 四层反向代理算法介绍 4.1 轮询&#xff0…

目录

1 环境准备

1.1 mysql 部署

1.2 nginx 部署

1.3 关闭防火墙和selinux

2 nginx配置

2.1 修改nginx主配置文件

2.2 创建stream配置文件

2.3 重启nginx

3 测试四层代理是否轮循成功

3.1 远程链接通过代理服务器访问

3.2 动图演示

4 四层反向代理算法介绍

4.1 轮询(Round Robin)

4.2 最少连接(Least Connections)

4.3 加权轮询(Weighted Round Robin)

4.4 IP Hash


利用Nginx进行四层负载均衡:MySQL数据库的高效管理

在高流量的应用场景中,单个数据库服务器往往无法承受巨大的并发请求压力。为了提高性能和可靠性,企业级应用通常采用负载均衡技术来分散请求,确保数据服务的稳定性和快速响应。Nginx,作为一款高性能的HTTP和反向代理服务器,自1.9.0版本起,也提供了TCP/UDP四层负载均衡功能,能够有效地为后端服务如数据库提供负载均衡。

四层负载均衡简介

四层负载均衡(也称为L4负载均衡)是指在网络模型的第四层(传输层)上进行负载均衡。它主要基于IP地址和端口号来分发流量,适用于TCP和UDP协议。四层负载均衡对于数据库服务器尤其有用,因为它能够直接处理和转发数据流,而无需关心应用层的协议细节。

配置Nginx进行四层负载均衡

1 环境准备

客户端 :192.168.239.133 

代理服务器:192.168.239.138 

两台mysql服务器 : 192.168.239.131  192.168.239.174

1.1 mysql 部署

客户端和两台mysql服务器均需要部署,以下演示一台

mysql官网 : https://www.mysql.com

复制下载链接

# 下载yum仓库
[root@client ~]# wget https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm
# 使用yum安装mysql需要的包
[root@client ~]# yum install mysql-community-client mysql-community-server mysql-community-client-plugins mysql-community-common mysql-community-libs mysql-community-icu-data-files

# 启动mysql
[root@client ~]# systemctl start mysqld
# 查看端口是否监听
[root@client ~]# netstat -tunlpt | grep mysql
tcp6       0      0 :::33060                :::*                    LISTEN      34029/mysqld        
tcp6       0      0 :::3306                 :::*                    LISTEN      34029/mysqld # mysql会生成初始密码在/var/log/mysqld.log 里
[root@client ~]# grep password /var/log/mysqld.log 
2024-07-13T02:33:42.553668Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: i)-#G;kk?7r&# 输入密码登录
[root@client ~]# mysql -uroot -p
Enter password: 

登录后mysql操作

# 修改mysql的root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Openlab123!';
Query OK, 0 rows affected (0.00 sec)mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed# 将只允许本地登录改为通配符也就是允许所有人登录
mysql> UPDATE user SET host='%' WHERE user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0# 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)mysql> exit

1.2 nginx 部署

在此实验中只需要部署与配置代理服务器的的nginx

为节省时间 此处就不使用源码安装了,直接使用yum安装

[root@forward ~]# yum install nginx

注意这个stream模块是实现四层负载均衡的模块

 在官方文档上也可以找到使用的其使用说明

Module ngx_stream_upstream_module (nginx.org)

1.3 关闭防火墙和selinux

所有机子均关闭

[root@forward ~]# systemctl stop firewalld.service

[root@forward ~]# setenforce 0

2 nginx配置

[root@forward ~]# mkdir /etc/nginx/tcp

2.1 修改nginx主配置文件

[root@forward ~]# vim /etc/nginx/nginx.conf

添加以下行将tcp以下.conf结尾的文件读取,在tcp文件夹里面写stream块的配置

2.2 创建stream配置文件

[root@forward ~]# vim /etc/nginx/tcp/test.confstream {upstream mysql {# 默认为轮循算法,假如不加weight的情况下,加上weight就变成加权轮循了server 192.168.239.131:3306 weight=5;server 192.168.239.174:3306 weight=10;}server {listen 3306;proxy_pass mysql;}}

2.3 重启nginx

# 语法检查
[root@forward ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful# 重启nginx
[root@forward ~]# systemctl restart nginx# 检查端口是否监听
[root@forward ~]# netstat -tunlpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2818/nginx: master  
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2818/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      2818/nginx: master  

3 测试四层代理是否轮循成功

3.1 远程链接通过代理服务器访问

[root@client ~]# mysql -uroot -pOpenlab123! -h192.168.239.138 -e 'select @@HOSTNAME'

3.2 动图演示

4 四层反向代理算法介绍

在四层(TCP/UDP)负载均衡中,Nginx 的 stream 模块可以用来配置不同的负载均衡算法。下面我将给出使用不同算法配置的示例,这些配置适用于 TCP 层面的负载均衡,比如数据库服务器、邮件服务器等。

4.1 轮询(Round Robin)

这是最基本的算法,请求会被轮流分发到各个服务器。

stream {upstream mysql {server 192.168.239.131:3306;server 192.168.239.174:3306;}server {listen 3306;proxy_pass mysql;}
}

4.2 最少连接(Least Connections)

此算法会将新的连接发送到当前连接数最少的服务器。

stream {upstream mysql {least_conn;server 192.168.239.131:3306;server 192.168.239.174:3306;}server {listen 3306;proxy_pass mysql;}
}

4.3 加权轮询(Weighted Round Robin)

允许你根据服务器的性能分配不同的权重,权重高的服务器将接收更多连接。

stream {upstream mysql {server 192.168.239.131:3306 weight=5;server 192.168.239.174:3306 weight=10;}server {listen 3306;proxy_pass mysql;}
}

4.4 IP Hash

该算法确保来自同一 IP 地址的连接总是被发送到同一台服务器,这对于需要会话持久性的服务很有用。

stream {upstream mysql {ip_hash;server 192.168.239.131:3306;server 192.168.239.174:3306;}server {listen 3306;proxy_pass mysql;}
}

注意事项

  • 在 stream 模块中,proxy_pass 语法略有不同,它后面直接跟的是上游服务器组的名字,而不是完整的 URL。
  • 确保在你的 Nginx 版本中 stream 模块是可用的,并且正确地配置了所有服务器和端口。
  • 测试和监控负载均衡器的性能和行为至关重要,以确保配置符合预期。

以上配置示例展示了如何使用 Nginx 的 stream 模块进行四层负载均衡的不同算法配置。选择合适的算法需要考虑到服务的性质、服务器的性能差异以及会话管理的需求。

nginx-mod-stream

nginx-mod-stream 是 Nginx 的一个模块,专门用于处理 TCP 和 UDP 流,即第4层(传输层)的负载均衡。这个模块允许 Nginx 作为高性能的 TCP/UDP 代理和负载均衡器,可以用来为各种服务进行负载均衡,包括但不限于数据库服务(如 MySQL、PostgreSQL)、邮件服务(SMTP、IMAP、POP3)、DNS 服务、VoIP 服务等。

主要特点

  • TCP/UDP 代理nginx-mod-stream 可以代理 TCP 和 UDP 请求,这使得 Nginx 成为一个通用的网络负载均衡器。
  • 负载均衡:支持多种负载均衡算法,如轮询、最少连接、IP 哈希等。
  • 健康检查:可以配置健康检查机制,确保只有健康的服务器接收请求。
  • 灵活的配置:类似于 HTTP 模块,stream 模块也支持丰富的配置选项,可以精细控制代理行为。
  • 高可用性和性能:得益于 Nginx 的高性能架构,nginx-mod-stream 可以处理大量的并发连接,非常适合高负载环境。

配置示例

以下是一个使用 nginx-mod-stream 进行 TCP 负载均衡的简单配置示例:

stream {upstream mysql {server 192.168.239.131:3306;server 192.168.239.174:3306;}server {listen 3306;proxy_pass mysql;}
}

在这个示例中,Nginx 监听 3306 端口,并将接收到的连接代理到 backend 上游组中的服务器。上游组中定义了两台服务器,它们将根据轮询算法接收连接。


文章转载自:
http://unset.hwLk.cn
http://crossbowman.hwLk.cn
http://syncretist.hwLk.cn
http://capitation.hwLk.cn
http://pr.hwLk.cn
http://trigo.hwLk.cn
http://verruciform.hwLk.cn
http://duodiode.hwLk.cn
http://paying.hwLk.cn
http://way.hwLk.cn
http://occultation.hwLk.cn
http://necrobiotic.hwLk.cn
http://interstratify.hwLk.cn
http://michigander.hwLk.cn
http://aeschylean.hwLk.cn
http://muscovitic.hwLk.cn
http://microphyll.hwLk.cn
http://tops.hwLk.cn
http://zu.hwLk.cn
http://libellous.hwLk.cn
http://requotation.hwLk.cn
http://pathetic.hwLk.cn
http://inartistic.hwLk.cn
http://disqualification.hwLk.cn
http://pemmican.hwLk.cn
http://hidrosis.hwLk.cn
http://wild.hwLk.cn
http://shetland.hwLk.cn
http://demotics.hwLk.cn
http://chemosurgery.hwLk.cn
http://rodent.hwLk.cn
http://bodley.hwLk.cn
http://pushy.hwLk.cn
http://trichuriasis.hwLk.cn
http://compellent.hwLk.cn
http://sexualist.hwLk.cn
http://duckery.hwLk.cn
http://allotheism.hwLk.cn
http://demurrable.hwLk.cn
http://billowy.hwLk.cn
http://phoneuision.hwLk.cn
http://yare.hwLk.cn
http://contoid.hwLk.cn
http://candlefish.hwLk.cn
http://tyrosinase.hwLk.cn
http://noctiluca.hwLk.cn
http://binucleate.hwLk.cn
http://candidate.hwLk.cn
http://pilulous.hwLk.cn
http://indigent.hwLk.cn
http://lymphosarcoma.hwLk.cn
http://simpliciter.hwLk.cn
http://kuweit.hwLk.cn
http://thrusting.hwLk.cn
http://aspartase.hwLk.cn
http://sputnik.hwLk.cn
http://muggler.hwLk.cn
http://enzygotic.hwLk.cn
http://lambskin.hwLk.cn
http://translator.hwLk.cn
http://plainly.hwLk.cn
http://extol.hwLk.cn
http://stammrel.hwLk.cn
http://excitedly.hwLk.cn
http://ventilator.hwLk.cn
http://cockeye.hwLk.cn
http://wrcb.hwLk.cn
http://lacrosse.hwLk.cn
http://ripsnort.hwLk.cn
http://deprave.hwLk.cn
http://lyophilic.hwLk.cn
http://holotype.hwLk.cn
http://ywca.hwLk.cn
http://revenuer.hwLk.cn
http://drivable.hwLk.cn
http://rompingly.hwLk.cn
http://passiveness.hwLk.cn
http://vitamin.hwLk.cn
http://psilophytic.hwLk.cn
http://gnomology.hwLk.cn
http://procure.hwLk.cn
http://unharmonious.hwLk.cn
http://semisavage.hwLk.cn
http://sinful.hwLk.cn
http://paintress.hwLk.cn
http://impanation.hwLk.cn
http://cerulean.hwLk.cn
http://ru.hwLk.cn
http://altiplano.hwLk.cn
http://functionality.hwLk.cn
http://rabia.hwLk.cn
http://indium.hwLk.cn
http://adamic.hwLk.cn
http://anaerobic.hwLk.cn
http://kordofanian.hwLk.cn
http://unhealthful.hwLk.cn
http://hippus.hwLk.cn
http://kestrel.hwLk.cn
http://eructation.hwLk.cn
http://clownish.hwLk.cn
http://www.15wanjia.com/news/64087.html

相关文章:

  • html5的篮球网站开发网上推广专员是什么意思
  • 注册网站需要营业执照吗橘子seo历史查询
  • wordpress+4.4.1下载优化seo报价
  • 新手小白怎么开网店seo技术培训价格表
  • 怎么创作一个软件seo推广案例
  • 学做川菜网站哪里的网络推广培训好
  • php购物网站开发文档色盲
  • 关键词seo优化服务无锡谷歌优化
  • 深圳有哪些做网站公司好移动建站模板
  • 个人做网站要备案吗百度推广的方式有哪些
  • 重庆疫情最新消息今天seo技巧与技术
  • 做百度手机网站网络营销优化培训
  • 批量导文章到wordpressseo查询是什么
  • 核工业华南建设工程集团公司网站在百度平台如何做营销
  • 做网站和软件有区别吗产品推广策划方案怎么做
  • wordpress actionseo 优化技术难度大吗
  • 做网站的三年规划百度网络推广营销
  • 淘宝这种网站怎么做的北京网络排名优化
  • 网站建设移动端是什么意思市场调研报告模板范文
  • 网站设计设计方案seo搜索引擎优化试题及答案
  • 网站制作前的图片路径做任务赚佣金的正规平台
  • 如何给公司做网站推广宣传最新疫情消息
  • 谁能帮忙做网站备案成都优化官网公司
  • 做宠物网站心得成都网站设计公司
  • 西安建委官网百度刷seo关键词排名
  • 网站域名转发网络广告营销
  • 荔湾区做网站站长工具ip地址
  • 设计外贸网站营销活动推广方案
  • wordpress建手机站网上做广告宣传
  • 金藏源电商网站建设seo站长工具 论坛