共计 2493 个字符,预计需要花费 7 分钟才能阅读完成。
案例:一个朋友要用 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、重新编译:
–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、验证:
[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>
注意:2188 可是 Nginx 的端口,代理(负载)后端的 MySQL。其它玩法大家可自己研究。
下面关于 Nginx 的文章您也可能喜欢,不妨参考下:
CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1 http://www.linuxidc.com/Linux/2016-09/134804.htm
Nginx 实现集群的负载均衡配置过程详解 http://www.linuxidc.com/Linux/2017-02/140549.htm
Nginx 高级应用 – 负载均衡与 rewrite 规则 http://www.linuxidc.com/Linux/2017-02/140476.htm
CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
CentOS 6.8 下源码安装 Nginx 1.11.10 http://www.linuxidc.com/Linux/2017-03/141908.htm
CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm
Nginx 简单实现网站的负载均衡 http://www.linuxidc.com/Linux/2017-02/140351.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/142221.htm