通过 SSH 隧道连接到 .Net Core 中的 AWS DocumentDb

Posted

技术标签:

【中文标题】通过 SSH 隧道连接到 .Net Core 中的 AWS DocumentDb【英文标题】:Connect to AWS DocumentDb in .Net Core through SSH Tunnel 【发布时间】:2021-05-02 12:32:24 【问题描述】:

我已经设法通过 SSH 隧道将 MongoDb Compass 和 Studio3T 连接到了我在 AWS 上的 DocumentDb 集群。因此,VM 或集群上没有配置或安全问题。

但是,当尝试使用 .NET core 进行连接时,我不断出现超时。

我正在按如下方式设置 SSH 隧道:

ssh -i "VMKey.pem" -L 27015:<db-name>.<cluster-name>.eu-central-1.docdb.amazonaws.com:27015 <user>@<vm-name>.eu-central-1.compute.amazonaws.com -N

这里是连接代码:

string template = "mongodb://0:1@2/test?ssl=true&replicaSet=rs0&readpreference=3";
string readPreference = "secondaryPreferred";
string connectionString = String.Format(template, username, password, "localhost:27015", readPreference);

var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionString));
settings.AllowInsecureTls = true;
var client = new MongoClient(settings);

var database = client.GetDatabase("logs-database");
_collection = database.GetCollection<BsonDocument>("logs-collection");

尝试将记录插入数据库后,这是我收到的错误:

A timeout occured after 30000ms selecting a server using CompositeServerSelector Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector AllowedLatencyRange = 00:00:00.0150000  . Client view of cluster state is  ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [ ServerId: " ClusterId : 1, EndPoint : "Unspecified/<logs-db>.ckwpv9kdyp3i.eu-central-1.docdb.amazonaws.com:27015" ", EndPoint: "Unspecified/<logs-db>.ckwpv9kdyp3i.eu-central-1.docdb.amazonaws.com:27015", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. 172.31.41.178:27015
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
   at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
   at System.Net.Sockets.Socket.<>c.<ConnectAsync>b__274_0(IAsyncResult iar)
--- End of stack trace from previous location where exception was thrown ---
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.ConnectAsync(Socket socket, EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.TcpStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.InitializeConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.ServerMonitor.HeartbeatAsync(CancellationToken cancellationToken)", LastHeartbeatTimestamp: "2021-01-28T15:11:45.2629877Z", LastUpdateTimestamp: "2021-01-28T15:11:45.2629880Z" ] .

注意:集群上禁用了 TLS

【问题讨论】:

【参考方案1】:

请更改以下内容并重试

    ssl=false remove replicaSet=rs0 :您将无法使用 SSH 隧道作为副本集连接到 Amazon DocumentDB。详情请参阅link。

这是更新后的字符串

"mongodb://0:1@2/test?ssl=false&readpreference=3"

【讨论】:

以上是关于通过 SSH 隧道连接到 .Net Core 中的 AWS DocumentDb的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 python 中的 ssh 隧道连接到远程数据库

使用隧道 ssh 通过 nodeJS 中的 mongoose 通过 ssh 连接到远程服务器 mongoDB

PHP 通过 SSH 隧道连接到其他网络中的 LDAP

如何通过 ssh 隧道链(双隧道,公司网络中的服务器)连接到 Oracle Database 11g 服务器?

通过 JSch 建立 SSH 隧道

通过ssh隧道连接到MySQL到localhost