86

I've installed the mongodb 2.0.3, using the mongodb-10gen debian package. Everything went well, except the service which is installed by default is not starting up when computer starts. The mongod is running only as root user. maybe this is the reason. but as far as I know, the services should be running since they are added by the root user.

What may be the solution?

if I run just mongod

Tue Mar 27 13:00:44 [initandlisten] couldn't open /data/db/transaction_processor_dummy_development.ns errno:1 Operation not permitted

If I run sudo service mongodb start it says:

mongodb start/running, process 4861

but there's no process when looking with htop and mongo says:

MongoDB shell version: 2.0.3
connecting to: test
Tue Mar 27 13:02:40 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
3
  • 2
    Need more information. How are you trying to start MongoDB? What error messages, if any, are you seeing in the console? If my [Ubuntu] machine powers off without a clean shutdown, I have to manually remove /var/lib/mongodb/mongod.lock before restarting MongoDB with sudo service mongodb start. Mar 27, 2012 at 6:16
  • if start with sudo mongod after removing /data/db/mongod.lock it runs correctly. Mar 27, 2012 at 7:07
  • 1
    This doesn't apply here since its linux, but windows problems can be solved using: stackoverflow.com/questions/4661670/… Jul 26, 2014 at 16:25

25 Answers 25

129

On my ubuntu server, just run:

sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
4
  • 1
    Thanks for this! But is there a way to fix this issue permanently? I see some the solutions below that involve running this command every time I restart, but is there a better way? Feb 10, 2015 at 9:35
  • 1
    Awesome, this worked for me. Glad I scrolled down to find the highest-voted answer. smh StackOverflow, you gotta get these higher voted answers up there when there's no accepted solution to the question. Curious to know what could be the cause of this MongoDB problem Nov 12, 2015 at 7:44
  • @HammanSamuel Sorting the answers depends on you, not SO. If you sort the answers by votes, you'd see this answer on top :)
    – NSNoob
    Jan 4, 2016 at 9:20
  • For those who get error shutting down with code: 48 at mongd --repair should stop the mongod service first by connecting to mongo shell and type use admin and then db.shutdownServer() and after that run the repair command will do the trick
    – mzparacha
    Feb 13, 2019 at 5:29
38

This can also happen if your file permissions get changed somehow. Removing the lock file didn't help, and we were getting errors in the log file like:

2016-01-20T09:14:58.210-0800 [initandlisten] warning couldn't write to / rename file /var/lib/mongodb/journal/prealloc.0: couldn't open file    /var/lib/mongodb/journal/prealloc.0 for writing errno:13 Permission denied
2016-01-20T09:14:58.288-0800 [initandlisten] couldn't open /var/lib/mongodb/local.ns errno:13 Permission denied
2016-01-20T09:14:58.288-0800 [initandlisten] error couldn't open file /var/lib/mongodb/local.ns terminating

So, went to check permissions:

ls -l /var/lib/mongodb

total 245780
drwxr-xr-x 2 mongodb mongodb     4096 Jan 20 09:14 journal
drwxr-xr-x 2 root    root        4096 Jan 20 09:11 local
-rw------- 1 root    root    67108864 Jan 20 09:11 local.0
-rw------- 1 root    root    16777216 Jan 20 09:11 local.ns
-rwxr-xr-x 1 mongodb nogroup        0 Jan 20 09:14 mongod.lock

To fix:

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

Remove the lock file if it's still there:

# rm /var/lib/mongodb/mongod.lock

Start mongodb

# service mongodb start

Tail the log and you should see at the end of it:

tail -f /var/log/mongodb/mongodb.log
2016-01-20T09:16:02.025-0800 [initandlisten] waiting for connections on port 27017
3
  • 2
    This worked for me. There were some db files with root user Feb 2, 2017 at 16:25
  • Thank you, this fixed my problem!
    – JDev
    Oct 3, 2018 at 19:15
  • Thank you, This worked and need to run this command as sudo too Jan 4, 2019 at 5:17
37

Fixed!

The reason was the dbpath variable in /etc/mongodb.conf. Previously, I was using mongodb 1.8, where the default value for dbpath was /data/db. The upstart job mongodb(which comes with mongodb-10gen package) invokes the mongod with --config /etc/mongodb.conf option.

As a solution, I only had to change the owner of the /data/db directory recursively.

0
27

I was bored of this problem so I decided to create a shell script to restore my mongo data base easily.

#!/bin/sh
sudo rm /var/lib/mongodb/mongod.lock
sudo -u mongodb mongod -f /etc/mongodb.conf --repair
sudo service mongodb start

http://ideone.com/EuqDZ8

19

Removing the .lock file and reinstalling did not solve the issue on my machine (Ubuntu 19.10). The problem was that after unexpected shutdown, the MongoDB sock does not belong to the MongoDB group and user anymore.

So, I follow the steps below:

  1. cd /tmp
  2. ls *.sock
  3. Change the user:group permission:

    chown mongodb:mongodb <YOUR_SOCK>
    
  4. sudo systemctl start mongod

  5. sudo systemctl status mongod
16

Remember that when you restart the database by removing .lock files by force, the data might get corrupted. Your server shouldn't be considered "healthy" if you restarted the server that way.

To amend the situation, either run

mongod --repair

or

> db.repairDatabase();    

