您的位置:首页 > 运维架构 > Nginx

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

2017-08-23 11:43 387 查看

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的数据同步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql nginx 负载均衡