Adding shards
Use admin
Add Shard Nodes , each shard is a replica set "allowlocal:true only when the Shard is configured locally , not at production"
Db.runcommand ({addshard: "localhost:9335", allowlocal:true})
Db.runcommand ({addshard: "localhost:9336", allowlocal:true})
Db.runcommand ({addshard: "localhost:9337", Allowlocal:true, "MaxSize": 20000}) Note: You can also set the size "maxSize" for different shards: 20000 (20GB)
the database to be partitioned
Db.runcommand ({enablesharding: "MyDB"})
set the collection to be fragmented : Users collection , name field key to Shard
Db.runcommand ({shardcollection: "Mydb.users", Key:{name:1}})
Db.runcommand ({shardcollection: "Mydb.users", Key:{_id:1,name:1,name:1}}) "If " errmsg ":" Please create an index t Hat starts with the Shard key before sharding. " , create an index "
View Shard Results
View partition status
Use MyDB
Db.users.getShardDistribution ()
Db.users.stats ();
Add shards to the data that is already fragmented
Db.runcommand ({addshard: "localhost:9337", allowlocal:true}) automatically balances the data
Delete Shards
Db.runcommand ({removeshard: "localhost:9335"})
Db.runcommand ({removeshard: "localhost:9336"})
Db.runcommand ({removeshard: "localhost:9337"})
"If the primary node is deleted, you also need to"
Db.runcommand ({"Moveprimary": "MyDB", "to": "localhost:9336"})
"The Db.runcommand ({removeshard:" Localhost:xxxx "}) will need to be executed once after deletion in order to completely delete the"
"Observe the deletion:"
Use admin
Db.printshardingstatus ()
"You cannot delete all shards in this way, and the last shard cannot be deleted."