monit is not able to read mongod.lock - mongodb

I am writing a monit script for mongo(in ubuntu). Script given below
check process mongodb
with pidfile "/var/lib/mongodb/mongod.lock"
start program = "/sbin/start mongodb"
stop program = "/sbin/stop mongodb"
But I am getting error log
monit: Error reading pid from file '/var/lib/mongodb/mongod.lock'
Please let me know if any work around in monit for this issue.

Monit needs a pid file (containing the pid of the mongod process) so you have to create it when you start mongod
#!/bin/bash
mongod &
echo "$!" > /var/run/mongod.pid
and set /var/run/mongod.pid as pidfile

A bit late, but I found the pid file here: /var/run/mongodb.pid

I had the same error using MongoDB, this solved my problem:
chown -R mongod:mongod /var/lib/mongodb

Related

mac: command not found: mongod

I installed mongodb by brew on my MacOS as the mongodb official documentation, but I tried to run MongoDB manually as a background process, use command "mongod --config /usr/local/etc/mongod.conf --fork". Then the terminal displayed the message is : command not found: mongo.
Did I miss any steps?
The above error appears when mongodb executable is not included in the PATH environment variable.
MongoDB gets installed to /usr/local/bin/mongod
The command 'mongod --config /usr/local....' will work only if '/usr/local/bin' is included in PATH.
How to fix the problem?
The problem can be fixed in 2 ways -
Calling the mongod command with full path
/usr/local/bin/mongod --config /usr/local/etc/mongod.conf --fork
Adding '/usr/local/bin' to PATH
export PATH=$PATH:/usr/local/bin
Run the above command or add the above line to the end of .zshrc file and .bash_profile file and execute mongodb command in a new terminal window.
mongod --config /usr/local/etc/mongod.conf --fork

How to execute a mongo command in sh file?

I have a file named db.sh in bin folder and when I try to execute this command $ sh bin/db.sh I receive bin/db.sh: line 2: mongod: command not found in console what is wrong there?
#!/bin/sh
mongod --dbpath db --rest --jsonp;
Here is the situation:
which mongod would give you the path to the mongod binary. If there is no output from which, there which could not find mongod. This may be the case that there is not path in the $PATH variable, that contains the mongod binary. You can make sure by executing echo $PATH.
If you have your MongoDB installed manually, in some directory, then you will need to add /path/to/your/mongodb/bin to the $PATH variable in your .bashrc, like this:
PATH=/path/to/your/mongodb/bin:$PATH
But anyway :) seems like you do not have MongoDB installed on your machine. Follow this article to install it.

Mongo mongod init.d script not working on CentOS

I am trying to figure out why the provided init.d script is not working on CentOS. I tried starting it manually:
/etc/init.d/mongod start
But I get the following error:
Starting mongod: /usr/bin/dirname: extra operand `2>&1.pid'
Try `/usr/bin/dirname --help' for more information.
I looked in the script where it tries to start:
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
So I looked where mongod var is defined:
mongod=${MONGOD-/usr/bin/mongod}
Also tried:
service mongod start
Same error.
Not sure what I have setup wrong, but I have verified that I have the latest script but I cannot get mongod process to start.
Any ideas???
The following link appears to address the issue well
https://ma.ttias.be/mongodb-startup-dirname-extra-operand-pid/
In a nutshell, a bad script appears to have been distributed but the output it produces is not harmful, mongod still runs. If you run yum update you'll get a fixed script, but likely mongod will still fail because the script was not making it fail. Check your mongo logs (usually /var/log/mongodb/mongod.log, but can be different if specified in differently in /etc/mongod.conf). The log file should tell you the real reason it's failing.
Check the mongo pid file location in the config file /etc/mongod.conf:
awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' /etc/mongod.conf
By default there should be this line in mongod.conf: 'pidfilepath = /var/run/mongodb/mongod.pid'. Add it if it doesn't exist.
If you are using the YAML version of /etc/mongod.conf, check out this issue: https://jira.mongodb.org/browse/SERVER-13595. In short, you need to change this line in /etc/rc.d/init.d/mongod:
PIDFILE=`awk -F= '/^pidfilepath[[:blank:]]*=[[:blank:]]*/{print $2}' "$CONFIGFILE"`
to:
PIDFILE=`awk -F: '/^[[:blank:]]*pidFilePath[[:blank:]]*:[[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d ' '`
For me problem was in pidfilepath. Init script can't deal with path in format like this
pidfilepath = /var/run/mongodb/mongod.pid
PIDFILE variable inside of init script contains ' /var/run/mongodb/mongod.pid' and not '/var/run/mongodb/mongod.pid'
FIX:
replace PIDFILE line with this and it will work.
PIDFILE=awk -F= '/^pidfilepath[[:blank:]]*=[[:blank:]]*/{gsub(" ", "", $2);print $2}' "$CONFIGFILE"
I have also faced the same issue.
The fix is to make a small change in the script file(/etc/init.d/mongod) as mentioned below:
line 63 i guess:
daemon --user "$MONGO_USER" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
to
daemon --user "$MONGO_USER" --pidfile "$PIDFILE" "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"
Hope this helps !!!
It could be a RedHat bug on the initscript package:
goog.le forum
redhat bugzilla

How to set permernent dbpath for mongodb

I understand that mongo db need to be started before I can interact with it. But what I don't understand why do I set the dbpath every time? I thought we only need to configure that path once. Am I correct?
You can solve this two ways:
change your dbpath to the hard coded one which will point to /data/db/
Or make a startup script that will actually call the MongoDB instance for you
You could make a few scripts, as I said in my last point, to do this for you, as an example:
=== rnMongo.sh ===
./mongod --dbpath
Then with a single command:
./rnMongo.sh
Or as an upstart job:
# mongodb - Mongo Starter
author "lol"
description "Starts the MongoDB servers"
start on started network-services
#expect fork
exec /home/ubuntu/mongodb/bin/mongod --auth
#echo "Mongodb is now running";
#exit 0;
#stop
stop on runlevel [016]
#pre-stop
Something along those lines
Just add mongod --dbpath /home/user/mongodb to your startup applications ;)
sudo mongod --port portnumber --dbpath /path to your folder
By default it is set to
sudo mongod --port 27017 --dbpath /var/lib/mongodb

start mongodb and return to terminal

I can start mongodb on terminal via command
./mongod
It starts the mongodb server and then display me information that server is running on this port. but It does not give my terminal back. How can I start mongodb and can get terminal back so mongodb is running the background.
Also how to shutdown if its running in background
Use
./mongod --fork
or
./mongod &
To shutdown you have to send it a TERM signal.
ps aux | grep mongod - to find a PID
kill -TERM PID - send it a TERM signal, and using the first example we can use the PID file:
kill -TERM $(cat /var/run/mongodb/mongod.pid)
Also you can shut it down from the shell.
$ ./mongo
> use admin
> db.shutdownServer()
--
And another method:
./mongod --fork --pidfilepath /var/run/mongodb/mongod.pid
then (please notice the ticks around the cat)
kill -9 `cat /var/run/mongodb/mongod.pid`
./mongod &
You will see a number in the output, something similar to
[1]+ ./mongod &
To kill the process execute a kill %1 where 1 is the number between the angular brackets.