Connect your Application to MongoDB

How to connect your application to your MongoDB cluster at ScaleGrid.

Connect Your Application to MongoDB Cluster on ScaleGrid

You can easily connect to your MongoDB cluster from your application using the Connection String provided on your Cluster Details page.

982

Here's where you find your Connection String on the Cluster Details page

The Connection String is built in the following format:

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

📘

For more details on connection strings in MongoDB, check out their official documentation.

The easiest way to build the right Connection String will be to copy the default Connection String provided on the ScaleGrid console and replace the username, password, and database fields with the correct values.

Here is an example of a Connection String for a replica set without SSL enabled. In this example, we use the credentials of user 'testuser' to connect to the database 'test'. The user must have access to the database to be able to connect.

mongodb://testuser:<password>@SG-Prod001-261.devservers.scalegrid.io:27017,SG-Prod001-260.devservers.scalegrid.io:27017/test?replicaSet=RS-Prod001-0

Connecting to SSL Enabled Clusters

ScaleGrid MongoDB SSL enabled clusters can be identified by the SSL icon on the Overview tab of the Cluster Details page:

Our MongoDB clusters are configured with self-signed certificates by default. The SSL CA certificate is available for download on the Overview page.

Most MongoDB drivers will provide an option to specify a CA certificate file. You can download the file from the ScaleGrid console and store it on your application servers. If your driver doesn't provide an option to specify the CA certificate file, attempts to establish SSL connections to a MongoDB server with self signed certificates might fail. In order to fix this, look for an option that lets you disable certificate validation on your driver/platform.

SSL Connection Examples

Follow these ScaleGrid posts for examples on connecting to SSL enabled MongoDB clusters: