Created
March 1, 2017 16:43
-
-
Save gogson/40683f441f199018dbbe40547aa6ea1b to your computer and use it in GitHub Desktop.
MongoDB Simple 3 Shard Cluster with 1 Config server rep set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| ulimit -n 2048 | |
| ######################################################################## | |
| ### CONSTANTS | |
| ######################################################################## | |
| MONGO_INSTANCE_CONFIG_SERVER_PORT="27017" | |
| MONGO_INSTANCE_QUERY_ROUTER_PORT="27011" | |
| MONGO_INSTANCE_SHARD_1_PORT="27012" | |
| MONGO_INSTANCE_SHARD_2_PORT="27013" | |
| MONGO_INSTANCE_SHARD_3_PORT="27014" | |
| MONGO_LOG_DIRECTORY="/data/logs" | |
| MONGO_DATA_DIRECTORY="/data/db" | |
| ######################################################################## | |
| ### HELPERS | |
| ######################################################################## | |
| PRINT_SUCCESS() { | |
| printf "$1 \033[1;32mOK\033[0m\n" | |
| } | |
| PRINT_FAIL() { | |
| printf "$1 \033[1;31mFailed\033[0m\n" | |
| } | |
| PRINT_WARNING() { | |
| printf "\033[33m$1\033[0m\n" | |
| } | |
| PRINT_ERROR() { | |
| printf "\033[1;31m$1\033[0m\n" | |
| } | |
| ######################################################################## | |
| ### CORE | |
| ######################################################################## | |
| printf "\033[1;30m========================================================\033[0m\n" | |
| printf "\033[1;30mMONGODB SHARDED CLUSTER START\033[0m\n" | |
| printf "\033[1;30m========================================================\033[0m\n\n" | |
| printf "[CLUSTER] Starting MongoDB config server..." | |
| sudo mongod --configsvr --replSet frontConfigRep --port $MONGO_INSTANCE_CONFIG_SERVER_PORT --fork --logpath $MONGO_LOG_DIRECTORY/config0.log > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "Error launching the server, try manually." | |
| exit 1; | |
| fi | |
| # wait a bit for the first server to come up | |
| sleep 5 | |
| # call rs.initiate({...}) | |
| cfg="{ | |
| _id: 'frontConfigRep', | |
| members: [ | |
| {_id: 1, host: 'localhost:$MONGO_INSTANCE_CONFIG_SERVER_PORT'}, | |
| ] | |
| }" | |
| printf "[CLUSTER] Initiate replicate for config server..." | |
| sudo mongo localhost:$MONGO_INSTANCE_CONFIG_SERVER_PORT --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : $cfg}))" > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi | |
| printf "[CLUSTER] Starting MongoDB entry point (query router)..." | |
| sudo mongos --configdb frontConfigRep/localhost:$MONGO_INSTANCE_CONFIG_SERVER_PORT --port $MONGO_INSTANCE_QUERY_ROUTER_PORT --fork --logpath $MONGO_LOG_DIRECTORY/configdb.log > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi | |
| printf "[CLUSTER] Starting MongoDB sharding instance #1..." | |
| sudo mongod --shardsvr --port $MONGO_INSTANCE_SHARD_1_PORT --dbpath $MONGO_DATA_DIRECTORY/s0 --fork --logpath $MONGO_LOG_DIRECTORY/shard0.log > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi | |
| printf "[CLUSTER] Starting MongoDB sharding instance #2..." | |
| sudo mongod --shardsvr --port $MONGO_INSTANCE_SHARD_2_PORT --dbpath $MONGO_DATA_DIRECTORY/s1 --fork --logpath $MONGO_LOG_DIRECTORY/shard1.log > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi | |
| printf "[CLUSTER] Starting MongoDB sharding instance #3..." | |
| sudo mongod --shardsvr --port $MONGO_INSTANCE_SHARD_3_PORT --dbpath $MONGO_DATA_DIRECTORY/s2 --fork --logpath $MONGO_LOG_DIRECTORY/shard2.log > /dev/null | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi | |
| printf "[CLUSTER] Registering shard cluster..." | |
| mongo --port $MONGO_INSTANCE_QUERY_ROUTER_PORT > /dev/null <<EOF | |
| sh.addShard("localhost:$MONGO_INSTANCE_SHARD_1_PORT"); | |
| sh.addShard("localhost:$MONGO_INSTANCE_SHARD_2_PORT"); | |
| sh.addShard("localhost:$MONGO_INSTANCE_SHARD_3_PORT"); | |
| EOF | |
| if [[ $? -eq 0 ]];then | |
| PRINT_SUCCESS "" | |
| else | |
| PRINT_FAIL "" | |
| exit 1; | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment