MongoDB Atlas SRV Connection String (mongodb+srv://)
Use the mongodb+srv:// protocol for MongoDB Atlas clusters. Covers DNS SRV records, automatic replica discovery, and Atlas-specific connection options.
Detailed Explanation
MongoDB Atlas and SRV Connections
MongoDB Atlas uses the mongodb+srv:// protocol, which leverages DNS SRV records to automatically discover cluster members without listing individual hostnames.
Standard vs SRV Format
Standard format (listing each host):
mongodb://user:pass@shard1.example.com:27017,shard2.example.com:27017,shard3.example.com:27017/mydb?replicaSet=atlas-xxx&ssl=true
SRV format (Atlas recommended):
mongodb+srv://user:pass@cluster0.abc123.mongodb.net/mydb?retryWrites=true&w=majority
The SRV format is shorter, more readable, and automatically discovers all cluster nodes via DNS.
How SRV Works
When a MongoDB driver sees mongodb+srv://, it performs two DNS lookups:
- SRV record for
_mongodb._tcp.cluster0.abc123.mongodb.net— returns the actual hostnames and ports of all cluster members - TXT record for
cluster0.abc123.mongodb.net— returns default connection options (e.g.,authSource=admin&replicaSet=atlas-xxx)
This means the cluster can add/remove nodes or change ports without requiring connection string updates in your application.
Atlas Connection String from the Dashboard
MongoDB Atlas generates your connection string automatically:
- Go to your Atlas cluster
- Click Connect
- Choose your driver (Node.js, Python, etc.)
- Copy the connection string and replace
<password>with your actual password
Common Atlas Options
mongodb+srv://user:pass@cluster0.abc123.mongodb.net/mydb?retryWrites=true&w=majority&appName=myapp
| Parameter | Purpose |
|---|---|
retryWrites=true |
Automatically retry failed writes |
w=majority |
Writes acknowledged by majority of nodes |
appName=myapp |
Identifies your app in Atlas monitoring |
maxPoolSize=50 |
Maximum connection pool size |
readPreference=secondaryPreferred |
Read from secondaries when possible |
Important Notes
- No port number — SRV connections resolve ports via DNS, so you omit the port
- TLS is always enabled — Atlas requires TLS; the SRV protocol enables it by default
- Password encoding — Special characters in passwords must be percent-encoded (use this tool to handle that automatically)
- IP Access List — Atlas requires your IP to be whitelisted. Use
0.0.0.0/0for development only; restrict to specific IPs in production
Use Case
Connecting a Node.js, Python, or Java application to a MongoDB Atlas cluster in production, taking advantage of automatic node discovery and TLS for secure cloud database access.