1、场景

mysql数据库在纯内网环境,无公网ip,无VPN。

2、方案

在一台具备公网ip,并且能与mysql服务在同一内网环境内的服务器,安装nginx,实现对mysql访问的路由转发。

3、nginx安装

nginx版本需要1.9及以上, nginx既实现http的反向代理,也支持TCP的反向代理。

1)nginx编译时,需要加入--with-stream这个参数,以加载ngx_stream_core_module模块

示例

./configure --prefix=/opt/software/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module --with-pcre=/usr/local/src/pcre-8.35

4、nginx配置文件nginx.conf

监听具备公网ip服务器的3307端口,实现跳转到172.31.88.27的3306端口。

特别注意:stream要与http在同级目录  

stream {
    upstream mysql3306 {
        hash $remote_addr consistent;
        server 172.31.88.27:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
	
	 server {
        listen 3307;
        proxy_connect_timeout 10s;
        proxy_timeout 200s;
        proxy_pass mysql3306;
    }
}

 

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