Is mongodb+srv necessary for a MongoDB connection

14 April,2022 by Rambler

Question: I tried to connect to a MongoDB server just using the mongodb prefix -  mongo "mongodb://myuser:mypassword@myurl/test?retryWrites=true&w=majority" and got an error message :

Error: couldn't connect to server xxxxxxxxxxx, connection attempt failed: HostNotFound: Could not find address for xxxxxxxxxx: SocketException: Host not found (authoritative) :

But if I  use - mongo "mongodb+srv://myuser:mypassword@myurl/test?retryWrites=true&w=majority" it works ok . What is the difference with the SRV ?

 

Answer: MongoDB  - from version 3.6 - introduced the concept of a DNS seed list. From the MongoDB docs - "In order to leverage the DNS seed list, use a connection string prefix of mongodb+srv rather than the standard mongodb. The +srv indicates to the client that the hostname that follows corresponds to a DNS SRV record. The driver or mongosh will then query the DNS for the record to determine which hosts are running the   mongod instances"

Benefits:

1) The use of SRV records eliminates the requirement for every client to pass in a complete set of state information for the cluster. Instead, a single SRV record identifies all the nodes associated with the cluster (and their port numbers) and an associated TXT record defines the options for the URI.

2)Can change underlying servers without the need to reconfigure client connection strings

 

For more detailed information check out the MongoDB connection string documentation


Author: Rambler (http://www.dba-ninja.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on Is mongodb+srv necessary for a MongoDB connection


dba-ninja.com