Bussola

mongodb+srv:// Explained: SRV Records, TXT Options and Seed Lists

A mongodb+srv:// connection string is a shorthand: instead of listing every host and port, you give one DNS name, and the driver asks DNS for the seed list and a few options. The driver does a SRV lookup for _mongodb._tcp.<hostname> to get the hosts and ports, a TXT lookup on the same name for a small set of options, and then turns TLS on by default. Everything after that is a normal MongoDB connection. Understanding those two DNS queries explains most “works on my laptop, fails on this network” problems with a mongodb+srv connection string.

Standard vs SRV strings

A standard string lists the seeds explicitly:

mongodb://user:<password>@host1.example.net:27017,host2.example.net:27017,host3.example.net:27017/appdb?replicaSet=rs0&tls=true&authSource=admin

The SRV form carries the same information in DNS:

mongodb+srv://user:<password>@cluster0.example.mongodb.net/appdb?retryWrites=true&w=majority

Rules that follow from the SRV form:

  • You provide exactly one hostname, and no port. A port in a mongodb+srv URI is invalid, because the ports come from the SRV records.
  • tls is implicitly true. You can set tls=false explicitly, but almost nobody should.
  • authSource and replicaSet can come from DNS instead of the URI.
  • Changing the topology (adding or replacing nodes) only requires editing DNS, not every application’s configuration.

What the driver does, step by step

1. The SRV query

For cluster0.example.mongodb.net, the driver queries:

_mongodb._tcp.cluster0.example.mongodb.net  SRV

Each answer holds a priority, weight, port and target host. MongoDB drivers ignore priority and weight; they only use the target and port to build the seed list. You can look at it yourself:

dig +short SRV _mongodb._tcp.cluster0.example.mongodb.net
0 0 27017 cluster0-shard-00-00.example.mongodb.net.
0 0 27017 cluster0-shard-00-01.example.mongodb.net.
0 0 27017 cluster0-shard-00-02.example.mongodb.net.

On Windows, nslookup -type=SRV _mongodb._tcp.cluster0.example.mongodb.net does the same job.

2. The domain check

The driver does not trust arbitrary SRV targets. Every returned host must belong to the same parent domain as the name you put in the URI. Roughly, for cluster0.example.mongodb.net the parent domain is example.mongodb.net, and each target must end in that domain. A record pointing at an unrelated host is rejected. This stops a poisoned or misconfigured DNS answer from redirecting your credentials to a foreign server. The check also means the hostname needs at least three labels; a bare example.net is not a valid SRV host.

3. The TXT query

The driver then queries TXT records for the same name (without the _mongodb._tcp. prefix):

dig +short TXT cluster0.example.mongodb.net
"authSource=admin&replicaSet=atlas-abc123-shard-0"

Only a small allow-list of options is honored from TXT: authSource, replicaSet, and loadBalanced. Anything else is an error. There can be at most one TXT record. Options you put explicitly in the URI take precedence over TXT values, so a URI with authSource=$external overrides the DNS value.

This is why an Atlas string works without replicaSet= or authSource=: Atlas publishes them in TXT.

4. Implicit TLS and the connection

With the seed list known, the driver connects to each host over TLS, runs the usual handshake and server discovery, and from then on behaves exactly as with a standard string. The SRV step is only about finding the initial seeds; the real topology comes from the replica set’s own hello responses.

Limiting hosts with srvMaxHosts

Large deployments can publish many SRV records, and some clients do not need to connect to all of them. The srvMaxHosts option caps how many randomly chosen SRV results are used as seeds:

mongodb+srv://user:<password>@cluster0.example.mongodb.net/?srvMaxHosts=3

Notes:

  • The default is 0, meaning no limit.
  • It is aimed at sharded clusters with many mongos routers, to avoid every client opening connections to every router.
  • It cannot be combined with the replicaSet option (from the URI or TXT), since a replica set client needs to see all members.
  • srvServiceName changes the mongodb part of _mongodb._tcp, if your DNS publishes records under a different service name.

