因公司linux服务器mysql的3306端口不对外网开发,所以mysql管理工具就无法连接上,现在又需要把大量数据导入服务器,所以就研究了下面的实现方法。
------------------------------------------------------20210811补充 INDE------------------------------------------------
1、如果使用yum install -y nginx安装的nginx,通过nginx -v查看版本nginx version: nginx/1.20.1,确保nginx版本要1.9.0以上,版本太低没有stream这个功能
2、查看yum源,查找有没有 nginx-mod-stream.x86_64
yum list | grep nginx
3、安装stream,yum install -y nginx-mod-stream.x86_64 安装后就可以使用stream这个功能
------------------------------------------------------20210811补充 END------------------------------------------------
一、nginx使用stream配置代理mysql3306端口
1、nginx版本要1.9.0以上,版本太低没有stream这个功能
2、下载nginx,1.16.0
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
3、编译 主要是在末尾加上--with-stream
yum install -y gcc pcre pcre-devel openssl openssl-devel gd gd-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-stream
make && make install
4、修改配置文件,添加如下配置
#反向代理mysql
stream {
server {
listen 443;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass 127.0.0.1:3306;
}
}
完整配置内容:
vim /usr/local/nginx/conf/nginx.conf
nginx.conf
#user www;
worker_processes 1;
error_log logs/error.log warn;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
#反向代理mysql
stream {
server {
listen 443;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass 127.0.0.1:3306;
}
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
access_log off;
#隐藏 Nginx 的版本号,提高安全性。
server_tokens off;
#是否开启目录列表访问,默认关闭。
autoindex off;
#开启 gzip 压缩。
gzip on;
#默认编码
charset UTF-8;
#虚拟主机配置
include conf.d/*.conf;
}
/usr/local/nginx/conf/conf.d/default.conf
server {
listen 80;
listen 443 ssl;
server_name www.**.com;
ssl_certificate conf.d/mycert.crt;
ssl_certificate_key conf.d/mycert.key;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
#DENY:浏览器拒绝当前页面加载任何Frame页面
#SAMEORIGIN:frame页面的地址只能为同源域名下的页面
#ALLOW-FROM:允许frame加载的页面地址
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
#VUE 前端代码
proxy_pass http://127.0.0.1:3000;
}
#静态资源目录
location /asset {
#设置缓存时间
expires 30d;
proxy_pass http://127.0.0.1:81;
}
#api接口
location /api{
#DENY:浏览器拒绝当前页面加载任何Frame页面
#SAMEORIGIN:frame页面的地址只能为同源域名下的页面
#ALLOW-FROM:允许frame加载的页面地址
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
#IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:81;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
配置文件修改好,以后就可以启动nginx服务器了
cd /usr/local/nginx/sbin
#启动服务器
./nginx
#重新加载配置文件
./nginx -s reload
#停止服务器
./nginx -s stop
二、使用本地数据库管理工具就连接远程linux服务器上的mysql,只要把端口改成你nginx中配置的就可以了,我这边使用的是443,数据处理完记得还原配置文件,把3306端口转发代码注释掉,需要使用的时候再开启。