MongoDB Atlas connection string explained
An Atlas connection string (URI) looks like mongodb+srv://<db_username>:<db_password>@cluster0.abcd1.mongodb.net/?retryWrites=true&w=majority. The +srv prefix tells the driver to look up the cluster's servers in DNS and turns TLS on; the user and password are a database user, not your Atlas login; the options after ? tune retries and write safety. Copy it from the cluster's Connect dialog or with atlas clusters connectionStrings describe.
The parts of the string
mongodb+srv://app_user:S3cret%[email protected]/shop?retryWrites=true&w=majority| Part | Meaning |
|---|---|
mongodb+srv:// | The SRV (DNS seed list) format. The driver looks up DNS records to find the cluster's members, and TLS is switched on automatically. |
app_user:S3cret%21@ | A database user and its password, percent-encoded (! became %21). Optional: you can pass credentials as client options instead. |
cluster0.abcd1.mongodb.net | The cluster's SRV host name. It names the cluster, not one server, so it survives failovers and scaling. |
/shop | Optional default database. Without an authSource option it is also used as the authentication database. |
retryWrites=true | Retry certain writes once after a network blip or primary election. Official drivers already default to true. |
w=majority | Acknowledge writes only once a majority of the replica set has them. |
Where to get yours
- Atlas UI: open the cluster, click Connect, choose your driver, and copy the string under Use this connection string in your application. It uses the SRV format by default; turn off the SRV toggle to see the standard
mongodb://form. - Atlas CLI:
atlas clusters connectionStrings describe cluster0prints it. - Before it works you need two more things, both in Database & Network Access: a database user, and your IP on the IP access list. The Connect dialog offers to add your current IP for you.
The copied string contains placeholders such as <db_password>. Replace each whole placeholder, angle brackets included; leaving them in is a classic cause of bad auth : authentication failed.
SRV versus the standard format
The +srv string is short because DNS does the work. Behind cluster0.abcd1.mongodb.net are SRV records listing each member and its port, and a TXT record that can carry replicaSet and authSource. Options in your string override the TXT record. Spelled out, the same cluster in the standard format lists every host (host and replica set names here are illustrative):
mongodb://app_user:S3cret%[email protected]:27017,cluster0-shard-00-01.abcd1.mongodb.net:27017,cluster0-shard-00-02.abcd1.mongodb.net:27017/?tls=true&replicaSet=atlas-xyz-shard-0&authSource=adminUse the SRV string unless you have a reason not to. Reasons to switch: a DNS resolver that can't do SRV lookups (errors like querySrv ENOTFOUND or DNSHostNotFound: Failed to look up service), or an old driver or database tool that fails on SRV. Even then, copy the standard string from Atlas rather than writing host names yourself: Atlas doesn't guarantee that member host names stay the same through topology changes.
Options worth knowing
| Option | What it does | When to set it |
|---|---|---|
authSource=admin | Database the user authenticates against | Atlas password users live in admin; set it if you also give a default database |
appName=orders-api | Names the client in server logs | Always useful when several apps share a cluster |
maxPoolSize=20 | Caps connections per client | Serverless functions and small tiers with low connection limits |
serverSelectionTimeoutMS | How long to wait for a usable server (default 30,000 ms) | Lower it so health checks fail fast; don't raise it to hide network problems |
readPreference=secondary | Send reads to secondaries | Reporting and analytics reads that can be slightly stale |
Keep the full string in a secret or environment variable, never in source control. If a string leaks, rotate the database user's password; the string itself can't be revoked.
Questions
What is the difference between mongodb:// and mongodb+srv://?
mongodb:// lists every host and port in the string. mongodb+srv:// gives one host name and lets the driver fetch the member list from DNS SRV records, and it turns TLS on by default. Atlas clusters use the SRV format unless you choose otherwise.
Is the Atlas connection string the same as my Atlas login?
No. The user and password in the string belong to a database user created under Database & Network Access. Your Atlas account email and password only sign you in to the Atlas web app.
Do I need retryWrites=true?
Official MongoDB drivers already default to retryable writes, so it is harmless but usually redundant. Atlas includes it in the strings it shows.
Sources
- Connection String Formatswww.mongodb.com
- Connection String Optionswww.mongodb.com
- Connect to a Cluster via Client Librarieswww.mongodb.com
- Connect to an Atlas Clusterwww.mongodb.com
Shardherd brings your Atlas clusters to iPhone.