Skip to content

Instantly share code, notes, and snippets.

@knennigtri
Last active April 28, 2017 05:23
Show Gist options
  • Save knennigtri/aef296ae4e7934ef8cc4 to your computer and use it in GitHub Desktop.
Save knennigtri/aef296ae4e7934ef8cc4 to your computer and use it in GitHub Desktop.
Initializa and start a Mongo replset
##
# This script initializes and starts a Mongo replset on your local machine. All servers
# bind to your hostname for the replset. This script assumes the following folder structure
#
# + Top level folder
# - mongo-demo.sh
# + MONGODB_PATH[i]
# + db
# + logs
#
# Author:
# Kevin Nennig
# knennig213@gmail.com
#
# Version 1.0.0
#
##
#if you don't have mongo in your path, you'll need to set MONGO_BIN
MONGO_BIN=
BIND_IP=<Hostname or IP>
REPLSET=aem
MONGODB_PATH[0]=mongodb1
MONGODB_PATH[1]=mongodb2
MONGODB_PATH[2]=mongodb3
PORT[0]=27017
PORT[1]=27018
PORT[2]=27019
for ((i = 0; i < ${#MONGODB_PATH[@]}; i++))
do
echo "###creating mongodb ${BIND_IP}:${PORT[i]}"
${MONGO_BIN}mongod --bind_ip ${BIND_IP} --logpath ${MONGODB_PATH[i]}/logs/mongo.log --dbpath ${MONGODB_PATH[i]}/db --replSet ${REPLSET} --port ${PORT[i]} --fork
done
#wait for mongodbs to come online
echo "###waiting 5 seconds for servers to come online..."
sleep 5
#Connect to master and initiate the replset
echo "###initiating the replset ${REPLSET}"
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.initiate())"
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.conf())"
#Wait for the replset to come online
echo "###waiting 5 seconds for replset to initialize"
sleep 5
#Add remaining mongo servers to replset, this assumes all instances are at the same BIND_IP
for ((j = 1; j < ${#MONGODB_PATH[@]}; j++))
do
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.add('${BIND_IP}:${PORT[j]}'))"
done
# Check Conf to make sure that everything is right
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.conf())"
# Check Status of replication
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.status())"
##
# This script starts a previously existing Mongo replset on your local machine. All servers
# bind to your hostname for the replset. This script assumes the following folder structure
#
# + Top level folder
# - mongo-demo.sh
# + MONGODB_PATH[i]
# + db
# + logs
#
# Author:
# Kevin Nennig
# knennig213@gmail.com
#
# Version 1.0.0
#
##
#if you don't have mongo in your path, you'll need to set MONGO_BIN
MONGO_BIN=
BIND_IP=<Hostname or IP>
REPLSET=aem
MONGODB_PATH[0]=mongodb-27017
MONGODB_PATH[1]=mongodb-27018
MONGODB_PATH[2]=mongodb-27019
PORT[0]=27017
PORT[1]=27018
PORT[2]=27019
for ((i = 0; i < ${#MONGODB_PATH[@]}; i++))
do
echo "###creating mongodb ${BIND_IP}:${PORT[i]}"
${MONGO_BIN}mongod --bind_ip ${BIND_IP} --logpath ${MONGODB_PATH[i]}/logs/mongo.log --dbpath ${MONGODB_PATH[i]}/db --replSet ${REPLSET} --port ${PORT[i]} --fork
done
#wait for mongodbs to come online
echo "###waiting 5 seconds for servers to come online..."
sleep 5
# Check Status of replication
${MONGO_BIN}mongo ${BIND_IP}:${PORT[0]} --eval "printjson(rs.status())"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment