Created
March 1, 2015 16:50
-
-
Save scott4dev/5738624b8a16259220a8 to your computer and use it in GitHub Desktop.
Deploy a Sharded and replicated database for testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//start mongod instances for replica set rs0 | |
mongod --port 27010 --dbpath data/rs0-0 --replSet rs0 --smallfiles --oplogSize 128 | |
mongod --port 27011 --dbpath data/rs0-1 --replSet rs0 --smallfiles --oplogSize 128 | |
//add primary | |
mongo --port 27010 | |
rsconf = { | |
_id: "rs0", | |
members: [ | |
{ | |
_id: 0, | |
host: "localhost:27010" | |
} | |
] | |
} | |
rs.initiate( rsconf ) | |
rs.conf() | |
//add secondaries | |
rs.add("localhost:27011") | |
//set 0 priority | |
cfg = rs.conf() | |
cfg.members[1].priority = 0 | |
rs.reconfig(cfg) | |
//start mongod instances for replica set rs1 | |
mongod --port 27012 --dbpath data/rs1-0 --replSet rs1 --smallfiles --oplogSize 128 | |
mongod --port 27013 --dbpath data/rs1-1 --replSet rs1 --smallfiles --oplogSize 128 | |
//add primary | |
mongo --port 27012 | |
rsconf = { | |
_id: "rs1", | |
members: [ | |
{ | |
_id: 0, | |
host: "localhost:27012" | |
} | |
] | |
} | |
rs.initiate( rsconf ) | |
rs.conf() | |
//add secondaries | |
rs.add("localhost:27013") | |
//set 0 priority | |
cfg = rs.conf() | |
cfg.members[1].priority = 0 | |
rs.reconfig(cfg) | |
//start the config server | |
mongod --configsvr --dbpath data/configsvr-0 --port 27015 | |
//start the router | |
mongos --configdb localhost:27015 --port 27017 | |
//add shards | |
mongo --port 27017 | |
sh.addShard( "rs0/localhost:27010" ) | |
sh.addShard( "rs1/localhost:27012" ) | |
//add tags to distribute data between shards | |
sh.addShardTag("rs0", "DATACENTER0") | |
sh.addShardTag("rs1", "DATACENTER1") | |
sh.enableSharding("sharded_test_db") | |
sh.shardCollection("sharded_test_db.users", { "zipcode": 1 } ) | |
sh.shardCollection("sharded_test_db.test_collection", { "_id": "hashed" } ) | |
use sharded_test_db | |
db.users.insert({name:"G", "zipcode": 25030 }) | |
db.users.insert({name:"P", "zipcode": 25030 }) | |
db.users.insert({name:"A", "zipcode": 25030 }) | |
db.users.insert({name:"S", "zipcode": 25030 }) | |
db.users.insert({name:"X", "zipcode": 31230 }) | |
db.users.insert({name:"Y", "zipcode": 31230 }) | |
db.users.insert({name:"Z", "zipcode": 31230 }) | |
db.users.insert({name:"SA", "zipcode": 20030 }) | |
sh.addTagRange("sharded_test_db.users", { zipcode: 0 }, { zipcode: 29999 }, "DATACENTER0") | |
sh.addTagRange("sharded_test_db.users", { zipcode: 30000 }, { zipcode: 39999 }, "DATACENTER1") | |
//verify data distribution | |
use config | |
db.chunks.find({ns: "sharded_test_db.users"}, {shard:1, _id:0}).sort({shard:1}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment