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.

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 cluster.

Whitelist
To access a cluster, you must connect from an IP address on the Atlas group’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.

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

From the Connect dialog, review the IP Whitelist.

Atlas only allows client connections to the cluster from entries in the group’s whitelist.

Atlas displays any entries already on the group whitelist in the Connect modal under Check the IP Whitelist. If you do not see the IP address of the machine hosting your application on the list, you can:

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

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

  • Click Add Current IP Address to add your current IP address. This only grants your application access to the Atlas cluster if the machine you are using to access the Atlas UI is also hosting your application.

  • Click Allow Access From Anywhere to allow access from any IP address.

    Warning

    Using Allow Access From Anywhere results in all clusters in the Atlas group being publically accessible. Consider the security risks of having public-facing MongoDB clusters before using this option.

3

Select Connect Your Application.

The Connect dialog displays the URI connection string for connecting to the Atlas cluster with a MongoDB driver.

The URI connection string uses a MongoDB user set up for the group as the user to authenticate as. Update the <PASSWORD> placeholder with the password for this user.

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; i.e. %40bc123.

To connect with a different MongoDB user, update the USERNAME and PASSWORD components of the URI connection string with the username and password of a different MongoDB user.

mongodb://USERNAME:PASSWORD@...

You must also replace the <DATABASE> placeholder in the URI connection string with the database you want your application to connect to.

The following examples use the URI connection string to authenticate and connect to an Atlas cluster. In the examples, PASSWORD has been replaced with the password (myRealPassword) for the user kay.

C Driver Example

client = mongoc_client_new ("mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin");
db = mongoc_client_get_database (client, "test");

Minimum Driver Version(s)

Version 1.3

To connect to an Atlas M0 (Free 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.

C++ Driver Example

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

//...

mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin"}};
mongocxx::database db = conn["test"];

Minimum Driver Version(s)

To connect to an Atlas M0 (Free 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.

C#/.Net Driver Example

var client = new MongoClient("mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin");
var database = client.GetDatabase("test");

Minimum Driver Version(s)

Important

The .NET Core library does not support the SNI TLS extension on Linux and OSX. Applications using .NET Core on these operating systems cannot connect to a Atlas Free Tier cluster.

The issue is tracked on the dotnet/corefx github page.

To connect to an Atlas M0 (Free 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.

Java Driver Example

MongoClientURI uri = new MongoClientURI(
   "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin");

MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("test");

Minimum Driver Version(s)

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.

Node.js Driver Example

var MongoClient = require('mongodb').MongoClient;

var uri = "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uri, function(err, db) {
  db.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-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uriTestDb, function(err, db) {
   db.close();
});

Minimum Driver Version(s)

Version 2.1.0

To connect to an Atlas M0 (Free 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.

Perl Driver Example

my $client = MongoDB->connect("mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin");
my $db = $client->get_database( 'test' );

Minimum Driver Version(s)

Version 1.2.x

To connect to an Atlas M0 (Free 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.

PHP Driver Example

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://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin');

$db = $client->test;

Minimum Driver Version(s)

PHP library 1.0+

Version mongodb-1.1

To connect to an Atlas M0 (Free 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.

Python Driver Example

client = pymongo.MongoClient("mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin")
db = client.test

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.

As such, after you have 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 certificates bundle in order to connect 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 are not affected.

Minimum Driver Version(s)

Version 3.2

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.

Ruby Driver Example

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

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-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/test?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin')

Minimum Driver Version(s)

Version 2.2

To connect to an Atlas M0 (Free 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.

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-wpeiv.mongodb.net:27017
        - mycluster0-shard-00-01-wpeiv.mongodb.net:27017
        - mycluster0-shard-00-02-wpeiv.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

Scala Driver Example

val uri: String = "mongodb://kay:myRealPassword@mycluster0-shard-00-00-wpeiv.mongodb.net:27017,mycluster0-shard-00-01-wpeiv.mongodb.net:27017,mycluster0-shard-00-02-wpeiv.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin"
System.setProperty("org.mongodb.async.type", "netty")
val client: MongoClient = MongoClient(uri)
val db: MongoDatabase = client.getDatabase("test")

Minimum Driver Version(s)

Version 1.1

To connect to an Atlas M0 (Free 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.