MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (2024)

If you have logged into

MongoDB Atlasrecently - and you should, the entry-level tier is free! - you may havenoticed a strange new syntax on 3.6 connection strings.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (1)

MongoDB Seed Lists

What is this

mongodb+srv syntax?

Well, in MongoDB 3.6 we introduced the concept of a seedlistthat is specified using DNS records, specificallySRV andTXT records. You will recallfrom using replica sets with MongoDB that the client must specify atleast one replica set member (and may specify several of them) whenconnecting. This allows a client to connect to a replica set even if oneof the nodes that the client specifies is unavailable.

You can see an example of this URL on a 3.4 cluster connection string:

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (2)

Note that without the SRV record configuration we must list severalnodes (in the case of Atlas we always include all the cluster members,though this is not required). We also have to specify the ssl andreplicaSet options.

With the 3.4 or earlier driver, we have to specify all the options onthe command line using the MongoDB URIsyntax.

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

Reading SRV and TXT Records

We can see how this works in practice on a MongoDB Atlas cluster with asimple Python script.

We can run this script using the node specified in the 3.6 connectionstring as a parameter.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (4)

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (5)

You can also do this lookup with nslookup:

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (6)

You can see how this could be used to construct a 3.4 style connectionstring by comparing it with the 3.4 connection string above.

As you can see, the complexity of the cluster and its configurationparameters are stored in the DNS server and hidden from the end user. Ifa node's IP address or name changes or we want to change the replica setname, this can all now be done completely transparently from theclient's perspective. We can also add and remove nodes from a clusterwithout impacting clients.

So now whenever you see mongodb+srv you know you are expecting a SRVand TXT record to deliver the client connection string.

Creating SRV and TXT records

Of course, SRV and TXT records are not just for Atlas. You can alsocreate your own SRV and TXT records for your self-hosted MongoDBclusters. All you need for this is edit access to your DNS server so youcan add SRV and TXT records. In the examples that follow we are usingthe AWS Route 53 DNS service.

I have set up a demo replica set on AWS with a three-node setup. Theyare

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (7)

Each has a mongod process running on port 27022. I have set up asecurity group that allows access to my local laptop and the nodesthemselves so they can see each other.

I also set up the DNS names for the above nodes in AWS Route 53.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (8)

We can start the mongod processes by running the following command oneach node.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (9)

Now we need to set up the SRV and TXT records for this cluster.

The SRV record points to the server or servers that will comprise themembers of the replica set. The TXT record defines the options for thereplica set, specifically the database that will be used forauthorization and the name of the replica set. It is important to notethat the mongodb+srv format URI implicitly adds "ssl=true". In ourcase SSL is not used for the demo so we have to append "&ssl=false" tothe client connector. Note that the SRV record is specifically designedto look up the mongodb service referenced at the start of the URL.

The settings in AWS Route 53 are:

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (10)

Which leads to the following entry in the zone file for Route 53.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (11)

Now we can add the TXT record. By convention, we use the same name asthe SRV record (rs.joedrumgoole.com) so that MongoDB knows where tofind the TXT record.

We can do this on AWS Route 53 as follows:

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (12)

This will create the following TXT record.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (13)

Now we can access this service as :

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (14)

This will retrieve a complete URL and connection string which can thenbe used to contact the service.

The whole process is outlined below:

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (15)

Once your records are set up, you can easily change port numbers withoutimpacting clients and also add and remove cluster members.

SRV records are another way in which MongoDB is making life easier fordatabase developers everywhere.

You should also check out full documentation on SRV and TXT records inMongoDB3.6.

You can sign up for a free MongoDB Atlas tierwhich is suitable for single user use.

Find out how to use your favorite programming language with MongoDB viaour MongoDB drivers.

Please visit MongoDB University forfree online training in all aspects of MongoDB.

Follow Joe Drumgoole on twitter formore news about MongoDB.

MongoDB 3.6: Here to SRV you with easier replica set connections | MongoDB (2024)

References

Top Articles
Latest Posts
Article information

Author: Barbera Armstrong

Last Updated:

Views: 5462

Rating: 4.9 / 5 (79 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Barbera Armstrong

Birthday: 1992-09-12

Address: Suite 993 99852 Daugherty Causeway, Ritchiehaven, VT 49630

Phone: +5026838435397

Job: National Engineer

Hobby: Listening to music, Board games, Photography, Ice skating, LARPing, Kite flying, Rugby

Introduction: My name is Barbera Armstrong, I am a lovely, delightful, cooperative, funny, enchanting, vivacious, tender person who loves writing and wants to share my knowledge and understanding with you.