当前位置 博文首页 > 文章内容

    Nginx代理访问RDS

    作者: 栏目:未分类 时间:2020-10-25 18:00:24

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    一、简单网络拓扑图

    适用于RDS不能绑定公网IP地址。

     

    二、安装配置nginx

    1. 安装nginx

    yum install nginx

    1. 启动nginx

    systemctl start nginx

    1. 配置nginx为开机启动

    systemctl enable nginx

    1. 打开nginx配置文件

    vim /etc/nginx/nginx.conf

    在配置文件中在 http{} 结构体外新加如下配置:

    注:

    1. 10.128.40.128是RDS的内网地址,可以通过和ECS在同一VPC下的ECS去ping RDS的连接地址,能够解析到内网IP地址。

    2. ECS所在的安全组需开放对应的数据库监听端口,这里是监听3306端口。

     

    stream {

        upstream cloudsocket {

           hash $remote_addr consistent;

          # $binary_remote_addr;

           server 10.128.40.14:3306 weight=5 max_fails=3 fail_timeout=30s;

        }

        server {

           listen 3306; #数据库服务器监听端口

           proxy_connect_timeout 10s;

           proxy_timeout 300s; #设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。

           proxy_pass cloudsocket;

        }

    }

     

    完整配置:

    [root@iZe3e05p9rga88h9vwl5hcZ ~]# vim /etc/nginx/nginx.conf

     

    # For more information on configuration, see:

    #   * Official English Documentation: http://nginx.org/en/docs/

    #   * Official Russian Documentation: http://nginx.org/ru/docs/

     

    user nginx;

    worker_processes auto;

    error_log /var/log/nginx/error.log;

    pid /run/nginx.pid;

     

    # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

    include /usr/share/nginx/modules/*.conf;

     

    events {

        worker_connections 1024;

    }

     

    http {

        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  /var/log/nginx/access.log  main;

     

        sendfile            on;

        tcp_nopush          on;

        tcp_nodelay         on;

        keepalive_timeout   65;

        types_hash_max_size 2048;

     

        include             /etc/nginx/mime.types;

        default_type        application/octet-stream;

     

        # Load modular configuration files from the /etc/nginx/conf.d directory.

        # See http://nginx.org/en/docs/ngx_core_module.html#include

        # for more information.

        include /etc/nginx/conf.d/*.conf;

     

        server {

            listen       8088 default_server;

            listen       [::]:8088 default_server;

            server_name  _;  

            root         /usr/share/nginx/html;

     

            # Load configuration files for the default server block.

            include /etc/nginx/default.d/*.conf;

     

            location / {

            }

     

            error_page 404 /404.html;

            location = /404.html {

            }

     

            error_page 500 502 503 504 /50x.html;

            location = /50x.html {

            }

        }

     

    # Settings for a TLS enabled server.

    #

    #    server {

    #        listen       443 ssl http2 default_server;

    #        listen       [::]:443 ssl http2 default_server;

    #        server_name  _;

    #        root         /usr/share/nginx/html;

    #

    #        ssl_certificate "/etc/pki/nginx/server.crt";

    #        ssl_certificate_key "/etc/pki/nginx/private/server.key";

    #        ssl_session_cache shared:SSL:1m;

    #        ssl_session_timeout  10m;

    #        ssl_ciphers HIGH:!aNULL:!MD5;

    #        ssl_prefer_server_ciphers on;

    #

    #        # Load configuration files for the default server block.

    #        include /etc/nginx/default.d/*.conf;

    #

    #        location / {

    #        }

    #

    #        error_page 404 /404.html;

    #        location = /404.html {

    #        }

    #

    #        error_page 500 502 503 504 /50x.html;

    #        location = /50x.html {

    #        }

    #    }

     

    }

    stream {

     

        upstream cloudsocket {

           hash $remote_addr consistent;

          # $binary_remote_addr;

           server 10.128.40.14:3306 weight=5 max_fails=3 fail_timeout=30s;

        }

        server {

           listen 3306;#数据库服务器监听端口

           proxy_connect_timeout 10s;

           proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。

           proxy_pass cloudsocket;

        }

    }

                       

    1. 重启nginx

    systemctl restart nginx

    1. 查看断端口监听情况

    netstat -lntp

    三、使用navicat for mysql做测试

    1. 新建连接,选择mysql

     

    1. 填写参数

    主机名为ECS的弹性IP。

    账号为管控台上创建的账号,密码为账号对应的密码。

    已成功连接RDS数据库。

    四、使用命令行连接

    一.Linux

    1. 安装mysql

    yum install mysql -y

    1. 连接数据库

    mysql -hIP地址 -P3306 -u账号 -p密码 -Dmytest

    注:h表示主机,P表示端口,u表示账号,p表示密码,D表示数据库,-D可省略。

     

    二.Windows

    1. 安装MySQL客户端,下载地址为:

    https://dev.mysql.com/downloads/mysql/

    1. 把mysql路径添加到环境变量。

    我的电脑--》右键属性--》高级系统设置--》环境变量--》系统变量

    找到path编辑,新建环境变量。

    1. 使用命令连接RDS数据库。

    mysql -hIP地址 -P3306 -u账号 -p密码 -Dmytest