w3resource

Install MongoDB Community Edition on macOS Step-by-Step

Install MongoDB Community Edition on macOS

MongoDB is a widely-used NoSQL database designed for high performance and scalability. Installing MongoDB Community Edition on macOS is a streamlined process involving Homebrew or direct installation. This guide provides a step-by-step walkthrough, from downloading to verifying the setup, along with troubleshooting tips and beginner commands.

Step-by-Step Installation Guide

1. Pre-Installation Checklist

Before proceeding with the installation, ensure the following:

  • Supported macOS Versions: macOS 10.14 (Mojave) and later.
  • Hardware Requirements: Minimum 2GB RAM (4GB recommended).

2. Install MongoDB Community Edition using Homebrew

a) Update Homebrew

Open Terminal and run:

brew update

b) Install MongoDB Community Edition

Run the following command to install MongoDB:

brew tap mongodb/brew
brew install [email protected]

c) Start MongoDB

Launch the MongoDB service:

brew services start mongodb/brew/mongodb-community

d) Verify Installation?

Check the MongoDB version to ensure it was installed correctly:

mongo --version

3. Configure MongoDB Environment

a) Default Data Directory

MongoDB stores data in /usr/local/var/mongodb by default. Ensure this directory exists:

mkdir -p /usr/local/var/mongodb

b) Log Directory

MongoDB logs are stored in /usr/local/var/log/mongodb. Confirm its existence:

mkdir -p /usr/local/var/log/mongodb

c) Test MongoDB Server

Run the mongod process manually to test the server:

mongod --dbpath /usr/local/var/mongodb

4. Install and Use MongoDB Compass

a) Download Compass

b) Install Compass

  • Open the downloaded .dmg file and drag MongoDB Compass to the Applications folder.

c) Connect Compass to MongoDB

Launch Compass and connect to localhost:27017.

5. Starting MongoDB

a) Run MongoDB Service

  • Start MongoDB using Homebrew:
brew services start mongodb/brew/mongodb-community

b) Access MongoDB Shell

  • Open a new Terminal window and run:
mongo

c) Verify Functionality

  • Insert and query test data:

Code:

db.test.insertOne({ name: "Test", status: "Success" })
db.test.find()

Troubleshooting Installation Issues

1. Error: MongoDB not found

  • Ensure Homebrew is installed and updated.
  • Verify that brew tap mongodb/brew was run.

2. Server fails to start

  • Check permissions for /usr/local/var/mongodb and /usr/local/var/log/mongodb.
  • Run the mongod command with sudo if necessary.

3. Compass fails to connect

  • Confirm the MongoDB server is running on localhost:27017.

Uninstalling MongoDB

1. Stop the MongoDB Service

  • Run the following command:
brew services stop mongodb/brew/mongodb-community

2. Uninstall MongoDB

  • Use Homebrew to remove MongoDB:
brew uninstall mongodb-community

3. Clean Remaining Files

  • Delete data and log directories:
rm -rf /usr/local/var/mongodb
rm -rf /usr/local/var/log/mongodb

Additional Commands for Beginners

Code:


# Insert multiple documents
db.users.insertMany([
  { name: "Connor", age: 28 },
  { name: "Thera", age: 35 }
])

# Find all documents
db.users.find()

# Update a document
db.users.updateOne({ name:  “Connor" }, { $set: { age: 29 } })

# Delete a document
db.users.deleteOne({ name: "Connor" })


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mongodb/installation-mongodb-macos.php