30

I have VPS with Ubuntu 18.04 LTS with Mongodb and my script was working until 3 days ago fine, but suddenly my hosting server was rebooted (I purchased it from a hosting company) and when my server was boot again, I saw my website was not working and did not connect to database. The result of systemctl status mongod is:

 mongod.service - MongoDB Database Server
   Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2020-10-29 22:47:35 +0330; 17h ago
     Docs: https://docs.mongodb.org/manual
  Process: 2800 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
 Main PID: 2800 (code=exited, status=14)

Oct 29 22:47:35 MYHOSTNAME systemd[1]: Started MongoDB Database Server.
Oct 29 22:47:35 MYHOSTNAME systemd[1]: mongod.service: Main process exited, code=exited, status=14/n/a
Oct 29 22:47:35 MYHOSTNAME systemd[1]: mongod.service: Failed with result 'exit-code'.

When I restart the service, nothing happens. I checked /var/log/mongodb/mongod.log and this is the log:

2020-10-30T15:53:39.251+0330 I REPL     [signalProcessingThread] Stepping down the ReplicationCoordinator for shutdown, waitTime: 10000ms
2020-10-30T15:53:39.458+0330 I CONTROL  [signalProcessingThread] Shutting down the LogicalSessionCache
2020-10-30T15:53:39.460+0330 I NETWORK  [signalProcessingThread] shutdown: going to close listening sockets...
2020-10-30T15:53:39.460+0330 I NETWORK  [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2020-10-30T15:53:39.461+0330 I NETWORK  [signalProcessingThread] Shutting down the global connection pool
2020-10-30T15:53:39.461+0330 I STORAGE  [signalProcessingThread] Shutting down the PeriodicThreadToAbortExpiredTransactions
2020-10-30T15:53:39.462+0330 I REPL     [signalProcessingThread] Shutting down the ReplicationCoordinator
2020-10-30T15:53:39.462+0330 I SHARDING [signalProcessingThread] Shutting down the ShardingInitializationMongoD
2020-10-30T15:53:39.463+0330 I COMMAND  [signalProcessingThread] Killing all open transactions
2020-10-30T15:53:39.463+0330 I -        [signalProcessingThread] Killing all operations for shutdown
2020-10-30T15:53:39.463+0330 I NETWORK  [signalProcessingThread] Shutting down the ReplicaSetMonitor
2020-10-30T15:53:39.463+0330 I CONTROL  [signalProcessingThread] Shutting down free monitoring
2020-10-30T15:53:39.464+0330 I CONTROL  [signalProcessingThread] Shutting down free monitoring
2020-10-30T15:53:39.466+0330 I FTDC     [signalProcessingThread] Shutting down full-time data capture
2020-10-30T15:53:39.466+0330 I FTDC     [signalProcessingThread] Shutting down full-time diagnostic data capture
2020-10-30T15:53:39.470+0330 I STORAGE  [signalProcessingThread] Shutting down the HealthLog
2020-10-30T15:53:39.471+0330 I STORAGE  [signalProcessingThread] Shutting down the storage engine
2020-10-30T15:53:39.472+0330 I STORAGE  [signalProcessingThread] WiredTigerKVEngine shutting down
2020-10-30T15:53:39.472+0330 I STORAGE  [signalProcessingThread] Shutting down session sweeper thread
2020-10-30T15:53:39.472+0330 I STORAGE  [signalProcessingThread] Finished shutting down session sweeper thread
2020-10-30T15:53:39.648+0330 I STORAGE  [signalProcessingThread] shutdown: removing fs lock...
2020-10-30T15:53:39.648+0330 I -        [signalProcessingThread] Dropping the scope cache for shutdown
2020-10-30T15:53:39.648+0330 I CONTROL  [signalProcessingThread] now exiting
2020-10-30T15:53:39.648+0330 I CONTROL  [signalProcessingThread] shutting down with code:0

I did telnet localhost 27017 and it works fine and connects (it's closed for remote connections).

I removed /tmp/mongodb-27017.sock manually, checked ownership of /var/log/mongodb and /var/lib/mongodb and everything is correct. I also did chown -R mongodb:mongodb on both folders to correct them if any incorrect ownership has happened during sudden reboot, but nothing worked.

By the way, when I run /usr/bin/mongod --config /etc/mongod.conf command, my website runs, I can login to my user account and etc. I am currently doing this in a screen to run my website currently, but I do not know how to find the problem and fix it.

Would you help me to check what's wrong with MongoDB and how to fix it? Also like nginx -t that tests config file, is there any similar command? I read man mongod but found nothing.

edited to add /etc/mongodb.conf contents:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1


# how the process runs
processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:
14
  • 1
    Can you please provide the conf file Commented Oct 30, 2020 at 19:25
  • 1
    Try to set the processManagement.pidFilePath Check the service file /lib/systemd/system/mongod.service - which user runs the service? mongod or mongodb? Commented Oct 30, 2020 at 19:45
  • 1
    Could you please run /usr/bin/mongod --config /etc/mongod.conf with mongodb user once and also check ls -ld /tmp/mongodb-27017.sock Commented Nov 9, 2020 at 4:53
  • 1
    can you also share output of cat /etc/passwd | grep mongodb and cat /etc/group | grep mongodb Commented Nov 9, 2020 at 14:25
  • 2
    It means severity fail, and it's the log when the system actually fails starting. Also, I'd look if the mongod.lock file under the dbpath is empty, it should be 0B. Another thing to check it the systemd log, there could be relevant data there, from the system itself
    – user12582392
    Commented Nov 13, 2020 at 6:28

8 Answers 8

106

Try this, it worked for me:

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock    
sudo service mongod restart
6
  • 3
    Hi there! This answer worked for me too, for the same issue. However, do you have any idea as to why it worked? How even is the 'mongodb' user generated? Sorry for the confusion.
    – nikUoM
    Commented Aug 19, 2021 at 12:11
  • 1
    In my case, only /tmp/mongodb-27017.sock needed to be updated. The owner/permissions of /var/lib/mongodb were already correct.
    – Seth Falco
    Commented Sep 30, 2021 at 13:18
  • 3
    Care to add a little information why that happened and why changing permission worked? Thanks Commented Oct 9, 2021 at 2:55
  • 1
    wow this works for me also, thanks Commented Dec 28, 2021 at 7:14
  • 1
    Well, I accidentally starting mongod by non-sudo user and got me here. Saved the day by these chown answer.
    – Randy Lam
    Commented Oct 31, 2022 at 6:12
15

In my case the user is mongod and the folder in /var/lib folder is mongo, so I had to run this command

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

systemctl restart mongod
2
  • 1
    I appreciate you participate and answer. Yes, the user may differ in different distros and settings, and you can figure it out by watching mongo's conf file
    – Saeed
    Commented Jul 19, 2021 at 19:19
  • 1
    u may find the user name of mongo by: cat /etc/passwd
    – Sahin
    Commented Oct 13, 2021 at 21:26
6

I figured out the issue by doing a thorough investigation in /var/log/syslog with grepping mongo.

I did chown -R mongodb:mongodb /var/lib/mongodb/ and terminated my screen command, then restarted mongod service and then checking status, so now the service and my website is running without issue.

The better version of the solution, run these commands:

chown -R mongodb:mongodb /var/lib/mongodb/
systemctl restart mongod
0
2
  1. Go to /tmp and find mongodb sock

    cd /tmp; ls -l *.sock

  2. Change the owner of that .sock file, it used to named as mongodb-27017.sock, but if you find it in a different name, its ok.

    chown -R mongod:mongod /var/lib/mongo

    chown mongod:mongod mongodb-27017.sock

if the result come like this chown: invalid user: mongod:mongod then it means that the user and group for mongodb was not mongod.

1

In my case /tmp/mongodb-27017.sock was owned by root:root. I've deleted the file and restarted like the accepted answer did and after that the file was owned by mongodb:mongodb.

2
  • 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.
    – Community Bot
    Commented Mar 27 at 15:46
  • worked for me! thanks
    – nnolte
    Commented Aug 26 at 13:57
0

I know it's a late answer, but since this issue is a known one in Ubuntu for mongodb, I recommend this:

  1. Create a file called /root/mongo.sh with below contents:
chown -R mongodb:mongodb /var/lib/mongodb/
systemctl restart mongod
  1. Run chmod +x /root/mongo.sh in teminal
  2. Add that to crontab via running crontab -e:
0 2 * * * /root/mongo.sh

Now it runs this file each day at 02:00 A.M. and you won't worry.

If you see the issue during the day, only run this:

/root/mongo.sh
0

I killed all the process already running one by one with the name mongo

    root@lancer:/mongodb# ps -ef | grep mongo
root       721 27584  0 Sep27 pts/7    00:00:00 sudo nano mongo.conf
root       722   721  0 Sep27 pts/7    00:00:00 nano mongo.conf
root       736 27584  0 Sep27 pts/7    00:00:00 sudo nano mongo.conf
root       737   736  0 Sep27 pts/7    00:00:00 nano mongo.conf
root       779 27584  0 Sep27 pts/7    00:00:00 sudo nano mongo.conf
root       780   779  0 Sep27 pts/7    00:00:00 nano mongo.conf
root       873 27584  0 Sep27 pts/7    00:00:00 vi mongo.conf
root      1102     1  0 Sep27 ?        00:03:34 mongod --dbpath /mongodb/data --logpath /mongodb/log/mongod.log --fork
root      1150 27584  0 Sep27 pts/7    00:00:02 mongosh
root     10858  5566  0 Sep20 pts/7    00:00:00 sudo systemctl status mongod
root     12386 27584  0 16:27 pts/7    00:00:00 vi mongod.conf
root     13091 27584  0 16:48 pts/7    00:00:00 sudo systemctl status mongod
root     13092 13091  0 16:48 pts/7    00:00:00 systemctl status mongod
root     13258 27584  0 16:52 pts/7    00:00:00 sudo adduser mongod
root     13459 27584  0 16:52 pts/7    00:00:00 sudo adduser mongodb
root     13507 13460  0 16:53 pts/7    00:00:00 /usr/bin/chfn mongodb
root     14091 27584  0 17:37 pts/7    00:00:00 mongo
root     16620 27584  0 20:32 pts/7    00:00:00 grep --color=auto mongo
root     19169 27584  0 11:25 pts/7    00:00:00 vi mongod.service
root     24606 22371  0 Sep27 pts/7    00:00:00 mongo
root     24763 22371  0 Sep27 pts/7    00:00:00 vi mongodb.conf
root     29184 27584  0 Sep27 pts/7    00:00:00 sudo nano /lib/systemd/system/mongod.service
root     29185 29184  0 Sep27 pts/7    00:00:00 nano /lib/systemd/system/mongod.service 


and then did

root@lancer:/# sudo systemctl start mongod
root@lancer:/# sudo systemctl status mongod

● mongod.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
   Active: active (running) since Tue 2021-09-28 20:39:09 IST; 33s ago
 Main PID: 16836 (mongod)

Then it starts running successfully.

1
  • 2
    You can kill all mongo processes killall mongo instead of kill PID
    – Saeed
    Commented Sep 28, 2021 at 15:48
0

Checked /etc/mongod.conf - it referenced

pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile

Checked /var/run (there was no mongodb)

mkdir -p /var/run/mongodb
chown mongod.mongod /var/run/mongodb
systemctl daemon-reload
systemctl start mongod
systemctl status mongod

This seemed like it worked ... but it didn't

Credit to: https://stackoverflow.com/a/71384602/2182349

On a CentOS 7 box, I added these lines to /lib/systemd/system/mongod.service

ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown -R mongod.mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/mongod.pid
Type=forking

mongod --version db version v5.0.15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.