Created
September 18, 2013 17:36
-
-
Save kamituel/6612658 to your computer and use it in GitHub Desktop.
Bash script to create MongoDB replica 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/sh | |
MONGO=../mongo/bin | |
NUMBER_OF_NODES=$1 | |
REPLICA_NAME=rs | |
PORT=27017 | |
if [[ -z "$1" ]]; | |
then | |
NUMBER_OF_NODES=3 | |
fi | |
mkdir -p $(eval echo data/rs{0..$NUMBER_OF_NODES}) | |
for ((i=0; i < $NUMBER_OF_NODES; i++)); | |
do | |
$MONGO/mongod --replSet $REPLICA_NAME --logpath $REPLICA_NAME.$i.log --dbpath data/rs$i --port $(($PORT+i)) --fork | |
done | |
sleep 2 | |
CONFIG="var config = { _id: \"$REPLICA_NAME\", members: [" | |
for ((i=0; i < $NUMBER_OF_NODES; i++)); | |
do | |
CONFIG+="{ _id: $i, host: \"localhost:$(($PORT+i))\" }" | |
if [[ $((i+1)) -lt $NUMBER_OF_NODES ]]; | |
then | |
CONFIG+=", " | |
fi | |
done | |
CONFIG+="]};" | |
CONFIG+="rs.initiate(config);" | |
$MONGO/mongo <<< $CONFIG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment