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

问题描述

我已经设法将 MongoDb Compass 和 Studio3T 通过 SSH 隧道连接到我在 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
  2. remove replicaSet=rs0 :您将无法使用 SSH 隧道作为副本集连接到 Amazon DocumentDB。有关详细信息,请参阅此 link

这是更新后的字符串

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