Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MongoDB multiple instance upstart scripts: mongodb, mongod --configsvr and mongos
MongoDB upstart scripts for Ubuntu.
Run following commands after installing upstart scripts:
ln -s /lib/init/upstart-job /etc/init.d/mongoconf
ln -s /lib/init/upstart-job /etc/init.d/mongodb
ln -s /lib/init/upstart-job /etc/init.d/mongos
To start services use:
service mongodb [start|stop|restart]
service mongoconf [start|stop|restart]
service mongos [start|stop|restart]
# /etc/mongo.conf
MONGODB_ENABLED=yes
MONGOCONF_ENABLED=no
MONGOS_ENABLED=no
# /etc/mongoconf.conf
#configsvr config file
#start mongod in configsvr mode
configsvr=true
#where to log
logpath=/var/log/mongodb/mongoconf.log
#log overwritten or appended to
logappend=true
#override port
#port=27019
#path to data files
#dbpath=/data/configdb
smallfiles = true
# /etc/mongos.conf
#mongos config file
#config servers
configdb=host1:27019,host2:27019,host3:27019
#where to log
logpath=/var/log/mongodb/mongos.log
#log overwritten or appended to
logappend=true
#override port
#port=27017
# Ubuntu upstart file at /etc/init/mongoconf.conf
limit nofile 20000 20000
kill timeout 300 # wait 300s between SIGTERM and SIGKILL.
start on runlevel [2345]
stop on runlevel [06]
script
if [ -f /etc/mongo.conf ]; then . /etc/mongo.conf; fi
if [ "x$MONGOCONF_ENABLED" = "xyes" ]; then
ENABLE_MONGOCONF="yes"
if [ "x$ENABLE_MONGOCONF" = "xyes" ]; then exec start-stop-daemon --make-pidfile --pidfile /var/run/mongoconf.pid --start --quiet --chuid mongodb --exec /usr/bin/mongod -- --config /etc/mongoconf.conf; fi
fi
end script
# Ubuntu upstart file at /etc/init/mongodb.conf
limit nofile 20000 20000
kill timeout 300 # wait 300s between SIGTERM and SIGKILL.
pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script
start on runlevel [2345]
stop on runlevel [06]
script
if [ -f /etc/mongo.conf ]; then . /etc/mongo.conf; fi
if [ "x$MONGODB_ENABLED" = "xyes" ]; then
ENABLE_MONGODB="yes"
if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --make-pidfile --pidfile /var/run/mongodb.pid --start --quiet --chuid mongodb --exec /usr/bin/mongod -- --config /etc/mongodb.conf; fi
fi
end script
# Ubuntu upstart file at /etc/init/mongos.conf
limit nofile 20000 20000
kill timeout 300 # wait 300s between SIGTERM and SIGKILL.
start on runlevel [2345]
stop on runlevel [06]
script
if [ -f /etc/mongo.conf ]; then . /etc/mongo.conf; fi
if [ "x$MONGOS_ENABLED" = "xyes" ]; then
ENABLE_MONGOS="yes"
if [ "x$ENABLE_MONGOS" = "xyes" ]; then exec start-stop-daemon --make-pidfile --pidfile /var/run/mongos.pid --start --quiet --chuid mongodb --exec /usr/bin/mongos -- --config /etc/mongos.conf; fi
fi
end script
@aivus

This comment has been minimized.

Copy link

@aivus aivus commented Apr 13, 2012

Thank you.
Your scripts were very helpful for me.

@alex-klepa

This comment has been minimized.

Copy link
Owner Author

@alex-klepa alex-klepa commented Apr 13, 2012

You're welcome! ;)

@ashpalmer

This comment has been minimized.

Copy link

@ashpalmer ashpalmer commented Oct 4, 2012

Excellent post!

@AntonStoeckl

This comment has been minimized.

Copy link

@AntonStoeckl AntonStoeckl commented Nov 6, 2012

Your scripts in /etc/init are a bit over complicated. ;-)
You source /etc/mongo.conf to check MONGODB_ENABLED, then you set ENABLE_MONGODB, the you check ENABLE_MONGODB. ;-)

Ubuntu (I guess Debian also) enables and disables services in /etc/default/.... (in this case /etc/default/mongodb).
So I recommend to put this into that file:

ENABLE_MONGODB=yes # (or no)
ENABLE_MONGOCONF=yes # (or no)
ENABLE_MONGOS=no # (or no)

This then works with the /etc/init/mongodb.conf that is shipped:

Ubuntu upstart file at /etc/init/mongodb.conf

limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script

start on runlevel [2345]
stop on runlevel [06]

script
ENABLE_MONGODB="yes"
if [ -f /etc/default/mongodb ]; then . /etc/default/mongodb; fi
if [ "x$ENABLE_MONGODB" = "xyes" ]; then exec start-stop-daemon --start --quiet --chuid mongodb --exec /usr/bin/mongod -- --config /etc/mongodb.conf; fi
end script

Replace the ENABLE_.... with the appropriate value, the exec lines (I think) are fine as you supplied them.

Same result, less complicated scripts. ;-)

Regards, Anton

@josmardias

This comment has been minimized.

Copy link

@josmardias josmardias commented Nov 24, 2012

Keep it simple avoiding "start-stop-daemon", "sudo" and "su". All of them forces you to use "expect" stanza to help upstart track the proper pid of the service.
More specific, start-stop-daemon ensures that you will not have two services with the same name, forcing you to use "--pidfile" variation.

We can use "setuid":

Ubuntu upstart file at /etc/init/mongodb.conf


limit nofile 20000 20000

kill timeout 300 # wait 300s between SIGTERM and SIGKILL.

pre-start script
mkdir -p /var/lib/mongodb/
mkdir -p /var/log/mongodb/
end script

start on runlevel [2345]
stop on runlevel [06]

setuid mongodb
exec /usr/bin/mongod --config /etc/mongoconf.conf

@KevinBurton

This comment has been minimized.

Copy link

@KevinBurton KevinBurton commented Jan 16, 2013

I am getting:

Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service S20mongoconf start
initctl: Unknown job: S20mongoconf

Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the start(8) utility, e.g. start S20mongoconf

In the boot log. This is for the config server. What am I doing wrong?

@sebastianmarr

This comment has been minimized.

Copy link

@sebastianmarr sebastianmarr commented Jan 30, 2013

Helpful config files, thanks!
Is there any way to check if a config server is online before starting mongos? Because otherwise it would just fail silently.

@Darkflib

This comment has been minimized.

Copy link

@Darkflib Darkflib commented Feb 14, 2013

@KevinBurton take off the S20, this is simply a way for the system to understand the order these start and stop commands need to be executed.

@billmag

This comment has been minimized.

Copy link

@billmag billmag commented Sep 8, 2013

@sebastianmarr - What did you end up doing to solve the ordering problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment