'Mongodb not working on Ubuntu -> mongod.service: Failed with result 'exit-code'

I installed mongodb following this tutorial here, no errors during the installation but when I try to start the mongod server using this command sudo systemctl start mongodb as the tutorial mentions, i getting this error when i try to check whether it is running using this command sudo systemctl status mongodb.

● mongodb.service - High-performance, schema-free document-oriented 

database
   Loaded: loaded (/etc/systemd/system/mongodb.service; enabled; vendor preset: 
   Active: failed (Result: exit-code) since Rab 2016-06-01 18:04:20 MYT; 4s ago
  Process: 8241 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (cod
 Main PID: 8241 (code=exited, status=14)

Jun 01 18:04:20 yasinya systemd[1]: Started High-performance, schema-free docume
Jun 01 18:04:20 yasinya systemd[1]: mongodb.service: Main process exited, code=e
Jun 01 18:04:20 yasinya systemd[1]: mongodb.service: Unit entered failed state.
Jun 01 18:04:20 yasinya systemd[1]: mongodb.service: Failed with result 'exit-co
lines 1-10/10 (END)

so can anyone tell what is wrong and how I can fix it.



Solution 1:[1]

Recently i have solved the same issue. I was unable to find the solution on googled, but i get come clues to, how get figure it out. In my case this issue was related to mongodb-27017.sock file.

Failed to unlink socket file /tmp/mongodb-27017.sock errno:1 Operation not permitted

So i have changed the permission of /tmp/mongodb-27017.sock file to default mongodb user.

sudo chown mongodb /tmp/mongodb-27017.sock
sudo chgrp mongodb /tmp/mongodb-27017.sock

After this sudo systemctl status mongodb working fine and mongodb is started.

Solution 2:[2]

Stumbed on the same issue, just reboot

sudo reboot

Solution 3:[3]

This error returned by MongoDB applications which encounter an unrecoverable error, an uncaught exception or uncaught signal. The system exits without performing a clean shutdown.

I solved the problem by the command:

sudo chown -R mongodb:mongodb /var/log/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chmod -R 755 /var/lib/mongodb
sudo chmod -R 755 /var/log/mongodb

sudo chown mongodb /tmp/mongodb-27017.sock
sudo chgrp mongodb /tmp/mongodb-27017.sock

Then restart service

sudo systemctl restart mongod

Then check your mongodb service

sudo systemctl status mongod

Solution 4:[4]

I had same use ,

get solve just

sudo reboot

Solution 5:[5]

The first thing, you should examine the mongo log. It should tell you what went wrong.

$ tail -n 100 /var/log/mongodb/mongod.log

This command will show last 100 lines of the log file which will point you to the exact problem.

For example, mine wasn't working because i was running a mongo instance using the same port 27017. when i ran another instance, the instance failed to load.

LOG

2018-05-11T19:03:49.102-0400 I CONTROL  [initandlisten] MongoDB starting : pid=1247 port=27017 dbpath=/var/lib/mongodb 64-bit host=dev-hyuen
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] db version v3.2.20
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] git version: a7a144f40b70bfe290906eb33ff2714933544af8
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] allocator: tcmalloc
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] modules: none
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] build environment:
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten]     distmod: ubuntu1604
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten]     distarch: x86_64
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten]     target_arch: x86_64
2018-05-11T19:03:49.103-0400 I CONTROL  [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1", port: 27017 }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log", quiet: true } }
2018-05-11T19:03:49.124-0400 E NETWORK  [initandlisten] listen(): bind() failed errno:98 Address already in use for socket: 127.0.0.1:27017
2018-05-11T19:03:49.124-0400 E NETWORK  [initandlisten]   addr already in use
2018-05-11T19:03:49.124-0400 E STORAGE  [initandlisten] Failed to set up sockets during startup.
2018-05-11T19:03:49.124-0400 I CONTROL  [initandlisten] dbexit:  rc: 48

Solution 6:[6]

Step1. sudo systemctl stop mongodb

Step2. sudo systemctl start mongod

Step3. sudo systemctl status mongod

I hope it helps you

Solution 7:[7]

Case with me:

  1. I installed mongodb from official site.
  2. Worked fine after installation, even worked with command-line, Robo3T and Compass.
  3. 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?

  1. 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"
  1. 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
  1. 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 
  1. Restart the mongod server.
sudo systemctl restart mongod
  1. 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 ?

Solution 8:[8]

I was getting the similar error, in my case mongo couldn't use 27017. I needed to stop whatever was using 27017 port and restart mongo.

Solution 9:[9]

because the permission setting

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock

if this worked for me after reinstalling mongodb. I have created manually

sudo mkdir /var/lib/mongodb
sudo mkdir /var/log/mongodb

and changed owner for both

sudo chown -R mongodb:mongodb /var/lib/mongodb
chown -R mongodb:mongodb /var/log/mongodb

Solution 10:[10]

The problem may be related to the lack of space with your /dev/root/ (it was mine).

Check with df -h and see if there enough free space. Otherwise, as explain there, try to free some space by moving the log files to another disk.

sudo /etc/init.d/rsyslog stop
sudo mv /var/log /OTHERDISK/
sudo ln -s /OTHERDISK/log /var/log
sudo /etc/init.d/rsyslog start

Hope it could help as it helps me

Solution 11:[11]

After googling around for a while. I found that that is because the permission setting on /var/lib/mongodb and /tmp/mongodb-27017.lock are wrong. You will have to change the owner to monogdb user

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock

Solution 12:[12]

In my case, increasing ulimit value for number of open files, solved my problem.

Like in the MongoDB docs note, if you execute ulimit -n command and the result is lower than 64000. Just execute this command: ulimit -n 128000. By the way 128000 is not a specific number. I made it up.

Solution 13:[13]

In my case the system was not able to to find mongod.service Running the following command will fixed it

sudo systemct1 enable mongod

then restart the database by

sudo service mongod restart