用于 MySQL 的 Nginx 反向代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32246992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:14:46  来源:igfitidea点击:

Nginx reverse proxy for MySQL

mysqlnginx

提问by Allen

I'm trying to use a reverse proxy for mysql. For some reason this doesn't work (where mysql-1.example.com points to a VM with MySQL).

我正在尝试为 mysql 使用反向代理。出于某种原因,这不起作用(其中 mysql-1.example.com 指向带有 MySQL 的 VM)。

upstream db {
    server mysql-1.example.com:3306;
}

server {
    listen 3306;
    server_name mysql.example.com;

    location / {
        proxy_pass http://db;
    }
}

Is there a correct way to do this? I tried connecting via mysql, but doens't work

有没有正确的方法来做到这一点?我尝试通过 mysql 连接,但不起作用

回答by samottenhoff

Make sure your config is not held within the http { } section of nginx.conf. This config should be outside of the http {}.

确保您的配置不在 nginx.conf 的 http { } 部分中。此配置应在 http {} 之外。

stream {
  upstream db {
    server mysql-1.example.com:3306;
  }

  server {
    listen 3306;
    proxy_pass db;
  }
}

回答by Luca Bruzzone

You're trying to accomplish a TCP proxy with an http proxy, which is wrong.

您正在尝试使用 http 代理完成 TCP 代理,这是错误的。

Nginx can do the TCP load balancing/proxy stuff but the syntax is different.

Nginx 可以做 TCP 负载平衡/代理的事情,但语法不同。

look at https://www.nginx.com/resources/admin-guide/tcp-load-balancing/for more info

查看https://www.nginx.com/resources/admin-guide/tcp-load-balancing/了解更多信息

回答by wwerner

It should be possible as of nginx 1.9 using TCP reverse proxies. You need to compile nginx with the --with-streamparameter. Then, you can add streamblock in your config like @samottenhoff said in his answer.

从 nginx 1.9 开始应该可以使用 TCP 反向代理。您需要使用--with-stream参数编译nginx 。然后,您可以stream像@samottenhoff 在他的回答中所说的那样在您的配置中添加块。

For more details, see https://www.nginx.com/resources/admin-guide/tcp-load-balancing/and http://nginx.org/en/docs/stream/ngx_stream_core_module.html.

有关更多详细信息,请参阅https://www.nginx.com/resources/admin-guide/tcp-load-balancing/http://nginx.org/en/docs/stream/ngx_stream_core_module.html