MongoDB Connection String
What Is Connection String?
A connection string is a URI that tells mongosh, Compass, or your driver where the database is and how to authenticate. The standard format starts with mongodb://, followed by optional credentials, one or more hosts with ports, an optional default database, and optional query-string options. MongoDB Atlas uses the mongodb+srv:// form, which looks up host names through DNS.
Why It Matters
| Reason | Benefit |
|---|---|
| One setting for every tool | mongosh, Compass, and drivers all accept the same URI format. |
| Replica set aware | Listing several hosts lets drivers find the primary and fail over. |
| Options in one place | Timeouts, TLS, and write behavior can be set directly in the URI. |
| Easy environment switching | Keep the URI in an environment variable and change it between dev and production. |
How To Use It
- Start with the scheme: mongodb:// for self-managed servers or mongodb+srv:// for Atlas.
- Add credentials if authentication is enabled, percent-encoding special characters.
- Add the host and port, such as localhost:27017.
- Add a default database after the slash if you want one.
- Append options after ? separated by &, for example authSource and retryWrites.
- Test with mongosh using the full URI in quotes.
- Store the URI in an environment variable such as MONGODB_URI in your app.
mongosh Examples
# Local, no authentication
mongosh "mongodb://localhost:27017"
# Local with credentials and default database
mongosh "mongodb://appUser:s3cret@localhost:27017/shop?authSource=admin"
# Replica set with several hosts
mongosh "mongodb://db1.example.com:27017,db2.example.com:27017,db3.example.com:27017/shop?replicaSet=rs0"
# Atlas style SRV string
mongosh "mongodb+srv://appUser:[email protected]/shop?retryWrites=true&w=majority"
# Percent-encode special characters: p@ss -> p%40ss
mongosh "mongodb://appUser:p%40ss@localhost:27017/shop?authSource=admin" Edge Cases & Pitfalls
Unencoded special characters
Characters such as @, :, /, and ? in a password break URI parsing. Encode them.
Wrong authSource
If the user was created in admin but you do not set authSource=admin, authentication fails.
SRV needs DNS
mongodb+srv requires working DNS resolution and does not allow a port in the URI.
Shell quoting
Always wrap the URI in quotes, because & and ? have meaning in shells.
Cheat Sheet
| Topic | Detail |
|---|---|
| mongodb:// | Standard scheme with explicit hosts and ports |
| mongodb+srv:// | DNS seed list scheme used by Atlas; no port, TLS is on by default |
| user:password@ | Optional credentials before the host |
| host:port | Default port is 27017; list several hosts separated by commas |
| /database | Optional default database for the connection |
| ?authSource=admin | Database where the user is defined |
| ?replicaSet=rs0 | Name of the replica set to connect to |
| ?retryWrites=true | Retry certain write operations once on transient errors |
| ?tls=true | Use TLS for the connection |
| mongodb://localhost:27017 | Typical local development URI |
Key Takeaways
Special characters in a username or password must be percent-encoded (for example @ becomes %40). Never commit connection strings with real passwords.
Frequently Asked Questions
What is the default MongoDB port?
27017 for mongod and mongos. Shard servers and config servers started with their cluster roles default to 27018 and 27019.
What is the difference between mongodb:// and mongodb+srv://?
mongodb:// lists hosts and ports explicitly. mongodb+srv:// uses a single DNS name that resolves to the hosts, and enables TLS by default.
How do I connect to local MongoDB?
Run mongosh with no arguments, or use mongosh "mongodb://localhost:27017".
Where do I put the password?
Between the scheme and the host in the form user:password@host. Percent-encode any special characters.
What is authSource?
It names the database that holds the user's credentials. It is often admin.
Is it safe to put the connection string in code?
No. Keep it in an environment variable or a secrets manager so credentials are not committed to source control.
Next: MongoDB Compass
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
