83

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
Esenbek Kydyr uulu
  • 1,553
  • 1
  • 14
  • 19
  • 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`. – Adam Monsen Mar 27 '12 at 06:16
  • if start with `sudo mongod` after removing `/data/db/mongod.lock` it runs correctly. – Esenbek Kydyr uulu Mar 27 '12 at 07:07
  • 1
    This doesn't apply here since its linux, but windows problems can be solved using: http://stackoverflow.com/questions/4661670/cannot-start-mongodb-as-a-service – James Oravec Jul 26 '14 at 16:25

23 Answers23

126

On my ubuntu server, just run:

sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start
Nianliang
  • 2,926
  • 3
  • 29
  • 22
  • 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? – Deepak Joy Cheenath Feb 10 '15 at 09: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 – Hamman Samuel Nov 12 '15 at 07: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 04 '16 at 09: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 '19 at 05:29
36

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.

Esenbek Kydyr uulu
  • 1,553
  • 1
  • 14
  • 19
35

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
RyanH
  • 991
  • 7
  • 13
25

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

sivi
  • 10,654
  • 2
  • 52
  • 51
Andres
  • 4,323
  • 7
  • 39
  • 53
18

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
gotqn
  • 42,737
  • 46
  • 157
  • 243
15

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.

Socratees Samipillai
  • 2,985
  • 1
  • 20
  • 20
12

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
AbdullahDiaa
  • 1,316
  • 16
  • 17
5
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
sudo service mongodb start
trai bui
  • 588
  • 12
  • 36
4

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

sudo apt-get install  mongodb
shacharsol
  • 2,326
  • 20
  • 14
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

  • 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. – Abubacker Siddik Nov 30 '17 at 17:38
4

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

Joe
  • 386
  • 1
  • 6
3

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

ashish
  • 2,090
  • 3
  • 17
  • 16
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
.
.
.
schory
  • 631
  • 5
  • 5
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.

toblerpwn
  • 5,415
  • 8
  • 38
  • 46
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/

Greg Wozniak
  • 5,209
  • 3
  • 26
  • 29
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!

zevij
  • 2,416
  • 1
  • 23
  • 32
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

shacharsol
  • 2,326
  • 20
  • 14
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.

Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
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

Sanjay Bharwani
  • 3,317
  • 34
  • 31
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
sphinks
  • 3,048
  • 8
  • 39
  • 55
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.

Pescolly
  • 922
  • 11
  • 18
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

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
0

add this line in .bash_profile

export PATH="/usr/local/opt/mongodb-community@4.4/bin:$PATH"

then run source .bash_profileenter code here