In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
For MongoDB's Shard cluster, it's easy to add a shard, just AddShard.
But reducing clusters (removing shards) is rarely used, but in some scenarios, the data on it must be automatically migrated to other Shard.
Mongodb migration fragmentation
1. Execute the RemoveShard command-- just migrate the sharding table
1 db.runCommand ({removeshard: "your_shard_name"})
3 {msg: "draining startedsuccessfully", state: "started", shard: "mongodb0", ok: 1}
The above sentence will be returned immediately and actually executed in the background.
2. View the migration status
We can execute the above statement repeatedly to see the execution result.
1 db.runCommand ({removeshard: "your_shard_name"})
3 {msg: "draining ongoing", state: "ongoing", remaining: {chunks: 42, dbs: 1}, ok: 1}
As you can see from the above, it is being migrated, and there are still 42 pieces left to be migrated.
When remain is 0, this step ends.
3. Migrate non-Shard data or non-Shard libraries
1 db.runCommand ({movePrimary: "testdb", to: "shard2"})
This time it will not be returned immediately, it will take a long time, and then it will be returned as follows:
1 {"primary": "mongodb1", "ok": 1}
4. Final clean-up
After the above steps have been completed, you need to perform RemoveShard again to clean up the residual data.
1 db.runCommand ({removeshard: "shard1"})
When the execution is successful, the result is as follows:
1 {msg: "remove shard completedsuccesfully", stage: "completed", host: "shard1", ok: 1}
Once the completed is displayed, you can safely shut down the mongod process.
Pay attention to the official instructions on whether you need to run movePrimary:
In other words, if there is a non-sharded collection on this chip, in this way, the sharded data is merged with other shards, and the remaining non-sharded data cannot be merged to other servers by merging shards, so run a movePrimary command to move the non-sharded data to another server. Db.runCommand ({movePrimary: "testdb", to: "shard1"})
There is also an official note to pay attention to:
Warning
Do not run themovePrimary until you have finished draining the shard
In other words, you must wait until the sharding data has been migrated, and then run the movePrimary command!
And unlike removeshard, this command is asynchronous. The movePrimary command will not respond until all non-sharded data is moved to other servers, so it may take a long time, depending on how much non-sharded data is available on this server.
In addition, after the movePrimary execution, remember to run db.runCommand ({removeshard: "shardx"}) again until you see the following result {msg: "removeshard completed successfully", stage: "completed", host: "mongodb0", ok: 1}
At this point, the migration is really complete, and you can safely close mongod.
Mongodb turns off sharding of the table (removes shards)
Procedure: export collections with sharding, delete collections, import collections
1. Export collection
/ usr/local/mongodb/bin/mongodump--host 127.0.0.1 table1 30000-d testdb-c table1-o testdb/
two。 Disable automatic balancing of fragments
> use config
> db.settings.update ({_ id: "balancer"}, {$set: {stopped: true}}, true)
> db.printShardingStatus ()
3. Delete a collection
Db.table1.drop ()
4. Import collection
/ usr/local/mongodb/bin/mongorestore-- host 127.0.0.1 testdb testdb/ 30000-d
Execute db.printShardingStatus () to view the shard summary
Found that the sharding function of the collection table1 has been turned off!
Balanced closing and opening
When mongodb does automatic shard balancing, it may cause slow response of the database. This problem can be solved by disabling automatic balancing and setting the time for automatic balancing.
(1) disable the automatic balancing of fragments
> use config
> db.settings.update ({_ id: "balancer"}, {$set: {stopped: true}}, true)
Restore the dynamic platform:
Use config
Db.settings.remove ({"_ id": "balancer"})
(2) Custom the time period for automatic balancing
> use config
> db.settings.update ({_ id: "balancer"}, {$set: {activeWindow: {start: "21:00", stop: "9:00"}, true)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.