Initiate scripts for mongos and configuration server for MongoDB sharding

I created a crawl in the local environment for testing purposes.

I have three configuration servers on 1 machine and 1 query router on one computer and two data nodes on two different machines.

Everything works fine, but the problem is that I can not keep all processes active on different ports, since I have no start / stop script. I run processes on the command line with & at the end to make it active, which is a very bad way to keep processes active, and someday they will automatically die.

Please help or provide a way to use scripts, and the script can also handle various ports for the entire process to be active on a separate machine.

+6
source share
2 answers

For the configuration server, we can use the script below:

 #!/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 scripts like the one below 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 cannot use the default mongo configuration for mongos, since it must also determine the configuration database and port information at startup.

0
source

You should never try to manage multiple instances from the same init script, as this creates a whole redundant administrative work when you start / end any of the instances.

You should look for one init script for each individual instance of the database process, as this is the best Linux practice.

For the most part, you should be able to use the generic MongoDB provided by the init script, and then make a renamed copy for each database instance.

Then you must create separate configuration files for each instance, which should contain slightly different configurations and run each instance on its own port, its own dbpath and its own log file.

You can then specify each init script in the configuration file for your instance, and everything should work as planned.

Finally, in MongoDB you should use the --fork option, which provides a safe way to separate MongoDB from the shell instance. If you need to continue using shell fork (the and operator), then you should migrate MongoDB to "nohup" to avoid closing the instance by terminating your shell. It will look something like this: nohup <mongodb cmd and arguments> &

Edit: You can use the same process to use the same initialization scripts to start MongoS. You need to find the line that sets the mongod binary as the one to be executed. Under all debian derivatives, this will be the DAEMON variable. Change this to point to mongos instead of mongod and exit.

+6
source

All Articles