Setting Up a MongoDB Atlas Deployment
MongoDB Atlas allows you to create a cloud-hosted MongoDB deployment without installing the database server on your computer.
Step 1: Create an Atlas account
Visit https://www.mongodb.com/products/platform/atlas-database and follow the account creation instructions.
After signing in, create or select a project for your learning environment.
Step 2: Create a database deployment
Use the Atlas interface to create a deployment. Available deployment types, regions, and pricing depend on the current Atlas offerings and your account.
Choose a suitable region and deployment size for your learning workload. Do not assume that a particular tier or feature will always be free or available in every region.
Step 3: Create a database user
Create a database user with a username and strong password.
A database user is distinct from your Atlas website login. The database user is used to authenticate database connections.
For learning, grant only the privileges needed for your exercises. Avoid using administrative credentials in a public-facing application.
Step 4: Configure network access
Configure the deployment's network access rules to allow connections from your development environment.
If you connect from your computer, use your current public IP address when appropriate. If your network changes, the permitted address may also need updating.
Avoid allowing access from every IP address as a permanent production configuration. Network restrictions should be combined with authentication and appropriate permissions.
Step 5: Obtain a connection string
Open the deployment's connection instructions and choose the appropriate connection method.
A typical SRV connection string has this general form:
mongodb+srv://<username>:<password>@<cluster-host>/<database>?retryWrites=true&w=majority
This is a template, not a working connection string. Replace the placeholders with the values supplied by Atlas.
The SRV format uses DNS records to discover the servers associated with the deployment. It is commonly used with Atlas.
Step 6: Connect with mongosh
If mongosh is installed, you can connect using the connection string supplied by Atlas:
mongosh "mongodb+srv://<cluster-host>/learning"
The command above is illustrative. Supply your actual hostname and follow the authentication instructions for your deployment. Do not put a real password in a command that may be saved in shell history or exposed in process listings.
You can also use the Atlas-provided connection command and enter credentials when prompted, where supported.
Step 7: Verify the connection
After connecting, run:
db.runCommand({ ping: 1 });
A successful response includes ok: 1.
Then select a practice database:
use mongodb_course_lab
Insert a document to verify write access:
db.setupCheck.insertOne({
message: "MongoDB connection verified",
checkedAt: new Date()
});
If the insert fails, the connection may still be valid but the database user may not have the required write privileges.
Never commit a connection string containing a real password to GitHub, expose it through a NEXT_PUBLIC_ environment variable, or embed it in browser-side JavaScript.
db.runCommand({ ping: 1 });
use mongodb_course_lab
db.setupCheck.insertOne({
message: "MongoDB connection verified",
checkedAt: new Date()
});
db.setupCheck.find();- An Atlas account and a MongoDB database user serve different purposes.
- Network access rules control which client networks can reach the deployment.
- Authentication and authorization are separate from network access.
- A successful ping verifies connectivity but not every required permission.
- Connection strings should be stored securely.
What does a successful `{ ping: 1 }` command establish?