windows 下使用nginx对mysql进行负载均衡

本文介绍了在Windows环境下如何使用nginx对mysql进行负载均衡。从nginx 1.9.0版本开始支持tcp协议,使得在Windows下实现mysql负载成为可能。通过检查nginx已安装模块,确认stream模块存在,避免了编译过程。接着修改nginx.conf配置文件,注意避免端口冲突,并启动nginx和mysql。最后,通过netstat命令检查nginx运行状态,并验证连接成功。实际的负载均衡场景中,还需设置mysql集群以实现数据同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

windows 下使用nginx对mysql进行负载均衡

1.自从nginx版本1.9之后,nginx 便增加了对tcp与udp协议的支持

官方文档
The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the –with-stream configuration parameter.

2.但是在linux下默认不安装这个模块,需要在编译时通过指定 –with-stream 参数来激活这个模块。

在windows下

1.我希望在window下做mysql的均衡
2.于是使用命令查看nginx已安装的模块

E:\nginx-1.12.1>nginx -V

#版本信息
nginx version: nginx/1.12.1
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.0.2l  25 May 2017
TLS SNI support enabled

#安装的模块
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.40 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-select_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail **--with-stream **--with-openssl=objs.msvc8/lib/openssl-1.0.2l --with-openssl-opt=no-asm --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

3.可以看到已经安装了,需要的模块
4.免去了window下编译nginx的步骤,感谢大佬

对mysql进行均衡负载

1.修改nginx.conf

#stream  测试mysql均衡 2017-8-23
stream
{

    upstream mysql{
        hash $remote_addr consistent;
        server 127.0.0.1:3306 max_fails=3 fail_timeout=30s;

    }

    server {

        listen 3308;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysql;

    }
}

#简单的测试配置,更详细请参考官方文档
#https://nginx.org/en/docs/stream/ngx_stream_core_module.html

2.注意:如果你在本机上(Nginx与mysql都在同一机器上)做测试,server的端口不要为3306,不然会与mysql的端口冲突

3.如果是生产环境,负载均衡服务器与mysql不在同一机器,可以为3306

4.启动nginx,启动mysql

5.你可以使用命令行来查看端口占用,查看nginx是否运行成功

netstat -ano|findstr “3308”
使用nginx的地址与端口,直接连接mysql,查看是否连接成功

mysql的负载均衡

1.以上测试,只使用了一台mysql服务器,真正的负载均衡可不会
2.还要做以下的工作,创建mysql的集群,将mysql的数据同步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值