#### WARNING: This documenmt needs a run through and a few things left too. # I am going to assume we are not using thetemporary # Mongo servers and they are not running. # This will delete them. killall mongo mongod mongos sleep 2 killall -9 mongo mongod mongos # If restarting, kill the mogo processes and then do rm -rf mongo/d1 mongo/d2 mongo/d3 mongo/logs # Setup 3 directories: mkdir -p mongo/d1 mongo/d2 mongo/d3 mongo/logs # If not AWS, ignore or already did it. #sudo -s -- bash -c ' echo "127.0.0.1 localhost ">> /etc/hosts ' # Start the mongod with shard option. mongod --shardsvr --dbpath mongo/d2 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 > mongo/logs/2.log 2>&1 & sleep 1 # Start a mongo config server --- in the Sharding2 document, we will # have 3 in a replica set. mongod --configsvr --replSet c --dbpath mongo/d1 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 > mongo/logs/1.log 2>&1 & sleep 1 # Stupid but single config server still has to be in a replica set. mongo --port 27019 --eval "rs.initiate({ _id: 'c', version: 1, members: [ {_id :0, host: 'localhost:27019' } ] })" sleep 2 # Start a mongos -- this connects to the mongod, since the default for a config server is 27019 mongos --configdb "c/127.0.0.1:27019" --bind_ip 127.0.0.1 > mongo/logs/ms_1.log 2>&1 & sleep 3 # Start a mongo, which will connect to mongos to execute commands # Change the data server to a shard server. mongo --eval 'sh.addShard( "localhost:27018" )' sleep 1 #----------------------------------------------------------- #Now add a 2nd shard. mongod --shardsvr --dbpath mongo/d3 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 --port 4000 > mongo/logs/3.log 2>&1 & sleep 1 mongo --eval 'sh.addShard( "localhost:4000" )' sleep 1 # shard1 should be shard0000 # shard2 should be shard0001 # Let's tag the shards mongo --eval 'sh.addShardTag("shard0000", "TAG1")' mongo --eval 'sh.addShardTag("shard0001", "TAG2")' sleep 1 #Shard the database. mongo --eval 'sh.enableSharding("test")' # Add an index which is required for sharding. mongo --eval 'db.no.createIndex( { value : 1} )' test # shard the collection. mongo --eval 'sh.shardCollection("test.no", {value: 1})' mongo --eval 'sh.addTagRange("test.no", { value: 100 }, { value: 1000 }, "TAG1")' mongo --eval 'sh.addTagRange("test.no", { value: 1001 }, { value: 2000 }, "TAG2")' # Lets add some data. echo " function fillData() { db = db.getSiblingDB('test') for( var i = 0; i < 10000; i++ ) { db.no.insert( {value: i } ); } } fillData() " > mongo/fill.js mongo mongo/fill.js # Count the two shards individually mongo --quiet --port 27018 test --eval "db.no.count()" mongo --quiet --port 4000 test --eval "db.no.count()" mongo --quiet --port 27018 test --eval 'db.no.aggregate({ $group : { _id: null, max: { $max : "$value" }}});' mongo --quiet --port 4000 test --eval 'db.no.aggregate({ $group : { _id: null, max: { $max : "$value" }}});' mongo --quiet --port 27018 test --eval 'db.no.find({}, {_id:0, value:1}).sort({value:-1}).limit(1);' mongo --quiet --port 4000 test --eval 'db.no.find({}, {_id:0, value:1}).sort({value:-1}).limit(1);' mongo --quiet --port 27018 test --eval 'db.no.find({}, {_id:0, value:1}).sort({value:1}).limit(1);' mongo --quiet --port 4000 test --eval 'db.no.find({}, {_id:0, value:1}).sort({value:1}).limit(1);' # If the max and min don't make sense, its because of the weird tagging I did. # For some reason it technically doesn't know what to do with the lower and upper bound. # { "value" : { "$minKey" : 1 } } -->> { "value" : 100 } on : shard0001 Timestamp(2, 1) # { "value" : 100 } -->> { "value" : 1000 } on : shard0000 Timestamp(2, 0) # { "value" : 1000 } -->> { "value" : 1001 } on : shard0001 Timestamp(1, 3) # { "value" : 1001 } -->> { "value" : 2000 } on : shard0001 Timestamp(1, 4) # { "value" : 2000 } -->> { "value" : { "$maxKey" : 1 } } on : shard0001 Timestamp(1, 5) # tag: TAG1 { "value" : 100 } -->> { "value" : 1000 } # tag: TAG2 { "value" : 1001 } -->> { "value" : 2000 } mongo --quiet --eval "sh.status()" ##### add another shard, tag, and range and see what happens #----------------------------------------------------------- #Now add a 2rd shard. rm -rf mongo/d4 # Setup 3 directories: mkdir -p mongo/d4 mongod --shardsvr --dbpath mongo/d4 --smallfiles --oplogSize 50 --bind_ip 127.0.0.1 --port 4001 > mongo/logs/4.log 2>&1 & sleep 2 mongo --eval 'sh.addShard( "localhost:4001" )' sleep 1 mongo --eval 'sh.addShardTag("shard0002", "TAG3")' sleep 1 mongo --eval 'sh.addTagRange("test.no", { value: 2001 }, { value: 3000 }, "TAG3")' # the min and max values get weird still. # { "value" : { "$minKey" : 1 } } -->> { "value" : 100 } on : shard0002 Timestamp(3, 0) # { "value" : 100 } -->> { "value" : 1000 } on : shard0000 Timestamp(2, 0) # { "value" : 1000 } -->> { "value" : 1001 } on : shard0001 Timestamp(4, 1) # { "value" : 1001 } -->> { "value" : 2000 } on : shard0001 Timestamp(1, 4) # { "value" : 2000 } -->> { "value" : 2001 } on : shard0001 Timestamp(3, 2) # { "value" : 2001 } -->> { "value" : 3000 } on : shard0002 Timestamp(4, 0) # { "value" : 3000 } -->> { "value" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 4) # tag: TAG1 { "value" : 100 } -->> { "value" : 1000 } # tag: TAG2 { "value" : 1001 } -->> { "value" : 2000 } # tag: TAG3 { "value" : 2001 } -->> { "value" : 3000 } # Count the shards individually mongo --quiet --port 27018 test --eval "db.no.count()" mongo --quiet --port 4000 test --eval "db.no.count()" mongo --quiet --port 4001 test --eval "db.no.count()"