Navigation

Connect via Driver

The Connect dialog for a cluster provides the details to connect to a cluster with an application using a MongoDB driver.

3.6-Series Driver Compatibility with MongoDB 4.0

3.6-series drivers (i.e., driver versions that are recommended for MongoDB 3.6 in the driver compatibility matrices) are tested against MongoDB 4.0. As such, applications running with 3.6-series drivers will work against MongoDB clusters running MongoDB 4.0 as long as no functionality new to MongoDB 4.0 is being used.

We still recommend using MongoDB 4.0-series drivers with MongoDB 4.0 to utilize newer database features and better prepare for future version upgrades.

Prerequisites

TLS/SSL

Clients must have support for TLS/SSL to connect to an Atlas cluster.

Clients must have support for the SNI TLS extension to connect to an Atlas M0 Free Tier or M2/M5 shared starter cluster. To verify that your driver supports the SNI TLS extension, refer to the Driver Compatibility matrix for your driver. Drivers which are compatible with MongoDB version 3.4 and up support the SNI TLS extension.

Whitelist
To access a cluster, you must connect from an IP address on the Atlas project’s IP whitelist. If you need to add an IP address to the whitelist, you can do so in the Connect dialog. You can also add the IP address from the Security tab.
MongoDB User

To access a cluster, you must create a MongoDB User with access to the desired database(s) on your Atlas cluster. MongoDB users are separate from Atlas users. MongoDB users have access to MongoDB databases, while Atlas users have access to the Atlas application itself.

You can create a MongoDB user to access to your Atlas cluster in the Connect dialog. You can also add the MongoDB user from the Clusters view.

Connect Your Application

1

Open the Connect dialog.

Go to the Clusters view. Click the Connect button for the cluster to which you wish to connect.

2

Whitelist your connection IP address.

Atlas only allows client connections to the cluster from entries in the project’s whitelist. The project whitelist is distinct from the API whitelist, which restricts API access to specific IP or CIDR addresses.

Note

You can skip this step if Atlas indicates in the Setup Connection Security step that you have already configured a whitelist entry in your cluster. To manage the IP whitelist, see Add Entries to the Whitelist.

If the whitelist is empty, Atlas prompts you to add an IP address to the project’s whitelist. You can either:

  • Click Add Your Current IP Address to whitelist your current IP address.

  • Click Add a Different IP Address to add a single IP address or a CIDR-notated range of addresses.

    For Atlas clusters deployed on Amazon Web Services (AWS) and using VPC Peering, you can add a Security Group associated with the peer VPC.

You can provide an optional description for the newly added IP address or CIDR range. Click Add IP Address to add the address to the whitelist.

3

Create a MongoDB User.

To access the cluster, you must create a MongoDB user with access to the desired database(s) on the cluster.

Note

You can skip this step if Atlas indicates in the Setup Connection Security step that you have at least one MongoDB user configured in your project. To manage existing MongoDB users, see Add MongoDB Users.

If the project has no MongoDB users, Atlas prompts you to create a new user with the Atlas Admin privilege. Enter the new user’s Username and Password and click Create MongoDB User to save the user. Use this user to connect to your cluster in the following step.

Once you have whitelisted an IP address and added a MongoDB user, click Choose Your Connection Method.

4

Select Connect Your Application.

In the Choose a connection method step, select Connect Your Application.

5

Select Your Driver and Version.

  1. Select your driver and version from the dropdown menus.

  2. The Connection String Only tab displays the connection string you can use to connect to your Atlas cluster. The Full Driver Example tab displays a code sample for your selected driver and version demonstrating a connection to your Atlas cluster.

    Note

    The connection string displayed in the UI uses a placeholder value for the password. Be sure to replace it with your own MongoDB password.

    Important

    If the password contains reserved URI characters, you must escape the characters per RFC 2396. For example, if your password is @bc123, you must escape the @ character when specifying the password in the connection string; e.g. %40bc123.

For details on driver compatibility with MongoDB, see Driver Compatibility.

The connection string examples specify the test database for the initial connection. If the user is configured on a different database, specify that database in the connection string.

Driver Examples

In the following example URI connection strings, the user kay specifies their password myRealPassword to authenticate and connect to an Atlas cluster.

