Mongodb delete adds sharding with non sharding table maintenance

  • 2020-06-03 08:40:49
  • OfStack

MongoDB is a database based on distributed file storage. Written by 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. It is the most featureless and most relational database.

1. How to remove the sharding

1. Confirm that balancer is enabled

mongos > sh.getBalancerState()
true

2. Remove the sharding

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

The same perform


mongos> use admin
switched to db admin
mongos> db.runCommand( { removeShard: "shard3" } )
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(3),
"dbs" : NumberLong(0)
},
"ok" : 1
}

chunks in remaining indicates how many blocks remain unmigrated.

4. Remove unsharded data

In a cluster, a database with unsharded collections stores those collections only on a single shard.
That shard becomes the primary shard for that database. (Different databases in a cluster can have different primary shards.)
WARNING
Do not perform this procedure until you have finished draining the shard.
1) To determine if shard are is primary for any databases cluster databases of the methods:
sh.status()
db.printShardingStatus()
In the resulting document, the databases field lists each database and its primary shard.
For example, the following database field shows that the products database uses mongodb0 as the primary shard:
{ "_id" : "products", "partitioned" : true, "primary" : "mongodb0" }
2) To to another shard, use the movePrimary command
issue the following command:
use admin
db.runCommand({movePrimary: "products", to: "mongodb1"}) --products is db name
This command does not return until MongoDB completes moving all data, which may take a long time.
The response from this command will resemble the following:
{ "primary" : "mongodb1", "ok" : 1 }
If you use the movePrimary command to move un-sharded collections, you 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 do not update the mongos instances' metadata cache after using movePrimary, the mongos may not write data to the correct shard.
To recover, you must manually intervene.

According to the above, it is best to stop when migrating non-sharding tables. After running the db.runCommand ({movePrimary: "products", to: "mongodb1"}) command, refresh all mongos (run db.runCommand ("flushRouterConfig") on all mongos) and then provide service. You can also restart all instances of mongos.

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, the migration is complete.

2. Add sharding

1. First, confirm that balancer has been turned on

mongos > sh.getBalancerState()
true

2. Execute the command to add sharding

If the following error occurs, delete the test1 database on 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 }

Finally, running the sh.status () command to verify that the migration was successful may take a while.

This is an introduction to Mongodb delete add sharding and non-sharding table maintenance.


Related articles: