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:
Go to Security > Network Access and open the Private Endpoint tab.
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.
Click on Next and choose the Region where you want to deploy your endpoint.
Enter your Resource Group Name, Virtual Network Name, Subnet Name, and the name you want to give to your endpoint.
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.
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.
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.
You can skip this step if you prefer to use password-based authentication. We are using an X.509 certificate authentication to add an extra layer of security because a shared type cluster allows all internet traffic, which also requires code changes.
To add a layer of security, we will authenticate it with an X.509 certificate. You may follow the below steps to do so:
Go to Database Access and click on Add new database user.
Select Certificate and enter the username.
Check the Download certificate when the user is added and then choose the certificate expiration duration.
Click on Add user and save the certificate to your DevBox.
Step 4: Connecting with the Cluster
To connect to the MongoDB cluster, follow the below steps:
Choose between Dedicated or Serverless database cluster type.
Enter the Instance name, Provider, and Region.
Click on Create Deployment and give it time to deploy the infrastructure.
Go to Security > Network Access and open the Private Endpoint tab.
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.
Click on Next and choose the Region where you want to deploy your endpoint.
Enter your Resource Group Name, Virtual Network Name, Subnet Name, and the name you want to give to your endpoint.
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.
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.
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.
You can skip this step if you prefer to use password-based authentication. We are using an X.509 certificate authentication to add an extra layer of security because a shared type cluster allows all internet traffic, which also requires code changes.
To add a layer of security, we will authenticate it with an X.509 certificate. You may follow the below steps to do so:
Go to Database Access and click on Add new database user.
Select Certificate and enter the username.
Check the Download certificate when the user is added and then choose the certificate expiration duration.
Click on Add user and save the certificate to your DevBox.
Step 4: Connecting with the Shared Cluster
To connect to the MongoDB cluster, follow the below steps: