Case with me:
- I installed mongodb from official site.
- Worked fine after installation, even worked with command-line, Robo3T and Compass.
- Problems occurred after rebooting once.
So, I checked the status
sudo systemctl status mongod
and got this:-
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2021-02-20 18:11:19 IST; 5s ago
Docs: https://docs.mongodb.org/manual
Process: 724411 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=1/FAILURE)
Main PID: 724411 (code=exited, status=1/FAILURE)
Feb 20 18:11:18 bunty systemd[1]: Started MongoDB Database Server.
Feb 20 18:11:19 bunty mongod[724411]: {"t":{"$date":"2021-02-20T12:41:19.657Z"},"s":"F", "c":"CONTROL", "id":>
Feb 20 18:11:19 bunty systemd[1]: mongod.service: Main process exited, code=exited, status=1/FAILURE
Feb 20 18:11:19 bunty systemd[1]: mongod.service: Failed with result 'exit-code'.
If your case is similar...
How I solved it?
- Check if these two directories exists or not:
/var/lib/mongodb
and /var/log/mongodb
.
You can do this with:
test -d /var/lib/mongodb && echo "Directory Exists"
test -d /var/log/mongodb && echo "Directory Exists"
- The one which does not exist, make it with
mkdir
command.
sudo mkdir /var/lib/mongodb # run only if it does not exist
sudo mkdir /var/log/mongodb # run only if it does not exist
- Change owners of these directories to
mongodb
. Do this for both the directories.
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
- Restart the mongod server.
sudo systemctl restart mongod
- Let's check.
sudo systemctl status mongod
If output is something like
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2021-02-20 18:15:01 IST; 59min ago
Docs: https://docs.mongodb.org/manual
Main PID: 734383 (mongod)
Memory: 166.7M
CGroup: /system.slice/mongod.service
└─734383 /usr/bin/mongod --config /etc/mongod.conf
Feb 20 18:15:01 bunty systemd[1]: Started MongoDB Database Server.
you are good to go.
For mongo shell:
mongo
This works for me every time mongod
fails, hope it works for you too.
Namaste