MongoDB Shard Operations

Source: Internet
Author: User

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."

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.