Nikita Fedorov
Nikita Fedorov

Reputation: 870

Unable to connect to MongoDB Atlas Cluster with MongoDB.Driver: No connection could be made because the target machine actively refused it

I'm trying to connect to MongoDB Atlas from my ASP.NET Core 3.1 application:

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

And I'm getting the following error:

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)

I have tried to whitelist all ip addresses in Atlas, but it didn't help. My drivers:

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

I added the .Core driver trying to fix the issue, but it didn't help also.What I'm doing wrong?

P.S. I'm able to connect with mongo shell though.

Upvotes: 0

Views: 532

Answers (1)

Joe
Joe

Reputation: 28336

No connection could be made because the target machine actively refused it. is not a whitelist problem.

Connection refused means the machine at the remote IP received the packet, but there was no process listening on the specified port.

The remote IP was reported in the error as <my_ip>:53. Port 53 is the registered port for a DNS resolver. . So if you are running as DNS server on <my_ip>, you need to figure out why it is not listening.

If are not running a DNS server on my_ip, you need to figure out why your asp.net is using <my_ip> as its DNS resolver.

You might also try using the legacy mongodb:// url for you cluster to see if the problem is only with SRV and TXT resolution.

Upvotes: 2

Related Questions