in the mongo shell to bring your database back to "healthy" state.

1
  • 1
    Thank you. I ran mongod --repair and it upgraded my databases. It was necessary. But the service is still not starting. Mar 28, 2012 at 6:09
13

1 - disable fork option in /etc/mongodb.conf if enabled

2 - Repair your database

mongod --repair --dbpath DBPATH

3 - kill current mongod process

Find mongo processes

ps -ef | grep mongo

you'll get mongod PID

mongodb   PID     1  0 06:26 ?        00:00:00 /usr/bin/mongod --config /etc/mongodb.conf

Stop current mongod process

kill -9 PID

4 - start mongoDB service

service mongodb start
5
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
sudo service mongodb start
4

Sometimes you need to remove the .lock file to get the service to run

1
  • Please include instructions on how to remove the file.
    – Jeppe
    Dec 5, 2021 at 17:22
4

For ubunto , what made it happen and was real simple is to install mongodb package:

sudo apt-get install  mongodb
1
  • 1
    no, dont use this command, it installs older version of mongdb Aug 1, 2020 at 20:29
4

Please check your permissions for "data" folder. This is a permission issue, as linux users will face this issue due to SE linux permissions. So you change the ownerand group both to "mongodb" for the "data" folder

1
  • I had been trying to figure out why mongod service was not picking up custom data folder. That was permission issue. /var/lib/mongodb, /var/log/mongodb and your-custom-data-path should have mongodb user permission properly including any subfolders. Nov 30, 2017 at 17:38
3

just re-installed mongo and it worked. No collections lost. Easiest solution atleast for me

3

Nianliang's solution turned out so useful from my Vagrant ubunuto, thart I ended up adding these 2 commands to my /etc/init.d/mongodb file:

.
.
.    
start|stop|restart)
        rm /var/lib/mongodb/mongod.lock
        mongod --repair
.
.
.
3

This can also happen if the disk is full at your logpath path (e.g. if you have a dedicated /log/ directory/drive and it is full).

This had me panicking for a good 15 minutes because it also prevents you from reading the mongod.log when starting the process, so difficult to troubleshoot.

3

Recall that according to official MongoDB documentation, the right command to start the service is (@Ubuntu): sudo service mongod start (06/2018) (no mongodb or mongo).

Reference: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

2

I took a different approach:

Background: I use mongodb, single node, local to my server, as a staging area.

I installed mongo based on instructions available in MongoDB docs.

So, this is not 'prime' infrastructure, does not need to be up all the time - but still essential.

What I do: In my run script, I check if mongo is running using -

if pgrep "mongod" >/dev/null 2>&1

If it is not running, then I run

sudo mongod &

Been working like a charm, no setup etc.

If your use case is similar, let me know if it works for you too.

Good luck!

2

I had issue that starting the service mongodb failed, without logs. what i did to fix the error is to give write access to the directory /var/log/mongodb for user mongodb

2

What helped me diagnose the issue was to run mongod and specify the /etc/mondgob.conf config file:

mongod --config /etc/mongodb.conf

That revealed that some options in /etc/mongdb.conf were "Unrecognized". I had commented out both options under security: and left alone only security: on one line, which caused the service to not start. This looks like a bug.

security:
#  authorization: enabled
#  keyFile: /etc/ssl/mongo-keyfile

^^ error

#security:
#  authorization: enabled
#  keyFile: /etc/ssl/mongo-keyfile

^^ correctly commented.

1

I had struggled with the similar issue and sharing the solution accordingly. We use a mongo replica set and mongo service on my secondary server was taking too long to start. (approx 10-15 minutes)

Couldn't find anything until attempted to configure the mongo with some different folder in --dbpath And that worked fine, so we got to know that the earlier path, which was a mount directory had some issues. So followed with nss team to get the network mount issue solved.

Hope this helps someone

0

Install mongodb for ubuntu 14.04

sudo nano /etc/apt/sources.list.d/mongodb-org-3.2.list

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

sudo apt-get update

sudo apt-get install -y mongodb-org=3.2.10 mongodb-org-server=3.2.10 mongodb-org-shell=3.2.10 mongodb-org-mongos=3.2.10 mongodb-org-tools=3.2.10
0

Here's a weird one, make sure you have consistent spacing in the config file.

For example:

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

security:
    authorization: 'enabled'

If the authorization key has 4 spaces before it, like above and the rest are 2 spaces then it won't work.

0

In Fedora 35, I had to delete /tmp/mongodb-27017.sock file to have it run successfully.

You may check the logs to confirm your issue sudo less /var/log/mongodb/mongod.log

0

add this line in .bash_profile

export PATH="/usr/local/opt/[email protected]/bin:$PATH"

then run source .bash_profileenter code here

0

I have another situation where removing the lock file does not work.

And I solved it with:

systemctl stop mongod 
chown -R mongodb:mongodb /var/lib/mongodb
systemctl start mongod

Where you replaced the second line chown -R mongodb:mongodb /var/lib/mongodb to chown -R mongodb:mongodb {your mongodb file location}

0

In my case the mongodb directory inside /var/lib and /var/log was missing.

I traced this from the syslog and journalctl logs.

Creating the mongodb directory in the respective folders and changing the ownership to MongoDB user has fixed the issue.

Make sure to run sudo systemctl restart mongod.service after the above steps.

YW!

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