Mongodb Delete Add fragment and non-fragment table maintenance _mongodb

Source: Internet
Author: User
Tags mongodb

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB is a product between relational database and non relational database, and is the most powerful and relational database in the relational database.

How to remove a fragment

1, confirm Balancer has been opened

Mongos> Sh.getbalancerstate ()
True

2, remove the fragment

Note: Execute the command under Admin db.

mongos> use admin
switched to DB admin
mongos> db.runcommand ({removeshard: "Shard3"})
{
"msg": "Draining started successfully",
"state": "Started",
"Shard": "Shard3",
"OK": 1
}

3, check the status of the migration

Same execution

mongos> use admin
switched to DB admin
mongos> db.runcommand ({removeshard: "Shard3"})
{
"ms G ":" Draining ongoing "," State
":" ongoing ",
" remaining ": {
" chunks ": Numberlong (3),
" DBS ": Numbe Rlong (0)
},
"OK": 1
}

The chunks in remaining indicates how many blocks of data have not been migrated.

4, remove the not fragmented data

In a cluster, a database with unsharded collections stores those collections only on a single shard.
That's Shard becomes the primary shard for that database. (different databases in a cluster can have different primary shards.)
WARNING
Don't perform this procedure until you have finished draining the Shard.
1 to determine if the Shard your are removing is the primary shard for any of the cluster, databases one of the issue Lowing methods:
Sh.status ()
Db.printshardingstatus ()
In the resulting document, the Databases field lists all database and its primary shard.
For example, the following database field shows this products database uses mongodb0 as the primary shard:
{"_id": "Products", "partitioned": true, "PRIMARY": "Mongodb0"}
2 to move a database to another shard and use the Moveprimary command. For example, to migrate all remaining unsharded the data from mongodb0 to MONGODB1,
Issue the following command:
Use admin
Db.runcommand ({moveprimary: ' Products ', to: ' MONGODB1 '})--products to DB name
This command does the until MongoDB completes moving all data, and which may take a long time.
The response from this command would resemble the following:
{"PRIMARY": "MONGODB1", "OK": 1}
If you have the moveprimary command to move un-sharded collections, you are must either restart all MONGOs instances,
or use the Flushrouterconfig command on all MONGOs instances before writing any data to the cluster.
This action notifies the MONGOs of the new shard for the database.
If you don't update the MONGOs instances ' metadata cache after using Moveprimary, the MONGOs could not write data to the CO Rrect Shard.
To recover, your must manually intervene.

According to the above, migrating a non-fragmented table is the best downtime, after the run Db.runcommand ({moveprimary: "Products", to: "MONGODB1"}) command complete, after refreshing all MONGOs ( All MONGOs are run on Db.runcommand ("Flushrouterconfig") and then externally provided services. You can, of course, restart all MONGOs instances.

5. Complete the migration

mongos> use admin
switched to DB admin
mongos> db.runcommand ({removeshard: "Shard3"})
{
"msg": "Removeshard completed successfully",
"state": "Completed",
"Shard": "Shard3",
"OK": 1
}

If state is completed, it indicates that the migration has completed.

Second, add fragmentation

1, first confirmed that Balancer has been opened

Mongos> Sh.getbalancerstate ()
True

2, the implementation of the order to add fragmentation

If the following error occurs, delete the Test1 database on the target Shard3 and execute the command again

Mongos> Sh.addshard ("shard3/192.168.137.138:27019")
{
"OK": 0,
"errmsg": "Can" T add shard shard3/ 192.168.137.138:27019 because a local database ' test1 ' exists in another shard1:shard1/ 192.168.137.111:27017,192.168.137.75:27017 "
}
mongos> Sh.addshard (" shard3/192.168.137.138:27019 ")
{"shardadded": "Shard3", "OK": 1}

The last Run Sh.status () command confirms that the migration is successful and may take a long time.

The above content is to introduce you to MongoDB Delete add fragmentation and non-fragmented table maintenance of all the narration, I hope to help.

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.