【问题标题】:Vagrant port fowarding on macOS?macOS上的流浪端口转发?
【发布时间】:2017-05-31 08:09:39
【问题描述】:

我在 macOS 上的 Vagrant centos 7.2 机器上运行 postgresql。我已确认 postgres 已在端口 5432 上的 Vagrant 盒子上启动并运行,方法是使用盒子本身上的 psql 连接到它。我正在尝试将 Vagrant box 上的端口 5432 转发到主机上的端口 10001,如下所示:

config.vm.define "acc_db" do | acc_db |
    acc_db.vm.box = "bento/centos-7.2"
    acc_db.vm.hostname = "acc.db"
    acc_db.vm.network :forwarded_port, guest: 5432, host: 10001

    acc_db.vm.provision "shell",
        inline: "yum upgrade -y -q --nogpgcheck"

    acc_db.vm.provision "shell",
        path: "install_postgres.sh" 

我已将我的pg_hba.conf 更改为绑定到所有 IP 地址并允许密码验证,如下所示:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             0.0.0.0/0            md5
# IPv6 local connections:
host    all             all             ::1/128                 ident

我已经关闭了防火墙,但仍然无法连接到主机上的 10001 端口上的 postgres:

psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.10001"?

如何解决这个问题,以便端口转发工作?

我见过https://gielberkers.com/fixing-vagrant-port-forwarding-osx-yosemite/,但还没有尝试过,因为它涉及到我不熟悉的文件。这是正确的方法吗?看起来你必须明确允许你想通过 Vagrant 转发的每个端口。

【问题讨论】:

    标签: macos postgresql vagrant


    【解决方案1】:

    ...connections on Unix domain socket "/tmp/.s.PGSQL.10001" 表示您没有尝试 TCP/IP 连接,因此无法进行端口转发。

    默认情况下,psql 在类 unix 操作系统上使用 Unix domain sockets

    使用 psql 的 -h 选项指定主机,如果使用 IPv4,则大概是 127.0.0.1


    此外,PostgreSQL 服务器必须监听连接将被路由到的网络接口。默认情况下,出于安全原因,它只侦听localhost
    postgresql.conf 中设置listen_addresses='*' 使其监听所有现有接口。

    【讨论】:

    • 谢谢。我将 -h 选项放入:psql -h 127.0.0.1 -p 10001 -u username 并得到psql: server closed the connection unexpectedly。我在 postgres 日志中没有看到任何错误。
    • @user783836: 你有listen_addresses='*' in postgresql.conf 让它监听所有接口吗?
    • 编辑listen-addresses='*'后的魅力!
    猜你喜欢
    • 2016-06-22
    • 2016-06-30
    • 2012-02-20
    • 2012-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多