简体   繁体   中英

Sharding in a replicaset MongoDB

I have mongoDb replica set , One primary one secondary and an arbiter to vote. I'm planning to implement sharding as the data is expected to grow exponentially.I find difficult in following mongoDb document for sharding. Could someone explain it clearly to set it up. Thanks in advance.

If you could do accomplish replicaset, sharding is pretty simple. Pretty much repeating the mongo documentation in fast forward here:

Below is for a sample setup: 3 configDB and 3 shards For the below example you can run all of it one machine to see it all working.

  1. If you need three shards setup three replica sets. (Assuming 3 Primary's are 127:0.0.1:27000, 127.0.0.1:37000, 127.0.0.1:47000)
  2. Run 3 instances mongod as three config servers. (Assuming: 127.0.0.1:27020, 127.0.0.1:27021, 127.0.0.1:270122)
  3. Start mongos (note the s in mongos) letting it know where your config servers are. (ex: 127.0.0.1:27023)
  4. Connect to mongos from mongo shell and add the three primary mongod's of your 3 replica sets as the shards.
  5. Enable sharding for your DB.
  6. If required enable sharding for a collection.
  7. Select a shard key if required. (Very Important you do it right the first time!!!)
  8. Check the shard status
  9. Pump data; connect to individual mongod primarys and see the data distributed across the three shards.

 #start mongos with three configs: mongos --port 27023 --configdb localhost:27017,localhost:27018,localhost:27019 mongos> sh.addShard("127.0.0.1:27000"); { "shardAdded" : "shard0000", "ok" : 1 } mongos> sh.addShard("127.0.0.1:37000"); { "shardAdded" : "shard0001", "ok" : 1 } mongos> sh.addShard("127.0.0.1:47000"); { "shardAdded" : "shard0002", "ok" : 1 } mongos> sh.enableSharding("db_to_shard"); { "ok" : 1 } mongos> use db_to_shard; switched to db db_to_shard mongos> mongos> sh.shardCollection("db_to_shard.coll_to_shard", {collId: 1, createdDate: 1} ); { "collectionsharded" : "db_to_shard.coll_to_shard", "ok" : 1 } mongos> show databases; admin (empty) config 0.063GB db_to_shard 0.078GB mongos> sh.status(); --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("557003eb4a4e61bb2ea0555b") } shards: { "_id" : "shard0000", "host" : "127.0.0.1:27000" } { "_id" : "shard0001", "host" : "127.0.0.1:37000" } { "_id" : "shard0002", "host" : "127.0.0.1:47000" } balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: No recent migrations databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : false, "primary" : "shard0000" } { "_id" : "db_to_shard", "partitioned" : true, "primary" : "shard0000" } db_to_shard.coll_to_shard shard key: { "collId" : 1, "createdDate" : 1 } chunks: shard0000 1 { "collId" : { "$minKey" : 1 }, "createdDate" : { "$minKey" : 1 } } -->> { "collId" : { "$maxKey" : 1 }, "createdDate" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM