Nginx反代MySQL案例

本文介绍如何使用Nginx 1.9.x版本代理局域网内的MySQL服务,通过配置实现负载均衡,并提供详细的步骤及验证方法。

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

案例:一个朋友要用Nginx代理MySQL(MySQL局域网),不用***,不用NAT映射等,好吧,做个笔记。

Nginx版本:1.9.x(持tcp的负载均衡,nginx_tcp_proxy_module(姚伟斌阿里团队也可以实现))

Nginx官方模块: ngx_stream_core_module --with-stream_ssl_module(ssl协议支持,比如MySQL ssl)

官网:http://nginx.org/en/docs/stream/ngx_stream_core_module.html

1、查看现有编译

 --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module

2、重新编译:

http://nginx.org/download/

--user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module

注意:--with-stream --with-stream_ssl_module

3、配置、检测、重启nginx:

配置:

stream {
upstream mysql {
zone myapp1 64k;
server localhost:3306 weight=1 max_fails=3 fail_timeout=30s;
#server 192.168.1.221:3306 weight=1 max_fails=2 fail_timeout=30s;   
}
server {
         listen 2188;
         proxy_connect_timeout 1s;
         proxy_timeout 3s;
         proxy_pass mysql;
}
}

检测:

[root@autoCentos67X64 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@autoCentos67X64 conf]#

启动:

[root@autoCentos67X64 conf]# netstat -atupn|grep nginx
tcp        0      0 0.0.0.0:2188                0.0.0.0:*                   LISTEN      2359/nginx          
[root@autoCentos67X64 conf]#

4、验证:(转载请保留:renzhiyuan.blog.51cto.com)

[root@log~]# mysql -uroot -prenzhiyuan -h 192.168.1.11 -P2188
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.21-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

wKiom1jR9suCbYRlAABbQsAeBPM652.png-wh_50

注意:2188可是Nginx的端口,代理(负载)后端的MySQL。其它玩法大家可自己研究。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值