MySQL 未知服务器主机(通过 ssh 隧道和 CLI,但不是工作台)

发布于 2025-01-05 20:20:11 字数 685 浏览 1 评论 0原文

当我尝试通过 SSH 隧道访问 mysql 时,出现错误:

ERROR 2005 (HY000): Unknown MySQL server host '[serverhost]' (0)

当我通过 MySQL Workbench 或普通 SSH CLI 进行尝试时,它工作正常。我做错了什么? (我已扫描了该站点上的多个相关线程,但没有一个线程向我解释了此行为。)

TMI:

我拥有的主机是一个名称,而不是 IP。

我这样打开隧道:

plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:localhost:[remote-mysql-port]

我在命令行上调用 mysql(通过隧道),这样:

mysql -h [serverhost] -u [user] -p[pw] --port [local-listen-port]

我通过 SSH 命令行(无隧道)调用 mysql,这样:

mysql -h [serverhost] -u [user] -p[pw] --port [remote-mysql-port]

When I attempt to access mysql over an SSH tunnel, I get the error:

ERROR 2005 (HY000): Unknown MySQL server host '[serverhost]' (0)

When I make the attempt through MySQL Workbench or over an ordinary SSH CLI, it works fine, though. What am I doing wrong? (I have scanned multiple related threads on this site, but none that explained this behaviour to me.)

TMI:

The host I have is a name, not an IP.

I open the tunnel thus:

plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:localhost:[remote-mysql-port]

I call mysql on my command line (through tunnel) thus:

mysql -h [serverhost] -u [user] -p[pw] --port [local-listen-port]

I call mysql over an SSH command line (no tunnel) thus:

mysql -h [serverhost] -u [user] -p[pw] --port [remote-mysql-port]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我只土不豪 2025-01-12 20:20:11

您现有的 plink 命令是错误的,因为它指定 localhost 作为目标地址(这是 SSH 会话另一端的 localhost,因此 [remote-ip],根据您的计算,这就是您收到错误的原因)-它应该是[服务器主机]。

然后,您错误地尝试将mysql直接连接到[serverhost],但隧道不影响路由,所以这是不正确的,您应该连接到本地主机。

您现有的示例命令在连接到 [remote-ip] 上的 ssh shell 会话时有效,因为您是从 [remote-ip] 进行连接,该设备可能可以访问 [serverhost]。

假设 [serverhost] 是远程服务器,您的命令应该是:

plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:[serverhost]:[remote-mysql-port]

然后

mysql -h localhost -u [user] -p[pw] --port [local-listen-port]

编辑:让我尝试为您绘制图表,因为您显然误解了隧道的性质:

SSH 连接

本地主机[?] <-------->远程IP[22]

隧道

localhost[本地端口] <---- SSH --->远程 IP[22] <--- TCP --->
服务器主机[远程端口]

因此,SSH/plink 使用您指定的 [local-port] 号码在 localhost 上绑定了一个端口,当收到连接请求时,建立从 [remote-ip] 到 [serverhost] 的连接:remote-port],然后使用其自己的 SSH 连接从 [localhost] 到 [remote-ip] 的跃点在它们之间来回发送/接收。

因此,一旦建立了隧道,要访问 [serverhost:remote-port],您实际上将工具指向 [localhost:local-port],隧道将流量路由到适当的位置。

Your existing plink command is wrong, as its specifying localhost as the destination address (which is the localhost on the other side of the SSH session, so [remote-ip], by your reckoning, which is why you are getting an error) - it should be [serverhost].

Then, you are then mistakenly attempting to connect mysql directly to [serverhost], but the tunnel does not affect routing, so that is incorrect, you should be connecting to localhost.

Your existing example command works when connected to an ssh shell session on [remote-ip], because you are connecting from [remote-ip], which has access to [serverhost] presumably.

Assuming [serverhost] is the remote server, your commands should be:

plink.exe [user]@[remote-ip] -P [ssh-port] -pw [pw] -L [local-listen-port]:[serverhost]:[remote-mysql-port]

Then

mysql -h localhost -u [user] -p[pw] --port [local-listen-port]

EDIT: Let me try and diagram it for you, since you are obviously misunderstanding the nature of the tunnel:

The SSH connection

localhost[?] <--------> remote-ip[22]

The tunnel

localhost[local-port] <---- SSH ---> remote-ip[22] <--- TCP --->
serverhost[remote-port]

Therefore, SSH/plink has bound a port on localhost, using the [local-port] number you have specified, that, when receiving a connection request, establishes a connection from [remote-ip] to [serverhost:remote-port], then shuffles the sends/receives back and forth between them, using its own SSH connection for the hop from [localhost] to [remote-ip].

So - once the tunnel has been set up, to access [serverhost:remote-port], you actually point your tools at [localhost:local-port], and the tunnel routes the traffic to the appropriate place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文