【Linux】CentOS6通过nginx进行mysql端口转发实操记录

写在前面

在使用nginx进行mysql端口转发时,尝试了很久的centos6防火墙端口转发,一直无效。因为一样的思路,在centos7上是可以正常使用,只能暂且归因跟操作系统版本有关。
centos7上,端口没有服务,telnet生效,且在防火墙设置端口转发是生效的。
centos6上,端口没有服务,telnet无效,且在防火墙设置端口转发也无效的。

[root@host-173-26-32-176 ~]# cat /etc/redhat-release 
CentOS release 6.10 (Final

nginx下载

http://nginx.org/en/download.html
【Linux】CentOS6通过nginx进行mysql端口转发实操记录_第1张图片
选择Stable version(稳定版)进行下载,截图红圈中的为Linux版本。

nginx解压及其配置安装

1、上传下载好的nginx-1.16.1.tar.gz,并进行解压tar -zxvf nginx-1.16.1.tar.gz

2、进入解压出来的安装目录cd nginx-1.16.1

3、进行软件编译./configure --prefix=/opt/nginx --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_stub_status_module && make && make install

4、进入perfix参数指定好的安装目录,找到conf目录下的nginx.conf配置文件,设置用于进行端口转发的stream模块,如下所示:

stream {
     
    #访问服务器的2201端口,会自动转到173.26.32.42服务器的3306端口
    #stream模块要与http模块处于同一级别
	server {
     
		listen 2201;
		proxy_pass 173.26.32.42:3306;
	}
}

http {
     
    #略
}

验证测试

1、查看服务器端口的监听情况

[[email protected] ~]# netstat -anltp | grep 2201
tcp     0      0 0.0.0.0:2201        0.0.0.0:*       LISTEN      28500/nginx

2、访问设置的端口测试端口转发是否成功

[[email protected] ~]# mysql -u maintainer -p -h173.26.32.176 -P2201
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 259
Server version: 5.7.28-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

设置成功。

你可能感兴趣的:(【Linux】CentOS6通过nginx进行mysql端口转发实操记录)