138
votes

I have installed mongodb and the php drivers on my unix server.

My question is how can I tell if mongodb is running? Is there a simple command line query to check status? If I start it once from the shell will it keep running if I exit the shell (this doesn't seem to be the case). How can I make the mongodb connection persistent and auto start on server reboot?

I can run:

-bash-3.2$ su
Password:
[root@xxx]# cd /var/lib
[root@xxx]# ./mongodb-linux-i686-1.6.5/bin/mongod
./mongodb-linux-i686-1.6.5/bin/mongod --help for help and startup options
Wed Feb 23 08:06:54 MongoDB starting : pid=7271 port=27017 dbpath=/data/db/ 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
** see http://blog.mongodb.org/post/137788967/32-bit-limitations

** WARNING: You are running in OpenVZ. This is known to be broken!!!

Wed Feb 23 08:06:54 db version v1.6.5, pdfile version 4.5
Wed Feb 23 08:06:54 git version: 0eb017e9b2828155a67c5612183337b89e12e291
Wed Feb 23 08:06:54 sys info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri
Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Wed Feb 23 08:06:54 [initandlisten] waiting for connections on port 27017
Wed Feb 23 08:06:54 [websvr] web admin interface listening on port 28017

If I open a seperate shell I can then then connect to mongodb:

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell version: 1.6.5
connecting to: test
db.foo.find()
{ "_id" : ObjectId("4d63d7d3eb95985ab19c8feb"), "a" : 1 }

However if I close the initial shell I can't connect:

-bash-3.2$ cd /var/lib
-bash-3.2$ ./mongodb-linux-i686-1.6.5/bin/mongo
MongoDB shell version: 1.6.5
connecting to: test
Wed Feb 23 08:25:10 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed

11

11 Answers

157
votes

check with either:

   ps -edaf | grep mongo | grep -v grep  # "ps" flags may differ on your OS

or

   /etc/init.d/mongodb status     # for MongoDB version < 2.6

   /etc/init.d/mongod status      # for MongoDB version >= 2.6

or

   service mongod status

to see if mongod is running (you need to be root to do this, or prefix everything with sudo). Please note that the 'grep' command will always also show up as a separate process.

check the log file /var/log/mongo/mongo.log to see if there are any problems reported

47
votes

I find:

ps -ax | grep mongo

To be a lot more consistent. The value returned can be used to detect how many instances of mongod there are running

39
votes

For quickly checking if mongodb is running, this quick nc trick will let you know.

nc -zvv localhost 27017

The above command assumes that you are running it on the default port on localhost.

For auto-starting it, you might want to look at this thread.

14
votes

this should work fine...

pgrep mongod

11
votes

To check current running status of mongodb use: sudo service mongodb status

8
votes

Correct, closing the shell will stop MongoDB. Try using the --fork command line arg for the mongod process which makes it run as a daemon instead. I'm no Unix guru, but I'm sure there must be a way to then get it to auto start when the machine boots up.

e.g.

mongod --fork --logpath /var/log/mongodb.log --logappend

Check out the full documentation on Starting and Stopping Mongo.

4
votes

You can use the below command, to check MongoDB status, e.g: sudo service MongoDB status which displays the status of MongoDB service as like the screenshot:

MongoDB status

4
votes

On Ubuntu (doc)

sudo systemctl status mongod

If running

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2020-10-14 14:13:40 UTC; 3s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 1604 (mongod)
     Memory: 210.8M
     CGroup: /system.slice/mongod.service
             └─1604 /usr/bin/mongod --config /etc/mongod.conf

If not running

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

To start mongodb

sudo systemctl start mongod
2
votes

Probably because I didn't shut down my dev server properly or a similar reason. To fix it, remove the lock and start the server with: sudo rm /var/lib/mongodb/mongod.lock ; sudo start mongodb

1
votes

I know this is for php, but I got here looking for a solution for node. Using mongoskin:

mongodb.admin().ping(function(err) {
    if(err === null)
        // true - you got a conntion, congratulations
    else if(err.message.indexOf('failed to connect') !== -1)
        // false - database isn't around
    else
        // actual error, do something about it
})

With other drivers, you can attempt to make a connection and if it fails, you know the mongo server's down. Mongoskin needs to actually make some call (like ping) because it connects lazily. For php, you can use the try-to-connect method. Make a script!

PHP:

$dbIsRunning = true
try {
  $m = new MongoClient('localhost:27017');
} catch($e) {
  $dbIsRunning = false
}
0
votes

To check whether the server is running on a systemd operating system, in a manner more suitable for use in a shell script, use

systemctl is-active mongod

rather than

systemctl status mongod