Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
Click here >
Docs Menu
Docs Home
/

Connect to a Cluster

Learn how to connect to MongoDB clusters whether they are hosted on MongoDB Atlas or self-managed.

To connect to your deployment, you need a connection string. The connection string format depends on whether you connect to a replica set, sharded cluster, or standalone deployment.

Replica Set:

mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin&replicaSet=myRepl

Sharded Cluster:

mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com/?authSource=admin

Standalone:

mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com/?authSource=admin

If the username or password includes the following characters, those characters must be converted using percent encoding:

$ : / ? # [ ] @

For detailed information about connection string formats and options, see Connection Strings.

How you obtain your connection string depends on whether your deployment is hosted on MongoDB Atlas or self-managed.

To connect to a cluster hosted on MongoDB Atlas, see Connect to an Atlas Cluster, which covers how to:

  • Get your connection string via the Atlas CLI or Atlas UI.

  • Configure your IP access list.

  • Create database users.

  • Use various connection methods including drivers, Compass, mongosh, and more.

Before you connect to a self-managed deployment:

  • Ensure your MongoDB deployment is running and accessible.

  • Create a database user with the appropriate privileges.

  • Verify network connectivity between your client and the MongoDB deployment.

To construct your connection string, use the appropriate connection string format for your deployment topology.

After you have your connection string, you can connect using the following methods:

Back

Clustered Collections

On this page