mongodb service is not starting up

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
344786 次浏览

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

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.

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.

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

On my ubuntu server, just run:

sudo rm /var/lib/mongodb/mongod.lock
mongod --repair
sudo service mongodb start

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
.
.
.

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

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

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

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.

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

sudo apt-get install  mongodb

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

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 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!

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

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

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/

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.

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

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

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.

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