Introduction
In this module, we’ll guide you through the process of setting up MongoDB both locally on your machine and in the cloud via MongoDB Atlas. MongoDB Atlas is a fully managed cloud service provided by MongoDB, making it easier to set up, scale, and manage your databases without the complexity of manual configuration.
By the end of this module, you will:
- Understand the steps involved in installing MongoDB locally.
- Learn how to create and configure a MongoDB cluster using MongoDB Atlas.
1. Installing MongoDB Locally
Step 1: Download MongoDB
- Visit the official MongoDB website:
MongoDB Download Center - Select your operating system (Windows, macOS, or Linux).
- Choose the version of MongoDB you want to install (select the latest stable version).
- Click on the Download button. The installer will be downloaded to your machine.
Step 2: Installing MongoDB
On Windows:
- Once the installer is downloaded, double-click to launch it.
- During the installation process, select Complete for the installation type.
- Make sure the option “Install MongoDB as a Service” is selected. This will allow MongoDB to run automatically as a background service after installation.
- Choose the installation directory (you can use the default location).
- After installation, click Finish.
On macOS:
- Homebrew is the easiest way to install MongoDB on macOS. If you don’t have Homebrew installed, you can install it by running the following command in the terminal: bashCopyEdit
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- To install MongoDB using Homebrew, run the following command in your terminal: bashCopyEdit
brew tap mongodb/brew brew install [email protected]
- After installation, start MongoDB using the following command: bashCopyEdit
brew services start mongodb/brew/mongodb-community
On Linux:
- Open your terminal and update your package index: bashCopyEdit
sudo apt-get update
- Install MongoDB: bashCopyEdit
sudo apt-get install -y mongodb
- Start MongoDB using the following command: bashCopyEdit
sudo systemctl start mongodb
- To ensure MongoDB starts on boot, enable it: bashCopyEdit
sudo systemctl enable mongodb
Step 3: Verify the Installation
- Open a command prompt (Windows) or terminal (macOS/Linux).
- Type the following command to enter the Mongo shell: bashCopyEdit
mongo
- If MongoDB is successfully installed and running, you should see the Mongo shell prompt, which looks like this: bashCopyEdit
>
- To check the current MongoDB version, run: bashCopyEdit
db.version()
2. Installing MongoDB via Atlas (Cloud)
Step 1: Create a MongoDB Atlas Account
- Visit MongoDB Atlas.
- Click Get Started Free to create an account. You can sign up using your Google account or an email address.
- Once your account is created, log in to the MongoDB Atlas dashboard.
Step 2: Create a Cluster
- After logging in, click on the “Build a Cluster” button.
- Select the Cloud Provider and Region closest to your application’s location (e.g., AWS, Google Cloud, or Azure).
- Choose the Cluster Tier. MongoDB Atlas offers a free-tier cluster (M0), which is ideal for development and testing purposes.
- Click Create Cluster. This may take a few minutes.
Step 3: Set Up Network Access
- After your cluster is created, navigate to the Network Access tab in the MongoDB Atlas dashboard.
- Under the IP Whitelist section, click Add IP Address.
- Add your current IP address (you can use 0.0.0.0/0 to allow all IP addresses, but this is not recommended for production environments).
- Click Confirm to allow network access.
Step 4: Create a Database User
- Go to the Database Access tab in the MongoDB Atlas dashboard.
- Click Add New Database User.
- Choose a username and password for your database user.
- Assign Database User Privileges as necessary (for general use, the default privileges are usually sufficient).
- Click Add User.
Step 5: Connect to Your Cluster
- Go to the Clusters tab in the MongoDB Atlas dashboard.
- Click Connect for your newly created cluster.
- Select Connect your application.
- Copy the connection string provided by MongoDB Atlas.
- The connection string will look like this: bashCopyEdit
mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority
- The connection string will look like this: bashCopyEdit
- Replace
<username>
and<password>
with your MongoDB Atlas database user credentials. - You can now use this connection string to connect to MongoDB from your application (Node.js, Java, etc.) or MongoDB shell.
3. Using MongoDB Atlas with Mongo Shell
If you want to interact with your MongoDB Atlas database from the command line, you can use the Mongo shell.
- First, ensure that the Mongo shell is installed on your machine. If not, you can download it from the MongoDB Downloads page.
- Once installed, run the following command to connect to your MongoDB Atlas cluster: bashCopyEdit
mongo "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority"
- After connecting, you can start running MongoDB commands just like you would with a local installation.
Conclusion
In this module, we’ve covered the steps to install MongoDB both locally and in the cloud using MongoDB Atlas. Local installation provides the flexibility of running MongoDB on your own machine, while MongoDB Atlas offers the advantages of a fully-managed, scalable cloud-based database solution.