In this blog post, which is part of the series "A Developer's Notes", we'd like to record useful MongoDB information in regards to connecting to your MongoDB setup in the right way, which is via TXT and SRV DNS records.
Introduction
MongoDB clients and applications connect to the database via the connection string. Connection strings can be used in two different ways:
Specification of host names or IP addresses (mongodb://)
Specification of a DNS alias (mongodb+srv://)
The variant via DNS has the great advantage that hosts can be exchanged without having to adapt the connection string.
To use the DNS variant you'd need to create multiple DNS records on your DNS server (or with your DNS provider):
A TXT record containing the connection string options
Multiple SRV records depending on how many MongoDB hosts there are
An example for DNS
Connection String:
mongodb+srv://user:pwd@cluster0.wctoc.mongodb.net/db?options
In this example, the DNS "cluster0.wctoc.mongodb.net" points to a replica set with 3 servers.
As soon as the MDB driver receives a connection string in the "mongodb+srv://" format, it performs a lookup of the SRV records by appending "_mongodb._tcp." (at the beginning) to retreive the seed list, a list of MongoDB hosts:
nslookup -q=SRV _mongodb._tcp.cluster0.wctoc.mongodb.net
Server: 10.128.36.211
Address: 10.128.36.211#53
Non-authoritative answer:
_mongodb._tcp.cluster0.wctoc.mongodb.net service = 0 0 27017 cluster0-shard-00-00.wctoc.mongodb.net.
_mongodb._tcp.cluster0.wctoc.mongodb.net service = 0 0 27017 cluster0-shard-00-01.wctoc.mongodb.net.
_mongodb._tcp.cluster0.wctoc.mongodb.net service = 0 0 27017 cluster0-shard-00-02.wctoc.mongodb.net.
Authoritative answers can be found from:
nslookup cluster0-shard-00-00.wctoc.mongodb.net
Server: 10.128.36.211
Address: 10.128.36.211#53
Non-authoritative answer:
cluster0-shard-00-00.wctoc.mongodb.net canonical name = ec2-18-169-1-235.eu- west-2.compute.amazonaws.com.
Name: ec2-18-169-1-235.eu-west-2.compute.amazonaws.com
Address: 18.169.1.235
The driver then receives the respective IP addresses of the individual hosts.
TXT record
The name of the replica set and other options can be made known via another TXT record. Again, without touching the application:
dig -t TXT cluster0.wctoc.mongodb.net
; <<>> DiG 9.11.22 <<>> -t TXT cluster0.wctoc.mongodb.net
;; global options: +cmd
;; Got answer:
;; >>HEADER<< opcode: QUERY, status: NOERROR, id: 12735
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 1220
; COOKIE: 92061beb865d78920100000066e1c06964d0ed359b799647 (good) ;; QUESTION SECTION:
;cluster0.wctoc.mongodb.net. IN TXT
;; ANSWER SECTION:
cluster0.wctoc.mongodb.net. 5 IN TXT "authSource=admin&replicaSet=atlas-1439ex-shard-0"
;; Query time: 1 msec
;; SERVER: 10.128.36.211#53(10.128.36.211) ;; WHEN: Wed Sep 11 18:08:09 CEST 2024
;; MSG SIZE rcvd: 144
Conclusion
By using the "mongodb+srv://" formatted connection string for connecting your clients and applications to your MongoDB servers you gain flexibility for whenever your hosts IP addresses or hostnames change. This is the recommended way of connecting to your MongoDB.
We hope that the above explanation together with the included commands help you to understand the process behind that approach.
Please get in touch, should there still be questions.
Thanks for reading.
Keep on mongodb'ing.
Photo by Heike Mintel on Unsplash.