Skip to content

Instantly share code, notes, and snippets.

@jewzaam
Last active August 29, 2015 14:10
Show Gist options
  • Save jewzaam/2de9c1bb79758d5bb010 to your computer and use it in GitHub Desktop.
Save jewzaam/2de9c1bb79758d5bb010 to your computer and use it in GitHub Desktop.
MongoDB shard in docker
# setup:
# - one config server
# - one mongos server
# - 3 shard nodes (single mongod)
# reference: https://gist.github.com/achille/82847acc0b4b94ca9f0f
# start config server (note, override default db path to one already managed by mongo image)
docker run --name mongo-shard-config -d mongo --nojournal --oplogSize 10 --configsvr --dbpath /data/db
# start shard mongod
docker run --name mongo-shard-mongod-1 -d mongo --nojournal --oplogSize 10
docker run --name mongo-shard-mongod-2 -d mongo --nojournal --oplogSize 10
docker run --name mongo-shard-mongod-3 -d mongo --nojournal --oplogSize 10
# start mongos
SHARD_CONFIG_IPADDR=`docker inspect mongo-shard-config | grep IPAddress | sed 's/.*: "\([^"]*\).*/\1/g'`
docker run --name mongo-shard-mongos -d mongo sh -c "mongos --configdb $SHARD_CONFIG_IPADDR"
# add shard members, enable sharding on 'test' database, and setup shard for collection 'foo'
docker run -it --link mongo-shard-mongos:mongos --link mongo-shard-mongod-1:mongo1 --link mongo-shard-mongod-2:mongo2 --link mongo-shard-mongod-3:mongo3 --rm mongo /bin/bash
# find all IP's for later commands
MONGO1=`grep mongo1 /etc/hosts | awk '{print $1}'`
MONGO2=`grep mongo2 /etc/hosts | awk '{print $1}'`
MONGO3=`grep mongo3 /etc/hosts | awk '{print $1}'`
mongo mongos:27017 --eval "sh.addShard( '$MONGO1:27017' ); sh.addShard( '$MONGO2:27017' ); sh.addShard( '$MONGO3:27017' );"
mongo mongos:27017 --eval "sh.enableSharding('test'); sh.shardCollection("test.foo",{_id:1});"
//insert data to test
for(i=0;i<10000;i++){ db.foo.insert({_id:i}) }
//manually split (optional)
sh.splitAt("test.foo",{_id:5000});
exit
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment