71

I can't start mongoDB with the command "sudo systemctl start mongod".

I'm getting the following message :

● mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Wed 2020-02-19 22:39:00 CET; 2s ago
     Docs: https://docs.mongodb.org/manual
  Process: 29368 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
 Main PID: 29368 (code=exited, status=14)

Feb 19 22:39:00 pop-os systemd[1]: Started MongoDB Database Server.
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Feb 19 22:39:00 pop-os systemd[1]: mongod.service: Failed with result 'exit-code'.

I've already reinstalled mongodb and I having the same problem. I don't know where the error came from if you can help me. Thank you.

Joundill
  • 6,828
  • 12
  • 36
  • 50
Donastien
  • 711
  • 1
  • 5
  • 4
  • Its always good practice is to check the logs first and find out the actual cause. Here is the log location `/var/log/mongodb/` – Muhammad Tariq Mar 05 '21 at 05:50

13 Answers13

193

Just do those two commands for temporary solution:

sudo rm -rf /tmp/mongodb-27017.sock
sudo service mongod start

For details:

That shall be fault due to user permissions in .sock file, You may have to change the owner to monogdb user.

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

For more details visit Documentation for installation

alper
  • 2,919
  • 9
  • 53
  • 102
Mohamed Sabry
  • 2,585
  • 1
  • 8
  • 13
36

I had the same problem in Manjaro and these commands solved it:

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock
HosseinNedaee
  • 500
  • 5
  • 6
27

I had the same problem, so I tried changing the permissions and some other commands I found online that messed up my MongoDB installation. Following are the commands that I tried from online and screwed up my installation:

$ sudo chown mongodb:mongodb /tmp/mongodb-27017.sock

$ sudo systemctl status mongod

$ sudo systemctl start mongodb

$ sudo systemctl unmask mongodb

After that, I reinstall mongodb by using following commands, which solved the issue:

$ sudo service mongod stop

$ sudo apt-get purge mongodb-org*

$ sudo rm -r /var/log/mongodb

$ sudo rm -r /var/lib/mongodb

$ sudo apt-get install -y mongodb-org

Now start the mongodb and check the status using following commands:

$ sudo systemctl start mongod

$ sudo systemctl status mongod

Minhaj98
  • 376
  • 3
  • 7
  • 1
    Great!. This solves the problem but it removes old databases as well and everytime if we face problem for exit code , this might not work. But this time it worked – James Aug 06 '22 at 19:29
  • Don't forget the quotes around `sudo apt-get purge 'mongodb-org*'` – Anne van Rossum Aug 26 '22 at 09:09
  • 1
    This worked for me, I also had to remove some leftovers: `sudo rm /usr/bin/mongo*` which were conflicting from an earlier install. – Dave Dec 16 '22 at 09:06
26

This worked for me and I don't reinstall 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
sudo chown -R mongodb:mongodb /var/log/mongodb
Rakibulinux
  • 451
  • 5
  • 4
12

These two commands solve the problem for me(Linux Mint 20.2).

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
Mr. Kabir
  • 695
  • 8
  • 9
7

This error happend to me because of the ownship of the .sock file just try these two command

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

now restart the mongo server-

sudo systemctl start mongod

now you can check the status of mongo server with the following command -

sudo systemctl status mongod
Manish Khulbe
  • 71
  • 1
  • 1
5

This will worked for you and don't reinstall MongoDB.

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

then run these commands

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

start mongod service

sudo service mongod start 

mongod.service will be active

sudo service mongod status
Mhammed Talhaouy
  • 1,129
  • 10
  • 14
4

for me, this works

sudo systemctl stop mongod
sudo mongod --repair --dbpath /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
sudo systemctl start mongod
  • Thanks to @[https://stackoverflow.com/users/811240/mike](Mike), I was reminded of reading logs. For this answer, the situation is that: `Failed to unlink socket file`, "Operation not permitted"`. – Ryuujo Jun 07 '23 at 12:25
2

It's weird that none of the other answers here mention to look in the log files. This should always be the first thing you do. On Ubuntu, this is located at /var/log/mongodb/mongod.log.

In my case, I was getting messages like the following:

{
   "t":{
      "$date":"2022-12-03T17:19:25.117+00:00"
   },
   "s":"F",
   "c":"CONTROL",
   "id":20574,
   "ctx":"main",
   "msg":"Error during global initialization",
   "attr":{
      "error":{
         "code":140,
         "codeName":"InvalidSSLConfiguration",
         "errmsg":"ssl client initialization problem for certificate: /etc/ssl/mongodb.pem :: caused by :: The provided SSL certificate is expired or not yet valid. notBefore 2022-08-22T21:15:57.000+00:00, notAfter 2022-11-20T21:15:56.000+00:00"
      }
   }
}

So, the solution was to update the SSL certificate, which had expired.

If you are using Let's Encrypt, here's a --post-hook script to do this automatically (change example.com to your domain):

#!/bin/bash
cat /etc/letsencrypt/live/example.com/{fullchain.pem,privkey.pem} | tee /etc/ssl/mongodb.pem 
chown mongodb:mongodb /etc/ssl/mongodb.pem
chmod 600 /etc/ssl/mongodb.pem

If the certificate already exists, you can also add this under [renewalparams] of /etc/letsencrypt/renewal/example.com.conf

post_hook = /root/path/to/script.sh

Or you can just do a one-liner instead of linking to an external script:

post_hook = cat /etc/letsencrypt/live/example.com/fullchain.pem /etc/letsencrypt/live/example.com/privkey.pem | sudo tee /etc/ssl/mongodb.pem && chown mongodb:mongodb /etc/ssl/mongodb.pem && chmod 600 /etc/ssl/mongodb.pem
Mike
  • 23,542
  • 14
  • 76
  • 87
  • This answer should be bumped up. In my case, after looking at the log, I saw that the error was the TCP port was in use by something else. I then ran "sudo lsof -i -P -n | grep LISTEN" to figure out who was the culprit (for me it was mongo running in a docker container). – Cosmo Apr 14 '23 at 14:43
0

These 3 steps solved my problem:

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/log/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
Sudarshan
  • 1
  • 1
  • 2
  • 2
0

sudo mongod --dbpath /var/lib/mongodb/ --journal

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 10 '21 at 16:22
0

I had the same issue on AWS EC2 ubuntu server. I eventually found out that this was due to lack of available space on the disk. I ran df -h to determine which partition is full, and compared it to my volume on AWS console (this guide helped).

I increased the available storage on the AWS volume, ran sudo reboot now and the issue was fixed

Semion Vino
  • 121
  • 1
  • 9
0

Use this command:

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

then this command:

sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
ayush
  • 1
  • 1