Skip to content

Instantly share code, notes, and snippets.

@fccoelho
Forked from uiltondutra/gist:231960
Created September 21, 2011 17:26
Show Gist options
  • Save fccoelho/1232721 to your computer and use it in GitHub Desktop.
Save fccoelho/1232721 to your computer and use it in GitHub Desktop.
MongoDB init script for a sharding node.
To install this init script, you should save mongodb to /etc/init.d/ and mongosharding.conf to /etc/
then run the following commands as root or with sudo:
chmod 755 /etc/init.d/mongodb
chown root:root /etc/init.d mongodb
update-rc.d mongodb defaults
This installation procedure was tested on UBUNTU 11.10
#! /bin/bash
# This file should be saved as /etc/init.d/mongodb
# Copyright 2011 Flavio Codeco Coelho <fccoelho at gmail dot com>
#
### BEGIN INIT INFO
# Provides: mongodb
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the mongodb sharding node
# Description: starts mongodb sharding node using start-stop-daemon
### END INIT INFO
source /etc/mongosharding.conf
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
SHARDPIDFILE=/usr/local/mongodb/logs/shard.pid
CONFIGPIDFILE=/usr/local/mongodb/logs/configdb.pid
MONGOSPIDFILE=/usr/local/mongodb/logs/mongos.pid
SHARDLOGFILE=/usr/local/mongodb/logs/shard.log
CONFIGLOGFILE=/usr/local/mongodb/logs/configdb.log
MONGOSLOGFILE=/usr/local/mongodb/logs/mongos.log
SHARD= $(which mongod)#/usr/local/sbin/mongod
SHARD_ARGS="--fork --shardsvr --dbpath $DATAPATH --port $SHARDPORT --logpath $SHARDLOGFILE"
CONFIG= $(which mongod)#/usr/local/sbin/mongod
CONFIG_ARGS="--fork --configsvr --dbpath $CONFIGDATAPATH --port $CONFIGPORT --logpath $CONFIGLOGFILE"
MONGOS= $(which mongos)#/usr/local/sbin/mongos
MONGOS_ARGS="--fork --configdb $CONFIG_SERVER:$CONFIGPORT --logpath $MONGOSLOGFILE"
NAME=mongodb
DESC=mongodb
test -x $SHARD || exit 0
test -x $CONFIG || exit 0
test -x $MONGOS || exit 0
#set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon -m --pidfile $SHARDPIDFILE --exec $SHARD --start -- $SHARD_ARGS >> $SHARDLOGFILE&
if $HOST_CONFIG;
then
echo -n "Starting MongoDB Config daemon"
start-stop-daemon -m --pidfile $CONFIGPIDFILE --exec $CONFIG --start -- $CONFIG_ARGS >> $CONFIGLOGFILE&
echo -n "Waiting for Config daemon to start:"
while [ ! -e $CONFIGPIDFILE ]
do
sleep 1 #waits for config to start
echo -n "."
done
fi
echo -n "Starting MongoDB Mongos daemon"
start-stop-daemon -m --pidfile $MONGOSPIDFILE --exec $MONGOS --start -- $MONGOS_ARGS >> $MONGOSLOGFILE&
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --quiet --pidfile $SHARDPIDFILE --exec $SHARD --stop --retry=TERM/10/KILL/3
start-stop-daemon --quiet --pidfile $CONFIGPIDFILE --exec $CONFIG --stop --retry=TERM/10/KILL/3
start-stop-daemon --quiet --pidfile $MONGOSPIDFILE --exec $MONGOS --stop --retry=TERM/10/KILL/3
echo "$NAME."
killall mongod # making sure they stop
killall mongos
sudo rm /usr/local/mongodb/logs/*.pid
;;
restart|force-reload)
echo "Restarting $DESC: "
start-stop-daemon --quiet --pidfile $SHARDPIDFILE --exec $SHARD --stop --retry 3
sleep 1
start-stop-daemon -m --pidfile $SHARDPIDFILE --exec $SHARD --start -- $SHARD_ARGS >> $SHARDLOGFILE&
if $HOST_CONFIG;
then
start-stop-daemon --quiet --pidfile $CONFIGPIDFILE --exec $CONFIG --stop --retry 3
sleep 1
start-stop-daemon -m --pidfile $CONFIGPIDFILE --exec $CONFIG --start -- $CONFIG_ARGS >> $CONFIGLOGFILE&
echo -n "Waiting for Config daemon to start:"
while [ ! -e $CONFIGPIDFILE ]
do
sleep 1 #waits for config to start
echo -n "."
done
fi
start-stop-daemon --quiet --pidfile $MONGOSPIDFILE --exec $MONGOS --stop --retry 3
sleep 1
echo -n "Starting MongoDB Mongos daemon"
start-stop-daemon -m --pidfile $MONGOSPIDFILE --exec $MONGOS --start -- $MONGOS_ARGS >> $MONGOSLOGFILE&
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile $SHARDPIDFILE --exec $SHARD
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
#!/bin/bash
###################################################################
# this configuration script will be read by the MongoDb init script
# it should be saved as mongosharding.conf in the /etc folder
# Copyright 2011 Flavio Codeco Coelho <fccoelho at gmail dot com>
###################################################################
# Config server options
HOST_CONFIG=false #Set to true if this box is to host a config server as well
CONFIG_SERVER=127.0.0.1 #one or three ip addresses separated by commas
CONFIGDATAPATH=/data/config
CONFIGPORT=20000
# Shard Daemon options
DATAPATH=/data/db
SHARDPORT=10000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment