我正在尝试通过SSH隧道连接到远程服务器上的Postgres数据库。我正在使用SSH.NET和Npgsql库。这是我的示例代码:
using (var client = new SshClient("210.130.90.110", "root", "pasword"))
{
client.Connect();
if (!client.IsConnected)
{
// Display error
Console.WriteLine("Client not connected!");
}
else
{
Console.WriteLine("Client connected!");
}
var port = new ForwardedPortLocal("127.0.0.1", 15432, "210.130.90.110", 5432);
client.AddForwardedPort(port);
port.Start();
using (var conn = new NpgsqlConnection("Server=127.0.0.1;Database=dbname;Port=15432;User Id=dbuser;Password=dbpassword;"))
{
conn.Open();
}
port.Stop();
client.Disconnect();
}
执行代码后,我得到:
Npgsql.NpgsqlException:'从流中读取时发生异常' EndOfStreamException:尝试读取流的末尾。
我能够使用DBeaver连接到数据库。
答案 0 :(得分:1)
端口转发的远程端已在服务器上解析。因此,您需要以一种可行的方式设置IP地址/主机名,因为它是从服务器本身连接 。
数据库可能仅在Your personal information provided to us will be used
privacy policy
接口上进行侦听。不在公共IP地址上(如果有的话,一开始您可能不需要端口转发)。
此外,对本地转发的端口进行硬编码也不是一个好主意。您不知道该端口是否尚未被另一个应用程序占用。让系统选择任何可用的本地端口。
localhost