首页 > 解决方案 > Unable to connect to MongoDB Atlas Cluster with MongoDB.Driver: 无法连接,因为目标机器主动拒绝

问题描述

我正在尝试从我的 ASP.NET Core 3.1 应用程序连接到 MongoDB Atlas:

var client = new MongoClient("mongodb+srv://<user>:<password>@<my_db_instance>/test?retryWrites=true&w=majority");
var database = client.GetDatabase("test");

我收到以下错误:

ExtendedSocketException: No connection could be made because the target machine actively refused it. <my_ip>:53
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)

DnsResponseException: Query 22151 => <my_db_instance> IN TXT on <my_ip>:53 failed with an error.
DnsClient.LookupClient.ResolveQuery(IReadOnlyList<NameServer> servers, DnsQuerySettings settings, DnsMessageHandler handler, DnsRequestMessage request, LookupClientAudit audit)

我曾尝试将 Atlas 中的所有 IP 地址列入白名单,但没有帮助。我的司机:

<PackageReference Include="MongoDB.Driver" Version="2.11.1" />
<PackageReference Include="MongoDB.Driver.Core" Version="2.11.1" />

我添加了 .Core 驱动程序试图解决这个问题,但它也没有帮助。我做错了什么?

PS虽然我可以连接mongo shell。

标签: mongodbasp.net-coreasp.net-core-3.1mongodb-atlas

解决方案


No connection could be made because the target machine actively refused it.不是白名单问题。

连接被拒绝表示远程 IP 的机器收到了数据包,但指定端口上没有进程监听。

远程 IP 在错误中报告为<my_ip>:53. 端口 53 是 DNS 解析器的注册端口。. 因此,如果您在 上作为 DNS 服务器运行<my_ip>,您需要弄清楚它为什么不监听。

如果没有在 上运行 DNS 服务器my_ip,您需要弄清楚为什么您的 asp.net 将<my_ip>其用作其 DNS 解析器。

您也可以尝试mongodb://为您的集群使用旧 URL,以查看问题是否仅与 SRV 和 TXT 分辨率有关。


推荐阅读