Starting and stopping a MongoDB server
Starting and stopping a MongoDB server involves managing the mongodb
process, which runs the MongoDB database server. Depending on the operating system, you can start, stop, and restart the MongoDB service through the command line or system service managers.
1. Starting MongoDB Server
Linux (Ubuntu/CentOS) Using systemctl
If MongoDB is installed as a service, you can manage it using the systemctl
command.
Start MongoDB:
sudo systemctl start mongod
Enable MongoDB to Start on Boot: This ensures MongoDB starts automatically when the system boots.
sudo systemctl enable mongod
Check MongoDB Status: You can verify whether MongoDB is running.
sudo systemctl status mongod
Linux (Manual Method)
If MongoDB is not installed as a service, you can manually start it using the mongod
command:
Start MongoDB with Default Configurations:
mongod
Start MongoDB with a Specific Configuration File:
mongod --config /etc/mongod.conf
Start MongoDB with a Specific Data Directory:
mongod --dbpath /path/to/data/db
The MongoDB server will run in the foreground by default unless you use the --fork
option.
macOS Using brew
(Homebrew)
If you installed MongoDB via Homebrew, you can manage it using the brew services
command.
Start MongoDB:
brew services start mongodb-community
Stop MongoDB:
brew services stop mongodb-community
Restart MongoDB:
brew services restart mongodb-community
Windows Using the Command Prompt
If MongoDB is installed as a service on Windows, you can manage it via the Services console or sc
command:
Start MongoDB from Command Prompt:
net start MongoDB
Stop MongoDB from Command Prompt:
net stop MongoDB
Alternatively, you can start MongoDB manually using the mongod
command:
- Start MongoDB Manually:
"C:\Program Files\MongoDB\Server\<version>\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\<version>\mongod.cfg"
2. Stopping MongoDB Server
Linux (Ubuntu/CentOS) Using systemctl
Stop MongoDB:
sudo systemctl stop mongod
Disable MongoDB from Starting on Boot: This prevents MongoDB from starting automatically on system boot.
sudo systemctl disable mongod
Linux (Manual Method)
If MongoDB is running manually, you can stop the server by finding and terminating the mongod
process.
Find the MongoDB process ID (PID):
ps aux | grep mongod
Kill the process:
sudo kill <pid>
macOS Using brew
- Stop MongoDB:
brew services stop mongodb-community
Windows Using the Command Prompt
If MongoDB is running as a Windows service, you can stop it with the following command:
- Stop MongoDB:
net stop MongoDB
Alternatively, if MongoDB is running manually, you can stop it by closing the Command Prompt window or by using Ctrl+C
if it was started in the foreground.
3. Restarting MongoDB Server
Linux (Ubuntu/CentOS) Using systemctl
- Restart MongoDB:
sudo systemctl restart mongod
macOS Using brew
- Restart MongoDB:
brew services restart mongodb-community
Windows
If MongoDB is running as a Windows service:
- Restart MongoDB:
net stop MongoDB net start MongoDB
4. Checking MongoDB Logs
To troubleshoot MongoDB issues, you may want to check the logs. The log files are typically located at:
- Linux:
/var/log/mongodb/mongod.log
- Windows: Defined in the configuration file (typically
C:\Program Files\MongoDB\Server\<version>\log\mongod.log
).
Use the following command to view logs in Linux:
tail -f /var/log/mongodb/mongod.log