• Nginx 代理 TCP协议 MySQL连接


    使用nginx代理mysql连接有个好处就是,如果做了容灾处理的话, 可以瞬间平滑切换到可用服务上。

    1. vi /etc/nginx/nginx.conf ,在 http{} 结构体外(也就是文件末尾)添加如下配置:

    stream {
    
        upstream cloudsocket {
           hash $remote_addr consistent;
          # $binary_remote_addr;
           server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
        }
        server {
           listen 3306;#数据库服务器监听端口
           proxy_connect_timeout 10s;
           proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
           proxy_pass cloudsocket;
        }
    }

    2. cat /etc/nginx/nginx.conf ,内容如下:

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    
    stream {
        upstream cloudsocket {
           hash $remote_addr consistent;
          # $binary_remote_addr;
           server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
        }
        server {
           listen 3306;#数据库服务器监听端口
           proxy_connect_timeout 10s;
           proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
           proxy_pass cloudsocket;
        }
    }

    3. systemctl restart nginx ,重启nginx。

    4.验证

    登录192.168.182.156服务器执行看是否有3306端口的监听

    [root@localhost sbin]# netstat -nap|grep 3306
    tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      89870/nginx: master 

    用Navicat for MySQ工具测试是否能连接

  • 相关阅读:
    CMake 3.8.2 Online Manuals
    如何查找UDID
    产品经理那些事儿学习整理笔记
    IOS KVO与NSNotificationCenter简单使用
    整理分享内容
    iOS解决两个静态库的冲突 duplicate symbol
    IOS 添加libMobileVLCKit .a到项目中编译问题
    OpenERP为form和tree视图同时指定view_id的方法
    openerp related字段解读
    openerp图片路径处理
  • 原文地址:https://www.cnblogs.com/phpdragon/p/12530745.html
Copyright © 2020-2023  润新知