How to build MongoDB fragmentation (no master-slave)

Keywords: Database MongoDB Fragment

First, MongoDB is ready on the server running the fragmentation. We need to plan:

  1. A configuration server is used to store fragmentation information and coordinate data storage.
  2. Several piecewise servers are used to save the data itself.
  3. A broker server, used to perform fragmentation operations, but also the access entrance of the client.

1. Start the Fragmentation Server

At this point, we first run each fragment server independently, and add the name of the shardsvr parameter table, which is an instance used to store fragmented data.

./mongod --shardsvr --port 10001 --dbpath /data/shard/s1 --fork --logpath /data/shard/log/s1.log --directoryperdb
./mongod --shardsvr --port 10002 --dbpath /data/shard/s2 --fork --logpath /data/shard/log/s2.log --directoryperdb
./mongod --shardsvr --port 10003 --dbpath /data/shard/s3 --fork --logpath /data/shard/log/s3.log --directoryperdb
./mongod --shardsvr --port 10004 --dbpath /data/shard/s4 --fork --logpath /data/shard/log/s4.log --directoryperdb

2. Start the configuration server

Then we will configure the server to open.

 ./mongod --configsvr --port 20000 --dbpath /data/shard/conf --fork --logpath /data/shard/log/conf.log --directoryperdb

3. Start the broker server

Continue to turn broker on, at which point you need to point it to the configuration server in the -- configdb parameter.

 ./mongos --port 30000 --configdb 192.168.39.241:20000 --fork --logpath /data/shard/log/route.log

So far, all servers have been opened, but they are still in a separate state. We need to use the client to log in to the management service and add the fragmented server.

4. Add a fragmented server (log in to broker, database admin)

db.runCommand( { addshard : "192.168.39.241:10001" } );
db.runCommand( { addshard : "192.168.39.241:10002" } );
db.runCommand( { addshard : "192.168.39.241:10003" } );
db.runCommand( { addshard : "192.168.39.241:10004" } );

5. Enabling fragmentation of databases

db.adminCommand({enableSharding:"content"});

6. Enable fragmentation for collections

 db.adminCommand({shardCollection:"content.content", key:{"_id":1}});

Delete fragmentation

If we need to drop a fragment server, MongoDB will automatically move the above data to other fragments to execute the corresponding commands. This is a relatively long process, where the efficiency of database operation will also be affected.

db.adminCommand( { listShards: 1 } )
db.adminCommand( { removeShard: "mongodb0" } )

// Delete progress status
use config
db.shards.update({},{$unset:{draining:true}}, false, true)

Posted by WormTongue on Mon, 14 Oct 2019 10:42:36 -0700