Installing MongoDB Locally and via Atlas (Cloud)


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

  1. Visit the official MongoDB website:
    MongoDB Download Center
  2. Select your operating system (Windows, macOS, or Linux).
  3. Choose the version of MongoDB you want to install (select the latest stable version).
  4. Click on the Download button. The installer will be downloaded to your machine.

Step 2: Installing MongoDB

On Windows:
  1. Once the installer is downloaded, double-click to launch it.
  2. During the installation process, select Complete for the installation type.
  3. Make sure the option “Install MongoDB as a Service” is selected. This will allow MongoDB to run automatically as a background service after installation.
  4. Choose the installation directory (you can use the default location).
  5. After installation, click Finish.
On macOS:
  1. 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)"
  2. To install MongoDB using Homebrew, run the following command in your terminal: bashCopyEditbrew tap mongodb/brew brew install [email protected]
  3. After installation, start MongoDB using the following command: bashCopyEditbrew services start mongodb/brew/mongodb-community
On Linux:
  1. Open your terminal and update your package index: bashCopyEditsudo apt-get update
  2. Install MongoDB: bashCopyEditsudo apt-get install -y mongodb
  3. Start MongoDB using the following command: bashCopyEditsudo systemctl start mongodb
  4. To ensure MongoDB starts on boot, enable it: bashCopyEditsudo systemctl enable mongodb

Step 3: Verify the Installation

  1. Open a command prompt (Windows) or terminal (macOS/Linux).
  2. Type the following command to enter the Mongo shell: bashCopyEditmongo
  3. If MongoDB is successfully installed and running, you should see the Mongo shell prompt, which looks like this: bashCopyEdit>
  4. To check the current MongoDB version, run: bashCopyEditdb.version()

2. Installing MongoDB via Atlas (Cloud)

Step 1: Create a MongoDB Atlas Account

  1. Visit MongoDB Atlas.
  2. Click Get Started Free to create an account. You can sign up using your Google account or an email address.
  3. Once your account is created, log in to the MongoDB Atlas dashboard.

Step 2: Create a Cluster

  1. After logging in, click on the “Build a Cluster” button.
  2. Select the Cloud Provider and Region closest to your application’s location (e.g., AWS, Google Cloud, or Azure).
  3. Choose the Cluster Tier. MongoDB Atlas offers a free-tier cluster (M0), which is ideal for development and testing purposes.
  4. Click Create Cluster. This may take a few minutes.

Step 3: Set Up Network Access

  1. After your cluster is created, navigate to the Network Access tab in the MongoDB Atlas dashboard.
  2. Under the IP Whitelist section, click Add IP Address.
  3. 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).
  4. Click Confirm to allow network access.

Step 4: Create a Database User

  1. Go to the Database Access tab in the MongoDB Atlas dashboard.
  2. Click Add New Database User.
  3. Choose a username and password for your database user.
  4. Assign Database User Privileges as necessary (for general use, the default privileges are usually sufficient).
  5. Click Add User.

Step 5: Connect to Your Cluster

  1. Go to the Clusters tab in the MongoDB Atlas dashboard.
  2. Click Connect for your newly created cluster.
  3. Select Connect your application.
  4. Copy the connection string provided by MongoDB Atlas.
    • The connection string will look like this: bashCopyEditmongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority
  5. Replace <username> and <password> with your MongoDB Atlas database user credentials.
  6. 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.

  1. First, ensure that the Mongo shell is installed on your machine. If not, you can download it from the MongoDB Downloads page.
  2. Once installed, run the following command to connect to your MongoDB Atlas cluster: bashCopyEditmongo "mongodb+srv://<username>:<password>@cluster0.mongodb.net/test?retryWrites=true&w=majority"
  3. 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.