mongodb service is not starting up

2019-01-10 01:24发布

问题:

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

回答1:

On my ubuntu server, just run:

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


回答2:

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.



回答3:

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


回答4:

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



回答5:

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.



回答6:

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


回答7:

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

sudo apt-get install  mongodb


回答8:

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



回答9:

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



回答10:

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


回答11:

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


回答12:

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.



回答13:

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



回答14:

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!



回答15:

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


回答16:

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



回答17:

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/



标签: mongodb