For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Connection Strings

You can use connection strings to define connections between MongoDB instances and the following destinations:

To connect to your cluster, you can use one of these connection string formats:

  • SRV connection strings use the mongodb+srv:// prefix to simplify connecting to a cluster. SRV connection strings automatically include all seed list hosts, which supports server rotation without client reconfiguration. When possible, use SRV connection strings over standard connection strings.

  • Standard connection strings use the mongodb:// prefix and require you to include all cluster members in replica sets and sharded clusters.

Use the selectors at the top of the page to choose your deployment type and connection string format. Complete the following steps to find your connection string.

To find your MongoDB Atlas connection string using the Atlas CLI, install and connect from the Atlas CLI, then run the following command. Replace <clusterName> with the name of the MongoDB Atlas cluster and replace <projectId> with the project ID.

atlas clusters connectionStrings describe <clusterName>
--projectId <projectId>

Your MongoDB Atlas connection string resembles the following example:

mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority

To learn more, see atlas clusters connectionStrings describe.

The following example connects to an Atlas cluster that has been configured to support authentication via AWS IAM credentials:

mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS

Connecting to Atlas using AWS IAM credentials in this manner uses the MONGODB-AWS authentication mechanism and the $external authSource.

If you use an AWS session token, specify an AWS_SESSION_TOKEN in the authMechanismProperties value, as shown in the following example:

mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>

Note

If the AWS access key ID, the secret access key, or the session token include the following characters:

$ : / ? # [ ] @

those characters must be converted using percent encoding.

You can also set these credentials on your platform using standard AWS IAM environment variables. mongosh checks for the following environment variables when you use the MONGODB-AWS authentication mechanism:

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_SESSION_TOKEN

If set, these credentials do not need to be specified in the connection string.

The following example sets these environment variables in the bash shell:

export AWS_ACCESS_KEY_ID='<aws access key id>'
export AWS_SECRET_ACCESS_KEY='<aws secret access key>'
export AWS_SESSION_TOKEN='<aws session token>'

Syntax for setting environment variables in other shells will be different. Consult the documentation for your platform for more information.

To verify that these environment variables have been set, run the following command:

env | grep AWS

Once set, the following example connects to an Atlas cluster using these environment variables:

mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS

To find your MongoDB Atlas connection string in the Atlas UI, follow these steps:

1
  1. If it's not already displayed, select the organization that contains your desired project from the Organizations menu in the navigation bar.

  2. If it's not already displayed, select your project from the Projects menu in the navigation bar.

  3. In the sidebar, click Clusters under the Database heading.

    The Clusters page displays.

2
  1. Click Connect for the cluster to which you want to connect.

  2. Click Choose a Connection Method. MongoDB Atlas selects Standard Connection by default. To connect using a private endpoint, select Private Endpoint.

3

To connect to your application, click Drivers. To connect using tools, click the tool you want to use to access your data.

4
  1. If you selected Drivers, select your driver and version. If you selected a tool, download the tool.

  2. Select Connect To Cluster.

  3. Copy the connection string. Replace <password> and <username> in the connection string with the database user's credentials.

Your MongoDB Atlas connection string resembles the following example:

mongodb+srv://myDatabaseUser:D1fficultP%40ssw0rd@cluster0.example.mongodb.net/?retryWrites=true&w=majority

The following example connects to an Atlas cluster that has been configured to support authentication via AWS IAM credentials:

mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS

Connecting to Atlas using AWS IAM credentials in this manner uses the MONGODB-AWS authentication mechanism and the $external authSource.

If you use an AWS session token, specify an AWS_SESSION_TOKEN in the authMechanismProperties value, as shown in the following example:

mongodb+srv://<aws access key id>:<aws secret access key>@cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<aws session token>

Note

If the AWS access key ID, the secret access key, or the session token include the following characters:

$ : / ? # [ ] @

those characters must be converted using percent encoding.

You can also set these credentials on your platform using standard AWS IAM environment variables. mongosh checks for the following environment variables when you use the MONGODB-AWS authentication mechanism:

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

  • AWS_SESSION_TOKEN

If set, these credentials do not need to be specified in the connection string.

The following example sets these environment variables in the bash shell:

export AWS_ACCESS_KEY_ID='<aws access key id>'
export AWS_SECRET_ACCESS_KEY='<aws secret access key>'
export AWS_SESSION_TOKEN='<aws session token>'

Syntax for setting environment variables in other shells will be different. Consult the documentation for your platform for more information.

To verify that these environment variables have been set, run the following command:

env | grep AWS

Once set, the following example connects to an Atlas cluster using these environment variables:

mongodb+srv://cluster0.example.com/testdb?authSource=$external&authMechanism=MONGODB-AWS

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following replica set connection string includes these elements:

  • The hostnames of the mongod instance(s) as listed in the replica set configuration.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

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

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

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following replica set connection string includes these elements:

  • The hostnames of the mongod instance(s) as listed in the replica set configuration.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?authSource=admin&replicaSet=myRepl

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

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following sharded cluster connection string includes these elements:

  • The hostnames of the mongos instances of the sharded cluster.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

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

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

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following sharded cluster connection string includes these elements:

  • The hostnames of the mongos instances of the sharded cluster.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongos0.example.com:27017,mongos1.example.com:27017,mongos2.example.com:27017/?authSource=admin

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

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following connection string for a standalone node includes these elements:

  • The hostname of the mongod instance.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

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:

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

If you are connected to your self-hosted MongoDB deployment, run db.getMongo() method to return the connection string.

If you are not connected to your deployment, you can determine your connection string based on the connection string format and options you want to use.

The following connection string for a standalone node includes these elements:

  • The hostname of the mongod instance.

  • Authentication as user myDatabaseUser with the password D1fficultP%40ssw0rd.

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

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

$ : / ? # [ ] @

The following examples show connection strings for common connection targets.

The following example connects and logs in to the admin database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost

The following example connects and logs in to the records database as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost/records

When you connect to a UNIX domain socket, use a URL-encoded connection string.

The following example connects to a UNIX domain socket with file path /tmp/mongodb-27017.sock as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@%2Ftmp%2Fmongodb-27017.sock

The following example connects to a replica set with two members, one on db1.example.net and the other on db2.example.net, as user myDatabaseUser with the password D1fficultP%40ssw0rd:

Note

For a replica set, specify the hostname(s) of the mongod instance(s) as listed in the replica set configuration.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@db1.example.net,db2.example.com/?replicaSet=test

The following example connects to a replica set with three members running on localhost on ports 27017, 27018, and 27019 as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@localhost:27017,localhost:27018,localhost:27019/?replicaSet=myRepl

The following example connects to a replica set with three members and distributes reads to the secondaries as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&readPreference=secondary

The following example connects to a replica set with write concern configured to wait for replication to succeed across a majority of the data-bearing voting members, with a two-second timeout. It authenticates as user myDatabaseUser with the password D1fficultP%40ssw0rd.

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl&w=majority&wtimeoutMS=2000

The following example connects to a sharded cluster with three mongos instances as user myDatabaseUser with the password D1fficultP%40ssw0rd:

mongodb://myDatabaseUser:D1fficultP%40ssw0rd@router1.example.com:27017,router2.example2.com:27017,router3.example3.com:27017/

For a full list of connection string options, see Connection String Options.

Rate this page

On this page