Installing and Running MongoDB
Learn how to properly install and run MongoDB on any system — whether you're using Windows, macOS, or Linux — and how to fix common setup errors like mongod: command not found that can stop your app from connecting to the database.
Introduction: Why MongoDB Installation Matters
Every Node.js developer working with Mongoose eventually encounters a frustrating moment: the database won’t connect, and an error like mongod: command not found appears.
This isn’t a code bug — it’s an environment issue. MongoDB might not be installed, or your system might not know where to find it. This lesson walks through how to install, verify, and run MongoDB correctly on all major operating systems. You’ll also learn how to fix the most common installation issues that developers around the world face.
1. What Is MongoDB and Why You Need It
MongoDB is a popular NoSQL database used by millions of developers and businesses for applications that need scalability, speed, and flexibility. Tools like Mongoose make it easier to work with MongoDB in Node.js by providing schema-based modeling.
But before your app can store or retrieve data, MongoDB must be installed and running on your system — or hosted remotely on a cloud provider like MongoDB Atlas.
2. Installing MongoDB on Linux (Ubuntu/Debian)
Linux users can install MongoDB easily using the APT package manager. Follow these steps:
# Step 1: Import the public key
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
# Step 2: Add the repository
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
# Step 3: Update and install MongoDB
sudo apt update
sudo apt install -y mongodb-org
# Step 4: Start the MongoDB service
sudo systemctl start mongod
sudo systemctl enable mongod
After installation, verify MongoDB is working:
mongod --version
If this command returns the version number, MongoDB is installed successfully.
3. Installing MongoDB on macOS
The easiest way to install MongoDB on macOS is through Homebrew:
# Step 1: Tap the MongoDB Homebrew tap
brew tap mongodb/brew
# Step 2: Install MongoDB Community Edition
brew install mongodb-community@7.0
# Step 3: Start MongoDB
brew services start mongodb/brew/mongodb-community
Then confirm installation with:
mongod --version
If you see a version output, MongoDB is correctly set up. You can also run it manually using:
mongod --config /usr/local/etc/mongod.conf
4. Installing MongoDB on Windows
On Windows, MongoDB provides an MSI installer for an easy setup:
- Download the MongoDB Community Edition MSI from the official MongoDB website.
- Run the installer and select the “Complete” installation option.
- Make sure to check “Install MongoDB as a Service.”
- Once installed, start MongoDB from the Services panel or using PowerShell:
net start MongoDB
To verify it’s working, open a terminal and run:
mongod --version
5. Fixing the “mongod: command not found” Error
If you see this error after installation, it means your system can’t locate MongoDB in your PATH. You can fix this by adding MongoDB’s binary path manually:
- On Linux: Add this to
~/.bashrcor~/.zshrc:export PATH=/usr/bin/mongodb/bin:$PATH - On macOS (Homebrew):
export PATH="/usr/local/opt/mongodb-community@7.0/bin:$PATH" - On Windows: Add the MongoDB
bindirectory to your System Environment Variables.
Restart your terminal and test again with:
mongod --version
If it works, your PATH is configured correctly.
6. Starting and Stopping MongoDB
Once installed, you need to ensure MongoDB runs whenever your system starts. Here’s how to manage it:
- Linux:
sudo systemctl start mongodorsudo systemctl stop mongod - macOS:
brew services start mongodb-community - Windows:
net start MongoDBornet stop MongoDB
To check if MongoDB is running, use:
mongo
If the Mongo shell opens successfully, your database is up and running.
7. Real-Life Business and Developer Scenarios
Many startups and developers face MongoDB setup issues that block critical project timelines. For instance:
- Case 1: A new SaaS team kept seeing “Mongoose connection timed out” errors because MongoDB wasn’t running. A quick
systemctl status mongodcheck revealed it had crashed on reboot. - Case 2: A freelancer working remotely found
mongodwasn’t recognized. The fix? Adding MongoDB’s binary path to their shell profile. - Case 3: An e-commerce project in Docker worked locally but not on the server — the developer hadn’t mapped the MongoDB port correctly. Installing and starting MongoDB manually solved the issue.
These examples show how mastering MongoDB installation and runtime management can prevent costly delays in production.
8. Troubleshooting Checklist
- Run
mongod --versionto confirm MongoDB installation. - If not found, add MongoDB’s binary directory to your PATH.
- Ensure MongoDB service is running with
systemctl status mongodorbrew services list. - Check your logs for errors at
/var/log/mongodb/mongod.log. - Verify your firewall isn’t blocking the default port
27017.
