利用linux 负载均衡nginx stream模块反向代理mysql数据库
由于越来越多的企业将业务转移到公有云上,导致公司服务器直接暴露在公网,存在被入侵的风险,云服务器上的mysql 3306端口基本不对外开放。但是遇到一个问题,就是每次通过工具连接数据库都需要公有云内网才能连接,这个时候我们可以利用nginx 的stream模块反向代理mysql,具体部署方法如下
nginx版本需要1.9以上才有stream功能模块,所有我们使用1.10
1.下载nginx
tar xvf nginx-1.10.3.tar.gz

2 .编译安装
cd nginx-1.10.3
./configure --prefix=/data/server/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream
make && make install
3.修改配置文件 vi
/data/server/nginx/config/nginx.conf
stream {
server {
listen 10000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass 127.0.0.1:3306;
}
}

这里是将连接本地的10000端口转发到本地mysql3306端口上,踩过一个坑,就是最开始配置的时候,将stream配置 写在 http里面导致nginx无法启动
4.启动nginx
先检查配置文件是否有错误
/data/server/nginx/sbin/nginx -t

启动nginx
/data/server/nginx/sbin/nginx -c /data/server/nginx/conf/nginx.conf

最后用 Navicat Premium 工具连接测试

整个配置部署完成。
相关推荐
-
第18问:MySQL CPU 高了,怎么办?2025-02-24 10:27:18
-
mysql索引类型 normal, unique, full text
mysql索引类型 normal, unique, full text2025-02-24 10:05:05 -
uwsgi+django+nginx 搭建部分总结2025-02-24 10:03:33
-
使用Docker配置Nginx环境部署Nextcloud2025-02-24 10:02:03
-
Nginx安装和怎么使用2025-02-24 10:00:45