Create two shards with replica set of 3 nodes in Mongo (windows BAT file, but probably easly changeable to .sh)
#!/bin/bash
# shell script to create a simple mongodb sharded cluster locally.
# Requires a replica set to be already running, you can run
# start-mongo-replset.sh first to start a replica set.
set -e
red=$(tput setaf 1)
green=$(tput setaf 2)
default=$(tput sgr0)
function finish {
pid=`cat ~/mongosvr/shard-config-0.pid`
kill $pid
wait $pid
}
trap finish EXIT
mkdir -p ~/mongosvr/config-0
# start up the mongodb config server for the shards
mongod --configsvr --dbpath ~/mongosvr/config-0 --port 27019 \
--config . --pidfilepath ~/mongosvr/shard-config-0.pid 2>&1 | sed "s/.*/$red&$default/" &
sleep 3
mongos --configdb localhost:27019 | sed "s/.*/$green&$default/" &
sleep 3
# add the first replica set instance as a shard, the others will be discovered automatically by mongos
mongo --eval "JSON.stringify(sh._adminCommand( { addShard : 'set/localhost:27091' } , true ))"
# enable sharding on the test database
mongo --eval "JSON.stringify(sh._adminCommand( { enableSharding : 'test' } ))"
# sleep forever
cat
#!/bin/bash
# shell script to create a simple mongodb replica set (tested on osx)
set -e
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
default=$(tput sgr0)
function finish {
pids=(`cat ~/mongosvr/rs-*.pid`)
for pid in "${pids[@]}"
do
kill $pid
wait $pid
done
}
trap finish EXIT
mkdir -p ~/mongosvr/rs-0
mkdir -p ~/mongosvr/rs-1
mkdir -p ~/mongosvr/rs-2
mongod --shardsvr --dbpath ~/mongosvr/rs-0 --replSet set --rest --port 27091 \
--config . --pidfilepath ~/mongosvr/rs-0.pid 2>&1 | sed "s/.*/$red&$default/" &
mongod --shardsvr --dbpath ~/mongosvr/rs-1 --replSet set --rest --port 27092 \
--config . --pidfilepath ~/mongosvr/rs-1.pid 2>&1 | sed "s/.*/$green&$default/" &
mongod --shardsvr --dbpath ~/mongosvr/rs-2 --replSet set --rest --port 27093 \
--config . --pidfilepath ~/mongosvr/rs-2.pid 2>&1 | sed "s/.*/$yellow&$default/" &
# wait a bit for the first server to come up
sleep 5
# call rs.initiate({...})
cfg="{
_id: 'set',
members: [
{_id: 1, host: 'localhost:27091'},
{_id: 2, host: 'localhost:27092'},
{_id: 3, host: 'localhost:27093'}
]
}"
mongo localhost:27091 --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : $cfg}))"
# sleep forever
cat
#!/bin/bash
#
#Creates and initializes a MongoDb replica set: allows the user to choose the number of members and the starting port number; this should also work on a remote host, but I have only tested it so far on localhost
# Starts up a MongoDB replica set
#
# There is a lot of documentation about replica sets:
#
# http://docs.mongodb.org/manual/reference/replica-configuration/
# http://docs.mongodb.org/manual/administration/replica-sets/
#
# To read data from a SECONDARY, when in the client, use:
#
# > rs.slaveOk()
#
# Created by M. Massenzio, 2012-05-10
# Prints usage
function usage {
echo "Usage: `basename $0` replicaSetName [num] [port] [host]
Creates a replica set of 'num' MongoDB servers, running on consecutive ports
starting at 'port' on the same 'host' ('localhost' by default).
By default, it will start 3 servers, on ports {27011, 27012, 27013}.
Due to bash limited ability to parse command-line args, each argument MUST
be present, if any of the subsequent ones need to be specified.
The replicaSetName is required and can be used when connecting to
the set, using a URI of the form:
mongodb://host:27011,host:27012,host:27013?replicaSet='replicaSetName'
"
}
# Creates a replica set member's record for the configuration
#
# Arguments:
# $1 id
# $2 host
function create_record {
echo "{ \"_id\": $1, \"host\": \"$2\"}"
}
# Initialize the replica set
#
# Arguments:
# $1 primary mongod port (uses global: HOST)
# $2 replica set configuration record
function initialize_repset {
if [[ -z "$2" ]]; then
echo "No configuration record passed"
exit 1
fi
local CFG_REC="$2"
echo -e "rsconf = ${CFG_REC}\nrs.initiate(rsconf)" | \
mongo --port $1 --host ${HOST} > /dev/null
}
# Waits for the state of all mongo servers to get past the STARTUP phases
#
# Arguments:
# $1 a port for the mongo client to connect to (default: 27011)
# $2 the host to connect to (default: localhost)
function wait_for_repset {
local PORT=${1:-27011}
local H=${2:-localhost}
while true; do
num=$(echo "rs.status()" | mongo --port $PORT --host $H | grep stateStr | grep -v STARTUP | wc -l)
if [[ $(expr $num) == ${NUM_REPLICAS} ]]; then
return
fi
sleep 1
done
}
# Main body of script starts here
# TODO: factor out sub-sections in their own functions
if [[ -z "$1" ]]; then
usage
exit 1
fi
declare -r REPSET_NAME="$1"
declare -r NUM_REPLICAS=${2:-3}
declare -r STARTING_PORT=${3:-27011}
declare -r HOST=${4:-localhost}
echo "[INFO] Starting ${NUM_REPLICAS} MongoDB servers on ${HOST}, " \
" using ports starting at ${STARTING_PORT};" \
" the replica set will be named: ${REPSET_NAME}"
read -p "Do you want to continue (y/N)? " resp
if [[ $resp != 'y' ]]; then
echo "Terminating."
exit 1
fi
if [[ -z ${MONGO_BASE} ]]; then
echo "[WARN] Please define \$MONGO_BASE to point to where you want data/logs to be created"
MONGO_BASE=${HOME}/mongod_base
read -p "Would you like to continue using the default (${MONGO_BASE})? " resp
if [[ $resp != 'y' ]]; then
echo "Terminating."
exit 1
fi
fi
echo "Using base directory for logs and data: ${MONGO_BASE}"
declare -r LOGS_DIR="${MONGO_BASE}/logs"
declare -r DATA_DIR="${MONGO_BASE}/data"
mkdir -p ${LOGS_DIR}
echo "Logs sent to ${LOGS_DIR}, data saved in ${DATA_DIR}"
if [[ ! -d ${LOGS_DIR} || ! -d ${DATA_DIR} ]]; then
echo "[ERROR] Either logs or data directory could not be found/created; aborting."
exit 1
fi
MEMBERS=""
PORT=${STARTING_PORT}
for ((i=1; i <= ${NUM_REPLICAS}; i++))
do
mkdir -p "${DATA_DIR}/${REPSET_NAME}_${i}"
LOGPATH="${LOGS_DIR}/${REPSET_NAME}_${i}.log"
DBPATH="${DATA_DIR}/${REPSET_NAME}_${i}"
mongod --rest --replSet "${REPSET_NAME}" --logpath $LOGPATH \
--dbpath $DBPATH --port ${PORT} --fork --vvvvv \
--smallfiles --oplogSize 128 > /dev/null
echo "[INFO] MongoDB server started on port $PORT"
MEMBERS="$MEMBERS $(create_record $i ${HOST}:${PORT})"
if [[ $i < ${NUM_REPLICAS} ]]; then
MEMBERS="${MEMBERS},"
fi
((PORT++))
done
CONFIG="{ _id: \"${REPSET_NAME}\", members: [${MEMBERS}]}"
echo "[INFO] Configuring replica set ${REPSET_NAME} and initializing"
initialize_repset ${STARTING_PORT} "${CONFIG}"
if [[ $? != 0 ]]; then
echo "[ERROR] could not initialize the replica set [${REPSET_NAME}]"
exit 1
fi
echo "[SUCCESS] Replica set ${REPSET_NAME} started and configured"
echo "[INFO] Waiting for replica set to sync and elect primary..."
wait_for_repset ${STARTING_PORT} ${HOST}
echo "[SUCCESS] To connect to PRIMARY execute: mongo --port ${STARTING_PORT} --host ${HOST}"
Replica set :
P1 : ( Primary )
S1 : ( Sec 1 )
A2 : ( Arbiter )
1. start mongo with replica in all instance
stop iptables
service iptables stop for all instances
check if process exist run with replica
ps -ef | grep mongo
mongod 964 1 0 05:06 ? 00:00:01 /usr/bin/mongod -f /etc/mongod.conf
stop the mongo
service mongod stop
restart with replica
mkdir /srv/mongodb/rs0-0
/usr/bin/mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128
--smallfiles --oplogSize 128 ( for testing )
waiting for a while
into mongo shell - the primary one
check replica set status
> rs.status()
if running with replSet, it should show info with need to initiate
initiate set
> cfg = {"_id" : "rs0","members" : [{"_id" : 0,"host" : "ec2-54-245-53-45.us-west-2.compute.amazonaws.com:27017"}]}
> rs.initiate(cfg)
verify
> rs.conf()
rs0:PRIMARY>
> db.isMaster()
2. add members
in other node
service iptables stop
mkdir /srv/mongodb/rs0-1
> /usr/bin/mongod --port 27017 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128
in Primary
> rs.add( "<node's public ip>:27017" )
> rs.conf()
3. add arbiter
in arbiter node
service iptables stop
mkdir /srv/mongodb/arb
> mongod --port 30000 --dbpath /srv/mongodb/arb --replSet rs0 --smallfiles --oplogSize 128
in Primary
> rs.addArb("<node's public ip>:30000")
> rs.conf()
Done
REM Andrew Erlichson - Original author
REM Jai Hirsch - Translated original .sh file to .bat
REM 10gen
REM script to start a sharded environment on localhost
echo "del data files for a clean start"
del /Q c:\data\config
del /Q c:\data\shard0
del /Q c:\data\shard1
del /Q c:\data\shard2
del /Q c:\data\config
PING 1.1.1.1 -n 1 -w 5000 >NUL
REM start a replica set and tell it that it will be a shard0
mkdir c:\data\shard0\rs0
mkdir c:\data\shard0\rs1
mkdir c:\data\shard0\rs2
start mongod --replSet s0 --dbpath c:\data\shard0\rs0 --port 37017 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s0 --dbpath c:\data\shard0\rs1 --port 37018 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s0 --dbpath c:\data\shard0\rs2 --port 37019 --shardsvr --smallfiles --oplogSize 100
PING 1.1.1.1 -n 1 -w 5000 >NUL
REM connect to one server and initiate the set
start mongo --port 37017 --eval "config = { _id: 's0', members:[{ _id : 0, host : 'localhost:37017' },{ _id : 1, host : 'localhost:37018' },{ _id : 2, host : 'localhost:37019' }]};rs.initiate(config)"
REM start a replicate set and tell it that it will be a shard1
mkdir c:\data\shard1\rs0
mkdir c:\data\shard1\rs1
mkdir c:\data\shard1\rs2
start mongod --replSet s1 --dbpath c:\data\shard1\rs0 --port 47017 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s1 --dbpath c:\data\shard1\rs1 --port 47018 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s1 --dbpath c:\data\shard1\rs2 --port 47019 --shardsvr --smallfiles --oplogSize 100
PING 1.1.1.1 -n 1 -w 5000 >NUL
start mongo --port 47017 --eval "config = { _id: 's1', members:[{ _id : 0, host : 'localhost:47017' },{ _id : 1, host : 'localhost:47018' },{ _id : 2, host : 'localhost:47019' }]};rs.initiate(config);"
REM start a replicate set and tell it that it will be a shard2
mkdir c:\data\shard2\rs0
mkdir c:\data\shard2\rs1
mkdir c:\data\shard2\rs2
start mongod --replSet s2 --dbpath c:\data\shard2\rs0 --port 57017 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s2 --dbpath c:\data\shard2\rs1 --port 57018 --shardsvr --smallfiles --oplogSize 100
start mongod --replSet s2 --dbpath c:\data\shard2\rs2 --port 57019 --shardsvr --smallfiles --oplogSize 100
PING 1.1.1.1 -n 1 -w 5000 >NUL
start mongo --port 57017 --eval "config = { _id: 's2', members:[{ _id : 0, host : 'localhost:57017' },{ _id : 1, host : 'localhost:57018' },{ _id : 2, host : 'localhost:57019' }]};rs.initiate(config)"
REM now start 3 config servers
mkdir c:\data\config\config-a
mkdir c:\data\config\config-b
mkdir c:\data\config\config-c
start mongod --dbpath c:\data\config\config-a --port 57040 --configsvr --smallfiles --oplogSize 100
start mongod --dbpath c:\data\config\config-b --port 57041 --configsvr --smallfiles --oplogSize 100
start mongod --dbpath c:\data\config\config-c --port 57042 --configsvr --smallfiles --oplogSize 100
PING 1.1.1.1 -n 1 -w 5000 >NUL
ECHO now start the mongos on a standard port
start mongos --configdb localhost:57040,localhost:57041,localhost:57042
echo "Wait 60 seconds for the replica sets to fully come online"
PING 1.1.1.1 -n 1 -w 60000 >NUL
echo "Connnecting to mongos and enabling sharding"
REM add shards and enable sharding on the test db
start mongo --eval "db.adminCommand( { addshard : 's0/'+'localhost:37017' } );db.adminCommand( { addshard : 's1/'+'localhost:47017' } );db.adminCommand( { addshard : 's2/'+'localhost:57017' } );db.adminCommand({enableSharding: 'test'});db.adminCommand({shardCollection: 'test.grades', key: {student_id:1}});"