首页 > 解决方案 > 通过 SSH 隧道连接到 .Net Core 中的 AWS DocumentDb

问题描述

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

但是,当尝试使用 .NET 核心进行连接时,我不断收到超时。

我正在按如下方式设置 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

标签: c#ssh-tunnelaws-documentdb

解决方案


请更改以下内容并重试

  1. ssl=假
  2. remove replicaSet=rs0 :您将无法使用 SSH 隧道作为副本集连接到 Amazon DocumentDB。有关详细信息,请参阅此链接

这是更新的字符串

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

推荐阅读