MongoDB

Comprehensive guide for connecting to a MongoDB Cluster from your DevBox.

Architecture Diagram

This would be done by setting up the cluster in your Atlas dashboard through one of 3 available cloud providers (AWS, Azure, or GCP) and accessing it through DevBox.

Prerequisites

Before you begin, create your account with MongoDB.

MongoDB only supports Private Endpoint access for Dedicated or Serverless clusters. Private Endpoint access for the Shared cluster is not yet supported.

Existing MongoDB Cluster

Step 1: Creating a Private Endpoint

To connect to a database cluster, you must create a private endpoint with one of MongoDB's three supported cloud providers (AWS, GCP, Azure).

To check and configure the same, follow the below steps:

  1. Go to Security > Network Access and open the Private Endpoint tab.

  2. Click on Add Private Endpoint and choose your cloud provider. For this tutorial, we will be using Azure.

You must provide the billing information in the Edit Payment Method form if you still need to get the payment method configured for your organization.

  1. Click on Next and choose the Region where you want to deploy your endpoint.

  2. Enter your Resource Group Name, Virtual Network Name, Subnet Name, and the name you want to give to your endpoint.

  3. Copy and run this PrivateLink Endpoint Command where your Azure CLI is configured. Remember that you can only copy the command once Atlas finishes creating VNET resources in the background for internal use.

  1. After creating the private endpoint, copy the Private Endpoint Resource ID and Private Endpoint IP Address and paste them into the MongoDB Private Endpoint Dialog box.

  2. Click on Create, and your private endpoint will be created.

Step 2: Installing dependencies in DevBox

We need to install the mongosh shell tool to connect with the cluster.

Follow the below steps to do so:

  1. Install gnupg and curl if they are not already:

sudo apt-get install gnupg curl
  1. Get the MongoDB public GPG key:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor
  1. Create a list file for MongoDB:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt-get update
  1. Install the MongoDB Package:

sudo apt-get install -y mongodb-org
  1. verify if mongosh is installed or not:

mongosh

Step 3: Connecting to the Cluster

To connect to the MongoDB cluster, follow the below steps:

  1. Go to Database > Clusters and select the Cluster you want to access.

  2. Click on Connect and choose the private endpoint option.

  3. Then Click on Shell, copy the connection string, and paste it into your DevBox:

mongosh "mongodb+srv://<cluster-name>.<cluster-id>.mongodb.net/" --apiVersion 1 --username <db_username>
  1. Enter the password when prompted, and you will see the MongoDB cluster is connected.

New MongoDB Cluster

If you need to make a new database cluster and access it through DevBox, then follow the below steps:

Step 1: Creating a Cluster

  1. Go to Database > Clusters and click on Create.

  2. Choose between Dedicated or Serverless database cluster type.

  3. Enter the Instance name, Provider, and Region.

  4. Click on Create Deployment and give it time to deploy the infrastructure.

  5. Go to Security > Network Access and open the Private Endpoint tab.

  6. Click on Add Private Endpoint and choose your cloud provider. For this tutorial, we will be using Azure.

You must provide the billing information in the Edit Payment Method form if you don't have a payment method already configured for your organization.

  1. Click on Next and choose the Region where you want to deploy your endpoint.

  2. Enter your Resource Group Name, Virtual Network Name, Subnet Name, and the name you want to give to your endpoint.

  3. Copy and run this PrivateLink Endpoint Command where your Azure CLI is configured. Remember that you can only copy the command once Atlas finishes creating VNET resources in the background for internal use.

  1. Copy the Private Endpoint Resource ID and Private Endpoint IP Address and paste them into the MongoDB Private Endpoint Dialog box after creating the private endpoint.

  2. Click on Create, and your private endpoint will be created.

Step 2: Installing MongoDB Shell tool in DevBox

To connect with the cluster, we need to install the mongosh shell tool.

Follow the below steps to do so:

  1. Install gnupg and curl if they are not already:

sudo apt-get install gnupg curl
  1. Get the MongoDB public GPG key:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
   --dearmor
  1. Create a list file for MongoDB:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
  1. Install the MongoDB Package:

sudo apt-get install -y mongodb-org
  1. verify if mongosh is installed or not:

mongosh

Step 3: Connecting with the Cluster

To connect to the MongoDB cluster, follow the below steps:

  1. Go to Database > Clusters and select the cluster you want to connect to.

  2. Click on Connect and choose the private endpoint option.

  3. Then Click on Shell, copy the connection string, and paste it into your DevBox CLI:

mongosh "mongodb+srv://<cluster-name>.<cluster-id>.mongodb.net/" --apiVersion 1 --username <db_username>
  1. Enter the password when prompted, and you will see the mongosh shell if the connection is authenticated.

Last updated