Drivers that support it also re-poll the SRV record periodically on sharded topologies and update the set of mongos hosts. Exact behavior and defaults depend on the driver version, so check your driver’s documentation before relying on it.

Debugging common SRV failures

querySrv ENOTFOUND or “DNS name not found”

Node.js reports querySrv ENOTFOUND _mongodb._tcp.<host>. It means the SRV lookup returned nothing usable. Check, in order:

  1. Typo in the host. Run the dig command above. If it returns no records, the name is wrong (a common cause is copying a cluster name from a deleted or paused project).

  2. The resolver can’t answer SRV queries. Some captive portals, hotel and corporate networks, and home routers with limited DNS proxies fail on SRV or TXT record types while A records work fine. Test against a public resolver:

    dig +short SRV _mongodb._tcp.cluster0.example.mongodb.net @1.1.1.1
    dig +short SRV _mongodb._tcp.cluster0.example.mongodb.net @8.8.8.8

    If these work and your default resolver doesn’t, change the resolver or fall back to a standard string.

  3. Split-horizon or VPN DNS. A VPN client can route DNS to an internal server that doesn’t know the public name.

querySrv ECONNREFUSED

Usually the process can’t reach any DNS server at all, common in containers with a broken resolv.conf or when a sandbox blocks UDP/53. It is a network configuration problem, not a MongoDB one.

SRV resolves, but the connection times out

DNS worked; the TCP/TLS connection did not. On Atlas this is almost always the firewall rather than the string: the error is a server selection timeout, not an authentication failure. See Why MongoDB Atlas Rejects Your Connection on Mobile Data.

Authentication fails only with SRV

If you override authSource or build the string by hand, check that the user was created in the database you name. With SRV, a missing authSource means DNS supplies it, or it defaults to the database in the URI path, or admin. Unexpected combinations here produce “Authentication failed” even though the password is right.

“Invalid URI” for port or multiple hosts

mongodb+srv://host1,host2/ and mongodb+srv://host:27017/ are both invalid. Use a standard mongodb:// string if you need explicit hosts.

Falling back to a standard string

If SRV lookups are blocked on a network you can’t change, resolve the records once and write the equivalent standard string:

mongodb://user:<password>@cluster0-shard-00-00.example.mongodb.net:27017,cluster0-shard-00-01.example.mongodb.net:27017,cluster0-shard-00-02.example.mongodb.net:27017/?tls=true&replicaSet=atlas-abc123-shard-0&authSource=admin

Remember to add tls=true, replicaSet and authSource yourself, since you lose the implicit TLS and the TXT options. Atlas shows this form in its connection dialog for older drivers. The downside is that it goes stale if hosts change, which is exactly the problem SRV exists to avoid.

SRV strings on a phone

Mobile networks are one of the places where DNS behaves oddly: carrier or Wi-Fi resolvers that mishandle SRV, and roaming between networks. A client that talks to the database directly needs to do the SRV and TXT lookups itself. Bussola, a free iPhone MongoDB client, resolves them with the iOS system resolver and falls back to Cloudflare and Google public DNS. For the wider picture of querying from a phone, see How to Query MongoDB From an iPhone Without a Backend, and the app itself is at Bussola.

Quick reference

Element Source Notes
Hosts and ports SRV _mongodb._tcp.<host> Targets must share the parent domain
authSource, replicaSet, loadBalanced TXT on <host> Only these keys; URI values win
TLS Implicit On by default for mongodb+srv
Port in URI Not allowed Comes from SRV
Number of hosts srvMaxHosts Default 0; incompatible with replicaSet
Service name srvServiceName Default mongodb

Checklist when a mongodb+srv string fails:

  • Does dig SRV _mongodb._tcp.<host> return records from your default resolver?
  • Does it work against @1.1.1.1 or @8.8.8.8?
  • Is there exactly one host, no port, and only the allowed TXT options?
  • If DNS is fine, is your current IP allowed by the database firewall?
  • Do authSource and the user’s actual database match?
mongodbconnection-stringdnsatlastroubleshooting

Bussola is a free MongoDB client for iPhone: query, edit and aggregate over a direct connection, with no backend.

Coming soon to the App Store