psql 在通过 SSH 隧道代理连接时挂起

psql 在通过 SSH 隧道代理连接时挂起

我正在通过 SSH 隧道代理与 PostgreSQL 服务器的连接。

[laptop]  ---(ssh)--> [arbitrary linux server] --(TCP 5432)--> [postgresql server]

ssh user@arbitrarylinuxserver -L5432:postgres:5432

当我尝试在笔记本电脑上运行 psql 时,连接至少挂起几分钟才最终连接:

$ psql -h 127.0.0.1
[... several minutes pass ...]
Username:

如果没有 SSH 隧道,我无法连接到此 postgres 实例,因为它位于防火墙后面。当我不使用 SSH 隧道时,我的本地 psql 可以完美运行。

如果我尝试从我正在 SSH 连接的服务器直接连接到 PostgreSQL 服务器,它会立即起作用,正如预期的那样:

$ psql -h postgresserver.example.com
Username:

实际的 SSH 连接工作正常,当我输入命令时没有任何延迟或挂起。

我正在使用 M2 Macbook。

如果我使用该标志运行 ssh 连接-vvvv,当我尝试从笔记本电脑连接到 postgres 服务器时,我会看到这些消息出现在 ssh 窗口中:

debug1: Connection to port 5432 forwarding to postgres port 5432 requested.
debug2: fd 12 setting TCP_NODELAY
debug3: fd 12 is O_NONBLOCK
debug3: fd 12 is O_NONBLOCK
debug1: channel 3: new [direct-tcpip]
debug3: send packet: type 90
debug3: receive packet: type 91
debug2: channel 3: open confirm rwindow 2097152 rmax 32768

[... hangs for a while ...]

debug3: receive packet: type 96
debug2: channel 3: rcvd eof
debug2: channel 3: output open -> drain
debug2: channel 3: obuf empty
debug2: chan_shutdown_write: channel 3: (i0 o1 sock 12 wfd 12 efd -1 [closed])
debug2: channel 3: output drain -> closed
debug2: channel 3: read failed rfd 12 maxlen 32768: Broken pipe
debug2: channel 3: read failed
debug2: chan_shutdown_read: channel 3: (i0 o3 sock 12 wfd 12 efd -1 [closed])
debug2: channel 3: input open -> drain
debug2: channel 3: ibuf empty
debug2: channel 3: send eof
debug3: send packet: type 96
debug2: channel 3: input drain -> closed
debug2: channel 3: send close
debug3: send packet: type 97
debug3: channel 3: will not send data after close
debug1: Connection to port 5432 forwarding to postgres port 5432 requested.
debug2: fd 13 setting TCP_NODELAY
debug3: fd 13 is O_NONBLOCK
debug3: fd 13 is O_NONBLOCK
debug1: channel 4: new [direct-tcpip]
debug3: send packet: type 90
debug3: channel 3: will not send data after close
debug3: channel 3: will not send data after close
debug3: receive packet: type 97
debug2: channel 3: rcvd close
debug3: channel 3: will not send data after close
debug2: channel 3: is dead
debug2: channel 3: garbage collecting
debug1: channel 3: free: direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56662 to 127.0.0.1 port 5432, nchannels 5
debug3: channel 3: status: The following connections are open:
  #2 client-session (t4 r0 i0/0 o0/0 e[write]/0 fd 9/10/11 sock -1 cc -1 io 0x01/0x00)
  #3 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56662 to 127.0.0.1 port 5432 (t4 r1 i3/0 o3/0 e[closed]/0 fd 12/12/-1 sock 12 cc -1 io 0x00/0x00)
  #4 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432 (t3 nr0 i0/0 o0/0 e[closed]/0 fd 13/13/-1 sock 13 cc -1 io 0x00/0x00)

debug3: receive packet: type 91
debug2: channel 4: open confirm rwindow 2097152 rmax 32768
debug2: channel 4: read failed rfd 13 maxlen 32768: Broken pipe
debug2: channel 4: read failed
debug2: chan_shutdown_read: channel 4: (i0 o0 sock 13 wfd 13 efd -1 [closed])
debug2: channel 4: input open -> drain
debug2: channel 4: ibuf empty
debug2: channel 4: send eof
debug3: send packet: type 96
debug2: channel 4: input drain -> closed
debug3: receive packet: type 96
debug2: channel 4: rcvd eof
debug2: channel 4: output open -> drain
debug3: receive packet: type 97
debug2: channel 4: rcvd close
debug3: channel 4: will not send data after close
debug3: channel 4: will not send data after close
debug2: channel 4: obuf empty
debug2: chan_shutdown_write: channel 4: (i3 o1 sock 13 wfd 13 efd -1 [closed])
debug2: chan_shutdown_write: channel 4: shutdown() failed for fd 13 [i3 o1]: Socket is not connected
debug2: channel 4: output drain -> closed
debug2: channel 4: send close
debug3: send packet: type 97
debug2: channel 4: is dead
debug2: channel 4: garbage collecting
debug1: channel 4: free: direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432, nchannels 4
debug3: channel 4: status: The following connections are open:
  #2 client-session (t4 r0 i0/0 o0/0 e[write]/0 fd 9/10/11 sock -1 cc -1 io 0x01/0x00)
  #4 direct-tcpip: listening port 5432 for postgres port 5432, connect from 127.0.0.1 port 56668 to 127.0.0.1 port 5432 (t4 r1 i3/0 o3/0 e[closed]/0 fd 13/13/-1 sock 13 cc -1 io 0x00/0x02)

[now my postgres connection is working in the other terminal]

我该如何修复这个问题?根据我使用 SSH 隧道的经验,我以前从未遇到过这种情况,它会挂起一段时间然后开始正常工作。

相关内容