Select your driver from the options below:

    Note

    To connect to an Atlas M0 (Free Tier) cluster, you must use Python 2.7.9+ and use a Python driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Python driver and MongoDB, see the MongoDB compatibility matrix.

    import pymongo
    import dns # required for connecting with SRV
    
    client = pymongo.MongoClient("mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority")
    db = client.test
    
    import pymongo
    
    client = pymongo.MongoClient("mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority")
    db = client.test
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 3.7 or greater
    MongoDB 3.6 Version 3.6 or greater
    MongoDB 3.4 Version 3.4 or greater

    macOSX and Python 3.6 Installers

    The Python 3.6 installers for macOS from https://www.python.org do not automatically install any CA certificates. Without installed CA certificates, connections to Atlas will fail certificate verification.

    After you run the installer from https://www.python.org to install Python 3.6, you must run the following script to install an up-to-date CA bundle before connecting to Atlas:

    open "/Applications/Python 3.6/Install Certificates.command"
    

    For more information on Python 3.6 installers for macOS from https://www.python.org, see https://bugs.python.org/issue29065#msg283984. Earlier versions of Python as well as Python 3.6 installed by other means (e.g. Homebrew), are not affected.

    Note

    To connect to an Atlas M0 (Free Tier) cluster, you must use Java version 8 or greater and use a Java driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Java driver and MongoDB, see the MongoDB compatibility matrix.

    MongoClientURI uri = new MongoClientURI(
        "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority");
    
    MongoClient mongoClient = new MongoClient(uri);
    MongoDatabase database = mongoClient.getDatabase("test");
    
    MongoClientURI uri = new MongoClientURI(
        "mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority");
    
    MongoClient mongoClient = new MongoClient(uri);
    MongoDatabase database = mongoClient.getDatabase("test");
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 3.8 or greater
    MongoDB 3.6 Version 3.6 or greater
    MongoDB 3.4 Version 3.4 or greater

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a Node.js driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Node.js driver and MongoDB, see the MongoDB compatibility matrix.

    const MongoClient = require('mongodb').MongoClient;
    const uri = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority";
    const client = new MongoClient(uri, { useNewUrlParser: true });
    client.connect(err => {
      const collection = client.db("test").collection("devices");
     // perform actions on the collection object
      client.close();
    });
    

    To connect to a database other than admin but still authenticate to the admin database, update the database component of the connection string.

    mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
    

    For example, the following connection string specifies test in the database component and includes the authSource=admin option.

    var uriTestDb = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?ssl=true&authSource=admin&w=majority";
    MongoClient.connect(uriTestDb, function(err, db) {
       db.close();
    });
    
    var MongoClient = require('mongodb').MongoClient;
    
    var uri = "mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&w=majority";
    MongoClient.connect(uri, function(err, client) {
       const collection = client.db("test").collection("devices");
       // perform actions on the collection object
       client.close();
    });
    

    To connect to a database other than admin but still authenticate to the admin database, update the database component of the connection string.

    mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
    

    For example, the following connection string specifies test in the database component and includes the authSource=admin option.

    var uriTestDb = "mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
    MongoClient.connect(uriTestDb, function(err, db) {
       db.close();
    });
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 3.1 or greater
    MongoDB 3.6 Version 3.0 or greater
    MongoDB 3.4 Version 2.2.12 or greater

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a PHP driver version that supports MongoDB 3.4. For complete documentation on compatibility between the PHP driver and MongoDB, see the MongoDB compatibility matrix.

    The following example uses the MongoDB PHP Library which provides a high-level abstraction around the lower-level PHP driver:

    $client = new MongoDB\Client(
        'mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?ssl=true&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority"');
    
    $db = $client->test;
    
    $client = new MongoDB\Client(
        'mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority"');
    
    $db = $client->test;
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 PHPLIB 1.4 + mongodb-1.5
    MongoDB 3.6 PHPLIB 1.3 + mongodb-1.3
    MongoDB 3.4 PHPLIB 1.1 + mongodb-1.1

    Behavior

    MongoDB drivers automatically attempt server selection following a cluster election or failover event. By default, the driver immediately raises an error if its first attempt to select a server fails. The following configuration settings may improve application connectivity to an Atlas cluster at the expense of spending more time in a server selection loop:

    • Set serverSelectionTryOnce to false to direct the driver to perform server selection up to the time limit defined by serverSelectionTimeoutMS.
    • Lower the serverSelectionTimeoutMS to 15000 from the default of 30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000 milliseconds) covers the upper bound of election plus additional time for latency.

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a C driver version that supports MongoDB 3.4. For complete documentation on compatibility between the C driver and MongoDB, see the MongoDB compatibility matrix.

    client = mongoc_client_new ("mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority");
    db = mongoc_client_get_database (client, "test");
    
    mongoc_client_t client = mongoc_client_new ("mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority");
    db = mongoc_client_get_database (client, "test");
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 1.11 or greater
    MongoDB 3.6 Version 1.9 or greater
    MongoDB 3.4 Version 1.5 or greater

    Behavior

    Note

    The following configuration options only apply if running the C driver in single-threaded mode.

    MongoDB drivers automatically attempt server selection following a cluster election or failover event. By default, the C driver immediately raises an error if its first attempt to select a server fails. The following configuration settings may improve application connectivity to an Atlas cluster at the expense of spending more time in a server selection loop:

    • Set serverSelectionTryOnce to false to direct the C driver to perform server selection up to the time limit defined by serverSelectionTimeoutMS.
    • Lower the serverSelectionTimeoutMS to 15000 from the default of 30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000 milliseconds) covers the upper bound of election plus additional time for latency.

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a C++ driver version that supports MongoDB 3.4. For complete documentation on compatibility between the C++ driver and MongoDB, see the MongoDB compatibility matrix.

    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    
    //...
    
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{"mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?ssl=true&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority"}};
    mongocxx::database db = conn["test"];
    
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    
    //...
    
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{"mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority"}};
    mongocxx::database db = conn["test"];
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 3.4.x
    MongoDB 3.6 Version 3.3.x
    MongoDB 3.4 Version 3.1.x

    Note

    The Legacy C++ driver has reached End-Of-Life, and is no longer supported.

    Behavior

    Note

    The following configuration options only apply when using the C++ driver’s single-threaded mongocxx::client class to connect to the Atlas cluster.

    MongoDB drivers automatically attempt server selection following a cluster election or failover event. By default, the C++ driver immediately raises an error if its first attempt to select a server fails. The following configuration settings may improve application connectivity to an Atlas cluster at the expense of spending more time in a server selection loop:

    • Set serverSelectionTryOnce to false to direct the C++ driver to perform server selection up to the time limit defined by serverSelectionTimeoutMS.
    • Lower the serverSelectionTimeoutMS to 15000 from the default of 30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000 milliseconds) covers the upper bound of election plus additional time for latency.

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a C#/.Net driver version that supports MongoDB 3.4. For complete documentation on compatibility between the C#/.Net driver and MongoDB, see the MongoDB compatibility matrix.

    var client = new MongoClient("mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?ssl=true&authSource=admin&w=majority");
    var database = client.GetDatabase("test");
    
    var client = new MongoClient("mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority");
    var database = client.GetDatabase("test");
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 2.7
    MongoDB 3.6 Version 2.5
    MongoDB 3.4 Version 2.4

    Note

    The SNI TLS extension on Linux and OSX is only supported by .NET Core library versions 2.1 and greater. Applications using prior versions of .NET Core on these operating systems cannot connect to a Atlas M0 Free Tier or M2/M5 shared-tier cluster.

    For more information, see the dotnet/corefx github page.

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a Perl driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Perl driver and MongoDB, see the MongoDB compatibility matrix.

    my $client = MongoDB->connect('mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority');
    my $db = $client->get_database( 'test' );
    
    my $client = MongoDB->connect('mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority');
    my $db = $client->get_database( 'test' );
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 3.1 or greater
    MongoDB 3.6 Version 3.0 or greater
    MongoDB 3.4 Version 2.2.12 or greater

    Behavior

    MongoDB drivers automatically attempt server selection following a cluster election or failover event. By default, the Perl driver immediately raises an error if its first attempt to select a server fails. The following configuration settings may improve application connectivity to an Atlas cluster at the expense of spending more time in a server selection loop:

    • Set serverSelectionTryOnce to false to direct the Perl driver to perform server selection up to the time limit defined by serverSelectionTimeoutMS.
    • Lower the serverSelectionTimeoutMS to 15000 from the default of 30000. MongoDB elections typically take 10 seconds, but can be as fast as 5 seconds on Atlas. Setting this value to 15 seconds (15000 milliseconds) covers the upper bound of election plus additional time for latency.

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a Ruby driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Ruby driver and MongoDB, see the MongoDB compatibility matrix.

    client = Mongo::Client.new('mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?ssl=true&w=majority')
    

    To connect to a database other than admin but still authenticate to the admin database, update the database component of the connection string.

    mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
    

    For example, the following connection string specifies test in the database component and includes the authSource=admin option.

    client = Mongo::Client.new('mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority')
    
    client = Mongo::Client.new('mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&w=majority')
    

    To connect to a database other than admin but still authenticate to the admin database, update the database component of the connection string.

    mongodb://username:password@host1:port1,...,hostN:portN/database?authSource=admin&...
    

    For example, the following connection string specifies test in the database component and includes the authSource=admin option.

    client = Mongo::Client.new('mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority')
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 2.6 or greater
    MongoDB 3.6 Version 2.5 or greater
    MongoDB 3.4 Version 2.4 or greater

    Mongoid Example

    production:
      # Configure available database clients. (required)
      clients:
        # Defines the default client. (required)
        default:
          # Defines the name of the default database that Mongoid can connect to.
          # (required).
          database: 'myDatabaseName'
    
          # Provides the hosts the default client can connect to. Must be an array
          # of host:port pairs. (required)
          hosts:
            - mycluster0-shard-00-00.mongodb.net:27017
            - mycluster0-shard-00-01.mongodb.net:27017
            - mycluster0-shard-00-02.mongodb.net:27017
          options:
            # The name of the user for authentication.
            user: kay
    
            # The password of the user for authentication.
            password: myRealPassword
    
            # The database or source to authenticate the user against. If the database
            # specified above is not admin, admin MUST be specified here.
            auth_source: admin
    
            # All Atlas servers use SSL. (default: false)
            ssl: true
    
    MongoDB Version Minimum ODM Version
    MongoDB 4.0 Version 7.0.0 or greater
    MongoDB 3.6 Version 6.3.0 or greater
    MongoDB 3.4 Version 6.1.0 or greater

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a Scala driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Scala driver and MongoDB, see the MongoDB compatibility matrix.

    val uri: String = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/?ssl=true&authSource=admin&w=majority"
    System.setProperty("org.mongodb.async.type", "netty")
    val client: MongoClient = MongoClient(uri)
    val db: MongoDatabase = client.getDatabase("test")
    
    val uri: String = "mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&w=majority"
    System.setProperty("org.mongodb.async.type", "netty")
    val client: MongoClient = MongoClient(uri)
    val db: MongoDatabase = client.getDatabase("test")
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 2.4 or greater
    MongoDB 3.6 Version 2.2 or greater
    MongoDB 3.4 Version 1.2 or greater

    Note

    To connect to an Atlas M0 (Free Tier) or M2/M5 shared tier cluster, you must use a Go driver version that supports MongoDB 3.4. For complete documentation on compatibility between the Go driver and MongoDB, see the MongoDB compatibility matrix.

    import "go.mongodb.org/mongo-driver/mongo"
    
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    client, err := mongo.Connect(ctx, options.Client().ApplyURI(
       "mongodb+srv://<username>:<password>@<cluster-address>/test?w=majority",
    ))
    if err != nil { log.Fatal(err) }
    
    import "go.mongodb.org/mongo-driver/mongo"
    
    ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
    defer cancel()
    client, err := mongo.Connect(ctx, options.Client().ApplyURI(
       "mongodb://kay:myRealPassword@mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin&serverSelectionTryOnce=false&serverSelectionTimeoutMS=15000&w=majority",
    ))
    if err != nil { log.Fatal(err) }
    
    MongoDB Version Recommended Driver Version(s)
    MongoDB 4.0 Version 1.0.0 or greater
    MongoDB 3.6 Version 1.0.0 or greater
    MongoDB 3.4 Version 1.0.0 or greater

    Troubleshooting

    If you are experiencing issues connecting to your cluster, see Troubleshoot Connection Issues.