Last active
August 29, 2015 14:16
-
-
Save scott4dev/1dea2f60844b4517ee22 to your computer and use it in GitHub Desktop.
Deploy a Sharded 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 2 db instances | |
mongod --port 27010 --dbpath data/s-0 | |
mongod --port 27011 --dbpath data/s-1 | |
//start the config server | |
mongod --configsvr --dbpath data/configsvr --port 27015 | |
//start the router | |
mongos --configdb localhost:27015 --port 27017 | |
//add 2 shard | |
mongo --host localhost --port 27017 | |
sh.addShard("localhost:27010") | |
sh.addShard("localhost:27011") | |
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 }) | |
//distribute data between shards using tags | |
sh.addShardTag("shard0000", "DC1") | |
sh.addShardTag("shard0001", "DC2") | |
sh.addTagRange("sharded_test_db.users", { zipcode: 0 }, { zipcode: 29999 }, "DC1") | |
sh.addTagRange("sharded_test_db.users", { zipcode: 30000 }, { zipcode: 39999 }, "DC2") | |
//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