Aman Aggarwal Aman Aggarwal - 5 months ago 36
Bash Question

Init scripts for mongos and config server for MongoDB sharding

I created a sharding in local environment for testing purpose.

I have three config server on 1 machine and 1 query router on same machine and two data nodes on two different machines.

Everything works fine but my problem is I am unable to keep all process active running on different ports as I don't have any start / stop script. I run processes on command line with

&
in the end to make it active which is very poor way to keep proces active and sometime it dies automatically.

Please help or provide a way to use scripts and also script can handle various ports to active all process on single machine.

Answer

For config server we can use the below script :

#!/bin/bash

### BEGIN INIT INFO
# Provides:        MongoDB Config Server
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop MongoDB Config server
### END INIT INFO

start() {
        /usr/bin/mongod --configsvr --dbpath /home/configdb/ --port 27018 &
}

stop() {
        for a in `ps -ef | grep 27018 | awk '{print $2}'`; do kill -9 $a; done
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

We can use the scripts like below example for query_router:

#!/bin/bash

### BEGIN INIT INFO
# Provides:        MongoDB Config Server
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop MongoDB Config server
### END INIT INFO

start() {
        /usr/bin/mongos --configdb 192.168.3.187:27018 --port 27019 &
}

stop() {
        for a in `ps -ef | grep 27019 | awk '{print $2}'`; do kill -9 $a; done
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

We can't use default mongo configuration for mongos because it also need to define config database and port info in start-up.