使用Renci.SSH到MySQL Server的SSH访问 [英] SSH Access using Renci.SSH to MySQL Server
问题描述
尝试使用Renci主机SSH库访问MySQL服务器.
Trying to access a MySQL server using the Renci host SSH library.
我通过遵循信息在其中创建转发端口来使其工作SSH隧道
具体地说,这条线成功设置了我的本地端口,以便可以绑定它:
Specifically this line got my local port sucessfully set up such that it could be bound:
ForwardedPortLocal port = new ForwardedPortLocal("localhost", 3306, "localhost", 3306));
这很好,但是据我了解,SSH客户端绑定到3306以从服务器接收数据.如果用户已经安装了MySQL,这将是一个问题,因为它将使用此端口,因此我的服务无法使用.当然可以停止服务,但这不是一个非常友好的过程. 我原本希望能够通过High-Ephemeral-端口在连接期间进行监听.
This is fine, but as I understand it the SSH client is binding to 3306 to receive data back from the server. This is a problem if the user has MySQL already installed as it will be using this port so my service cannot. Of course the service can be stopped but this is not a very friendly process. I was expecting to be able to pass a High - Ephemeral - Port to listen on for the duration of my connection.
我最初应该认为第二个端口是我需要绑定的本地端口,我对应该传递哪个参数感到有些困惑.在对端口配置进行了广泛的试验之后,我对如何处理此问题一无所知.
I got a bit confused on which parameter I should pass, having originally thought the second port would be the local port I need to bind to. After extensive experimentation on port configs I am at a loss as to how to handle this.
此外,我尝试了各种重载,但其他3个重载似乎都没有产生我想要的.
In addition I tried various overloads but none of the 3 other overloads seemed to produce what I wanted.
有什么提示吗?
谢谢
安迪
推荐答案
好,我现在已经解决了这个问题.
Ok I've resolved this now.
解决方案是修改MySQL连接器字符串,使其使用临时端口.随机选择一个.
The solution is to modify the MySQL connector string so it uses a Ephemeral port. Picked one at random.
server = localhost; uid =; pwd =;数据库=;端口= 14324
server=localhost; uid=;pwd=; database=; port=14324
然后修改ForwardedLocalPort以绑定到该端口.
Then modify the ForwardedLocalPort to bind to this port.
ForwardedPortLocal port = new ForwardedPortLocal("localhost", 14324, "localhost", 3306);
因此,我们正在将到localhost 14324的连接转发到远程服务器上的localhost 3306.
So we're forwarding the connection to localhost 14324 to localhost 3306 on the remote server.
接下来的挑战是如何确保我使用的端口尚未绑定!
Next challenge how to ensure that the port I use isn't already bound!!
为此,请使用不需要端口号的重载.
So to do this use the overload that does not require a port number i.e.
ForwardedPortLocal("localhost", "localhost", 3306)
这将为您的进程分配一个可用的临时端口,该端口可以使用:
This will allocate an available Ephemeral port to your process which can be found using:
port.Start();
var portNumber = port.BoundPort;
然后可以将其添加到您的MySQL连接字符串中.
This can then be added to your MySQL connection string.
这篇关于使用Renci.SSH到MySQL Server的SSH访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!