c# - 通过 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
解决方案
请更改以下内容并重试
- ssl=假
- remove replicaSet=rs0 :您将无法使用 SSH 隧道作为副本集连接到 Amazon DocumentDB。有关详细信息,请参阅此链接。
这是更新的字符串
"mongodb://{0}:{1}@{2}/test?ssl=false&readpreference={3}"
推荐阅读
- python - clearsceeen 和 equal 函数在我的计算器代码中没有响应
- java - Java 代码在逐步调试模式下运行良好,而不是在默认运行模式下
- .net - 在没有安装 excel 客户端的情况下从 .net 代码生成 excel 文件
- log4j2 - 每个 JUnit 5 测试的日志文件,以便将其附加到 Allure 报告?
- python - 如何在python代码中拥有cfiles
- asp.net-core - ASP.NET Core 2.2 如何在支持HTTPS PFX证书的同时绑定域名?
- ruby - 无法在 Heroku 服务器上以生产模式运行
- python - 如何通过 Python 中的命名空间对象访问 argparse 位置参数
- javascript - React Firebase 中多个数据之间的信息
- visual-studio - 本地 TFS 无法从私人提要中恢复 nuget 包