Skip to content

Instantly share code, notes, and snippets.

@matthewtckr
Last active May 7, 2019 11:54
Show Gist options
  • Save matthewtckr/354d0ba521c22feff3cc to your computer and use it in GitHub Desktop.
Save matthewtckr/354d0ba521c22feff3cc to your computer and use it in GitHub Desktop.
MongoDB Sharding Example
REM
REM Setup a MongoDB Sharded Cluster
REM
REM Reference: http://cookbook.mongodb.org/operations/convert-replica-set-to-replicated-shard-cluster/
REM More Info: http://www.kchodorow.com/blog/2010/08/09/sharding-and-replica-sets-illustrated/
REM
REM Download and Unpack MongoDB Software
REM
SET URL="https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.11.zip"
SET ZIPFILENAME=C:\mongodb-win32-x86_64-2008plus-2.6.11.zip
SET MONGOHOME=C:\mongodb-win32-x86_64-2008plus-2.6.11
bitsadmin /transfer DownloadMongo /priority normal %URL% %ZIPFILENAME%
cd \
rmdir /s /q %MONGOHOME%
jar -xf %ZIPFILENAME%
REM Setup Data Directories
REM
SET MONGODATADIR="%MONGOHOME%\data"
mkdir %MONGODATADIR%
mkdir %MONGOHOME%\logs
mkdir "%MONGODATADIR%\replica1node1" "%MONGODATADIR%\replica1node2" "%MONGODATADIR%\replica1node3"
mkdir "%MONGODATADIR%\replica2node1" "%MONGODATADIR%\replica2node2" "%MONGODATADIR%\replica2node3"
mkdir "%MONGODATADIR%\replica3node1" "%MONGODATADIR%\replica3node2" "%MONGODATADIR%\replica3node3"
mkdir "%MONGODATADIR%\config1" "%MONGODATADIR%\config2" "%MONGODATADIR%\config3"
cd %MONGOHOME%\bin
REM Start Mongo Servers
REM
start "Mongo R1N1" /b mongod.exe --dbpath ..\data\replica1node1 --port 10001 --replSet replica1 --oplogSize 700 > %MONGOHOME%\logs\mongod_r1n1.log 2>&1
start "Mongo R1N2" /b mongod.exe --dbpath ..\data\replica1node2 --port 10002 --replSet replica1 --oplogSize 700 > %MONGOHOME%\logs\mongod_r1n2.log 2>&1
start "Mongo R1N3" /b mongod.exe --dbpath ..\data\replica1node3 --port 10003 --replSet replica1 --oplogSize 700 > %MONGOHOME%\logs\mongod_r1n3.log 2>&1
start "Mongo R2N1" /b mongod.exe --dbpath ..\data\replica2node1 --port 10004 --replSet replica2 --oplogSize 700 > %MONGOHOME%\logs\mongod_r2n1.log 2>&1
start "Mongo R2N2" /b mongod.exe --dbpath ..\data\replica2node2 --port 10005 --replSet replica2 --oplogSize 700 > %MONGOHOME%\logs\mongod_r2n2.log 2>&1
start "Mongo R2N3" /b mongod.exe --dbpath ..\data\replica2node3 --port 10006 --replSet replica2 --oplogSize 700 > %MONGOHOME%\logs\mongod_r2n3.log 2>&1
start "Mongo R3N1" /b mongod.exe --dbpath ..\data\replica3node1 --port 10007 --replSet replica3 --oplogSize 700 > %MONGOHOME%\logs\mongod_r3n1.log 2>&1
start "Mongo R3N2" /b mongod.exe --dbpath ..\data\replica3node2 --port 10008 --replSet replica3 --oplogSize 700 > %MONGOHOME%\logs\mongod_r3n2.log 2>&1
start "Mongo R3N3" /b mongod.exe --dbpath ..\data\replica3node3 --port 10009 --replSet replica3 --oplogSize 700 > %MONGOHOME%\logs\mongod_r3n3.log 2>&1
REM Configure Replica Sets
REM
mongo.exe admin --port 10001 --eval "printjson(db.runCommand({'replSetInitiate' : {'_id' : 'replica1', 'members' : [{'_id' : 1, 'host' : 'localhost:10001'}, {'_id' : 2, 'host' : 'localhost:10002'}, {'_id' : 3, 'host' : 'localhost:10003'}]}}));"
mongo.exe admin --port 10004 --eval "printjson(db.runCommand({'replSetInitiate' : {'_id' : 'replica2', 'members' : [{'_id' : 1, 'host' : 'localhost:10004'}, {'_id' : 2, 'host' : 'localhost:10005'}, {'_id' : 3, 'host' : 'localhost:10006'}]}}));"
mongo.exe admin --port 10007 --eval "printjson(db.runCommand({'replSetInitiate' : {'_id' : 'replica3', 'members' : [{'_id' : 1, 'host' : 'localhost:10007'}, {'_id' : 2, 'host' : 'localhost:10008'}, {'_id' : 3, 'host' : 'localhost:10009'}]}}));"
REM Start Configuration Servers
REM
start "Mongo Config 1" /b mongod.exe --configsvr --dbpath ..\data\config1 --port 20001 > %MONGOHOME%\logs\mongod_config1.log 2>&1
start "Mongo Config 2" /b mongod.exe --configsvr --dbpath ..\data\config2 --port 20002 > %MONGOHOME%\logs\mongod_config2.log 2>&1
start "Mongo Config 3" /b mongod.exe --configsvr --dbpath ..\data\config3 --port 20003 > %MONGOHOME%\logs\mongod_config3.log 2>&1
REM Start Mongo Router
REM
start "Mongo Router" /b mongos.exe --configdb localhost:20001,localhost:20002,localhost:20003 --port 27017 --chunkSize 1 > %MONGOHOME%\logs\mongos.log 2>&1
REM Enable Sharding
REM
mongo.exe admin --port 27017 --eval "printjson(db.runCommand( { addshard : 'replica1/localhost:10001,localhost:10002,localhost:10003' } ));"
mongo.exe admin --port 27017 --eval "printjson(db.runCommand( { addshard : 'replica2/localhost:10004,localhost:10005,localhost:10006' } ));"
mongo.exe admin --port 27017 --eval "printjson(db.runCommand( { addshard : 'replica3/localhost:10007,localhost:10008,localhost:10009' } ));"
mongo.exe admin --port 27017 --eval "printjson(db.runCommand({listshards:1}));"
mongo.exe admin --port 27017 --eval "printjson(db.runCommand( { enablesharding : 'test' } ));"
REM Create Sample Collection for Sharding
REM
mongo.exe test --port 27017 --eval "printjson(db.createCollection('test_collection'));"
mongo.exe test --port 27017 --eval "printjson(db.test_collection.ensureIndex({number:1}));"
mongo.exe admin --port 27017 --eval "printjson(db.runCommand({shardcollection:'test.test_collection', key:{'number':1}}));"
REM Load Collection with random data
REM
echo people = ['Marc', 'Bill', 'George', 'Eliot', 'Matt', 'Trey', 'Tracy', 'Greg', 'Steve', 'Kristina', 'Katie', 'Jeff']; > ..\test_collection.js
echo for(var i=0; i^<100000; i++){ >> ..\test_collection.js
echo name = people[Math.floor(Math.random()*people.length)]; >> ..\test_collection.js
echo user_id = i; >> ..\test_collection.js
echo boolean = [true, false][Math.floor(Math.random()*2)]; >> ..\test_collection.js
echo added_at = new Date(); >> ..\test_collection.js
echo number = Math.floor(Math.random()*10001); >> ..\test_collection.js
echo db.test_collection.save({'name':name, 'user_id':user_id, 'boolean': boolean, 'added_at':added_at, 'number':number }); >> ..\test_collection.js
echo }; >> ..\test_collection.js
start mongo.exe test --port 27017 ..\test_collection.js
REM Check on Loading status
REM
mongo.exe test --port 27017 --eval "print('Rows: ' + db.test_collection.count());printjson(db.printShardingStatus())"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment