MongoDB Connection String

Beginner
6 min read
Updated: Nov 2026
Getting Started

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

ReasonBenefit
One setting for every toolmongosh, Compass, and drivers all accept the same URI format.
Replica set awareListing several hosts lets drivers find the primary and fail over.
Options in one placeTimeouts, TLS, and write behavior can be set directly in the URI.
Easy environment switchingKeep the URI in an environment variable and change it between dev and production.

How To Use It

  1. Start with the scheme: mongodb:// for self-managed servers or mongodb+srv:// for Atlas.
  2. Add credentials if authentication is enabled, percent-encoding special characters.
  3. Add the host and port, such as localhost:27017.
  4. Add a default database after the slash if you want one.
  5. Append options after ? separated by &, for example authSource and retryWrites.
  6. Test with mongosh using the full URI in quotes.
  7. Store the URI in an environment variable such as MONGODB_URI in your app.

mongosh Examples

shell / mongosh
# 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

TopicDetail
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:portDefault port is 27017; list several hosts separated by commas
/databaseOptional default database for the connection
?authSource=adminDatabase where the user is defined
?replicaSet=rs0Name of the replica set to connect to
?retryWrites=trueRetry certain write operations once on transient errors
?tls=trueUse TLS for the connection
mongodb://localhost:27017Typical 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.

MongoDB Compass →

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful