Skip to content

Instantly share code, notes, and snippets.

@kamituel
Created September 18, 2013 17:36
Show Gist options
  • Save kamituel/6612658 to your computer and use it in GitHub Desktop.
Save kamituel/6612658 to your computer and use it in GitHub Desktop.
Bash script to create MongoDB replica set.
#!/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