Reputation: 18459
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.
Upvotes: 7
Views: 2246
Reputation: 18459
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.
Upvotes: 0
Reputation: 3696
You shouldn't never be looking to drive multiple instances from one init script, as this creates a whole amount of excess administrative work when you are doing startup/shutdown of any one of the instances.
You should look to have one init script for each individual database process instance as this is Linux best practice.
For the most part you should be able to use the generic MongoDB provided init script and then make a renamed copy for each database instance.
You should then create individual config files for each instance, which should contain slightly different configurations and run each instance on its own port, own dbpath and own logfile.
You can then point each init script at the config file for its instance and everything should work as planned.
Finally, within MongoDB, you should use the --fork option, which provides a safe way to detach MongoDB from a shell instance. If you absolutely must continue using shell fork (the & operator) then you should wrap MongoDB in "nohup" to avoid having the instance closed by a termination of your shell. This would look something like:
nohup <mongodb cmd and arguments> &
Edit:
You can use the same process to use the same init scripts to launch MongoS. You need to find the line which sets the mongod
binary as the one to be executed. Under all of the debian derivatives this would be the DAEMON
variable. Change this to point to mongos
instead of mongod
and off you go.
Upvotes: 6