Tuesday, November 5, 2013

MongoDB: Sharding Database

#start shardsrv
mongod --shardsvr --dbpath ./data/db --port 27018 --smallfiles --logappend
#start configsrv
with --configsvr specified the default port for listening becomes 27019 and the default data directory /data/configdb. Wherever your data directory is, it is suggested that you verify that the directory is empty before you begin.
mongod --configsvr --dbpath ./data/configdb --smallfiles --logappend
#start mongos
mongos --configdb localhost:27019


#connect mongos
mongo
sh.addShard("a/localhost:27018")

to shard a collection, you must have an index on the shard key, so you will need to create the index first


db.trades.ensureIndex( { ticker:1, time:1 } )
sh.enableSharding("week6")
sh.shardCollection("week6.trades",{ticker:1,time:1},false)
use config
db.chunks.find()
db.chunks.find({}, {min:1,max:1,shard:1,_id:0,ns:1})



#start new monogd
mkdir ./data/db2
mongod --shardsvr --dbpath ./data/db2 --port 27020 --smallfiles --logappend
#add Shard in mongos prompt
sh.addShard("a/localhost:27020")
Now wait for the balancer to move data among the two shards more evenly. Periodically run: use config
db.chunks.find( { ns:"week6.trades" }, {min:1,max:1,shard:1,_id:0} ).sort({min:1})
db.chunks.aggregate( [
 { $match : { ns : "week6.trades" } } ,
 { $group : { _id : "$shard", n : { $sum : 1 } } }
] )

No comments:

Post a Comment