In this article, I am going to walk you through exactly how to start the MongoDB server across different operating systems. I will focus on the technical reality of getting mongod up and running so you can get back to building.
How to Start MongoDB Server
Understanding the Core Components: mongod vs. mongosh
There are two primary, distinct binaries you need to be aware of:
mongod(The Daemon): This is the server process. It is the heart of the operation. It manages data access, background tasks, and listens for requests. If this isn’t running, nothing works.mongosh(The Shell): This is the client. Previously known simply asmongo(legacy), the modern version ismongosh. You use this to send commands to the server.
Note: In this tutorial, our sole focus is on the first one: getting mongod running successfully.
Pre-Flight Checks: The Data Directory
If you try to run MongoDB without preparing the environment, it will fail immediately. By default, MongoDB has a very specific expectation of where it should store your data.
I always tell my team: “Respect the defaults, or document your deviations.”
The Default dbpath
Unless you tell it otherwise, MongoDB will look for a folder at /data/db (on Unix-based systems) or C:\data\db (on Windows). If this folder does not exist, or if the user account running the server doesn’t have write permissions to it, the server will crash upon startup.
Before you proceed, ensure you have:
- Created the data directory.
- Assigned the correct read/write permissions to your current user.
Method 1: Starting MongoDB on macOS
As a developer primarily using a Mac in my daily workflow, I find macOS offers two distinct ways to handle this. One is for the “I need it now for 5 minutes” scenario, and the other is for “I am working on this project for the next six months.”
Option A: The “Foreground” Method (Manual)
This is great for debugging. You see the logs stream right in your terminal.
- Open your Terminal.
- Run the command:Bash
mongod --config /usr/local/etc/mongod.confNote: If you are on an Apple Silicon (M1/M2/M3) Mac using Homebrew, your config path might be/opt/homebrew/etc/mongod.conf. - Analyze the Output: You should see a stream of JSON-styled logs. Look for the line that says:”Waiting for connections on port 27017″If you see this, you are golden. Do not close this terminal window; if you do, the server dies.
Option B: The “Background Service” Method (Homebrew)
If you are like me and hate having six terminal tabs open just to keep servers alive, you should run it as a service using Homebrew.
- Start the service:Bash
brew services start mongodb-community(Note: Depending on the version installed, you might need to specify the version, e.g.,mongodb-community@7.0) - Verify it is running:Bash
brew services listYou should seemongodb-communitywith a status ofstartedand a green color code. - Stop the service:When you are done, don’t just leave it running and eating up RAM.Bash
brew services stop mongodb-community
Method 2: Starting MongoDB on Windows
Windows handles background processes differently, utilizing the “Services” management console. However, we can still run it manually if we need to inspect the logs.
Option A: The Command Prompt Method
- Open Command Prompt or PowerShell as an Administrator.
- Navigate to your MongoDB bin folder. By default, this is usually:C:\Program Files\MongoDB\Server\X.X\bin\
- Execute the daemon:PowerShell
.\mongod.exe --dbpath="C:\data\db" - Important: On Windows, I have found that specifying the
--dbpathexplicitly is often safer than relying on the default, which sometimes defaults to the root of the C drive where permissions are tricky.
Option B: The Windows Service Method
This is the production-ready way to do it. When you installed MongoDB via the MSI installer, you likely checked the box that said “Install as a Service”.
- Press
Win + R, typeservices.msc, and hit Enter. - Scroll down until you find MongoDB Server (MongoDB).
- Check the Status:
- If it says “Running”, you are already done.
- If it is blank, right-click the service and select Start.
Alternatively, if you prefer the command line (and I certainly do), you can use the net command in an Admin terminal:
PowerShell
net start MongoDB
To stop it later:
PowerShell
net stop MongoDB
Method 3: Starting MongoDB on Linux (Ubuntu/Debian)
In the Linux world, particularly on servers we run in data centers across the US, systemd is the king of process management. While you can run mongod directly, I strongly advise against it for anything other than a quick test.
Using systemctl (Recommended)
- Start the Server:Bash
sudo systemctl start mongod - Check the Status:This is the step most people skip, but you shouldn’t.Bash
sudo systemctl status mongodYou are looking for the textactive (running)in green. - Enable on Boot:If you want MongoDB to start automatically when your Linux server reboots:Bash
sudo systemctl enable mongod
Troubleshooting Linux Startups
If systemctl fails, I usually check the permissions on the data directory first:
Bash
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
Permission errors are responsible for 90% of Linux startup failures I have diagnosed.
Configuration Files: The Professional Approach
Running mongod with a bunch of flags like --dbpath, --port, and --logpath gets messy fast. In a professional environment, we use a configuration file.
The config file is written in YAML format. It is indentation-sensitive, so be careful with your tabs and spaces.
Typical Locations:
- Linux:
/etc/mongod.conf - Mac (Intel):
/usr/local/etc/mongod.conf - Mac (Apple Silicon):
/opt/homebrew/etc/mongod.conf - Windows:
<install directory>\bin\mongod.cfg
Sample Configuration Structure
Here is a breakdown of the essential settings I configure in every mongod.conf file:
YAML
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: 127.0.0.1
To start the server using this file, simply pass the --config flag:
Bash
mongod --config /etc/mongod.conf
Verifying Your Server is “Healthy”
Just because the process is running doesn’t mean it is healthy. I always perform a “handshake” to ensure the server is actually accepting connections.
The mongosh Test
- Open a new terminal window.
- Type
mongosh. - If the prompt changes to something like
test>, you are connected.
The netstat Verification
If you don’t have the shell installed, you can check if the port is open using network tools.
| OS | Command | Expected Output |
| macOS/Linux | lsof -i :27017 | You should see mongod listed under COMMAND. |
| Windows | `netstat -an | find “27017”` |
Troubleshooting: When It Won’t Start
In my years of consulting, I have seen MongoDB fail to start for dozens of reasons. However, three culprits usually account for the vast majority of issues.
1. The Zombie Lock File
If your server crashed (maybe a power outage or a forced reboot), MongoDB might have left behind a mongod.lock file in your data directory. This file prevents a new instance from starting to avoid data corruption.
The Fix:
- Check the logs to confirm the error relates to a lock file.
- Do not just delete the file immediately.
- Attempt to run a repair command (use with caution):Bash
mongod --repair - If that succeeds, try starting the server normally.
2. The “Address Already in Use” Error
This means something else is running on port 27017. Usually, it is a “ghost” instance of MongoDB that you thought you closed but didn’t.
The Fix:
- Mac/Linux:
killall mongod - Windows: Open Task Manager, find
mongod.exe, and End Task.
3. Exit Code 100
This is a generic error that usually points to permissions.
- Diagnosis: Did you create
/data/dbusingsudo? If so, your regular user account cannot write to it. - The Fix: change the ownership of the directory to your current user using
chown.
Security Best Practices for Starting the Server
I cannot finish this guide without mentioning security. By default, MongoDB usually binds to 127.0.0.1 (localhost). This is good. It means only your computer can access the database.
However, I often see tutorials telling you to bind to 0.0.0.0 to “fix connection issues.” Do not do this unless you have enabled Authentication.
If you must expose the server to the network:
- Edit your
mongod.conf. - Set
security.authorization: enabled. - Create an admin user before you restart the server with the new network binding.
Starting a MongoDB server without authentication exposed to the internet is essentially gifting your data to the public. Don’t be that developer.
Summary
Starting a MongoDB server isn’t just about typing a command; it is about understanding the environment your database lives in. Whether you are using systemctl on a Linux production rig or brew services on your MacBook Pro in a coffee shop in Seattle, the underlying principles remain the same:
- Daemon vs. Shell: Know the difference.
- Permissions: Respect the data directory ownership.
- Logs: Read them; they tell you exactly what is wrong.
- Config Files: Use them for reproducibility.
Once you have mastered these startup routines, you stop fighting with your tools and start working with them.
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.