Mongodb sharding principles learning and trial (5) addition and removal of shard

Source: Internet
Author: User
Tags mongodb sharding

1. This morning, a host setting part is localhost and another part is hostname, causing a failure to add shard to sharding. Special Records.

Problem: each existing shard in a sharding is another mongod process on the local machine. When the shard was added, db. runCommand ({addshard: "localhost: port "}). Later, a replSet is configured on the local machine. The problem arises. After the replSet deployment is successful, the host of each node uses hostname. In this way, an error is reported when I add replSet to sharding. If all sharding nodes are other processes on the local machine, either localhost or hostname will be used when host is set. If you use both nodes, an error will be reported.

Solution: At the beginning, I plan to change the host of each node in replSet to localhost. However, an error is reported during reconfig. The following error is reported:

  

Then, change the host of each shard added to the sharding to the hostname. the modification method is use config; db. shards. update ({_ id: xx}, {host: "xx"}); this modification is successful. Then it's easy.

2. Remove a shard from sharding. Data stored in the removed shard needs to be moved to another shard. After the removal, the removed shard has no data.

(1) Remove a shard. use admin; db. runCommand ({removeshard: "name "});

  

(2) If the execution result is as follows, note that the movePrimary operation is required. This is because the removed shard is the base camp for several databases. In this case, you need to perform the movePrimary operation after the data is removed, and set a new base camp for these databases. Several Libraries of the new base camp need to be set here, for example, the value of dbsToMove. If the removed shard does not exist as the base camp of any database, you do not need to execute movePrimary. For example:

  

  

(3) For example, the shard named shard0001 is not the base camp of any database, so when it is removed, the result output does not contain the note and dbsToMove values when shard0000 is removed. Note that the removed shard is the base camp for several databases. You need to set up a new base camp for the database, no matter whether any database does not participate in sharding. For example

  

  

(4) Before data migration, the value of partitioned in sar is false. After the movePrimary operation is completed, the value of partitioned in sar is still false, for example.

  

(5) It may take different time to remove data due to the network and data volume. Run the db. runCommand ({removeshard: "name"}) command to view the data migration progress.

   

(6) For example, by executing the db. runCommand ({removeshard: "name"}) command, you can not only start to remove the shard, but also view the data migration progress during the removal process. The value of remaining indicates the data migration progress. Chunks indicates the number of data blocks to be migrated. When the value of chunks is equal to 0, the table data migration is completed. The dbs value indicates the number of remaining databases that need to be set to a new base camp. The movePrimary operation will be performed after the data migration is complete. That is, when the chunks value is equal to 0, the movePrimary operation can be executed.

  

(7) For example, if the chunks value is 0, the data migration is completed. You can start the movePrimary operation.

  

(8) For example, You can reset the shard base camp of database foo and too to rs0. After the operation is completed, you can view the entire removal progress.

  

(9) For example, the entire removal process has been completed. At this time, you can safely stop the shard0000 shard.

  

   

  

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.