How to build mongodb partition (no master-slave)


First, prepare mongodb on the server running the sharding. We need to plan:

  1. A configuration server is used to store partition information and coordinate data storage;
  2. Several slicing servers are used to save the data itself;
  3. A broker server, which is used to perform sharding operations and is also the access portal of the client.

1. Start the partition server

At this time, we first run each partition server independently, and add--shardsvrParameter table name is an instance used to store partition 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 configuration server

Then we will configure the server to turn on.

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

3. Start the broker server

Continue to turn on the broker. At this time, you need to--configdbParameter to the configuration server.

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

At this point, all servers have been turned on, but they are still in a state of mutual independence. We need to use the client to log in the management service and add the shard server.

4. Add partition server (login to broker, database admin)

db.runCommand( { addshard : "" } );
db.runCommand( { addshard : "" } );
db.runCommand( { addshard : "" } );
db.runCommand( { addshard : "" } );

5. Enable sharding for database


6. Enable sharding for sets

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

Delete fragmentation

If we need to drop a partition server, mongodb will automatically move the above data to other partitions to execute the corresponding command. This is a relatively long process, and the efficiency of database operation will be affected.

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

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