MongoDB learning notes: segmentation

This article was updated on January 8, 2022, using MongoDB 4.4.5.

Create shards under a single server

  1. Ensure that the / data/db directory exists and that the current system user has read and write permissions. For example (permission needs to be set according to the actual situation):

    su root
    mkdir -p /data/db
    chmod 0777 /data /data/db
    
  2. Start the shell without connecting to any mongod.

    mongo --nodb
    
  3. Create a shard cluster (the author's shard cluster has three shards with a block size of 1MB). First delete the original database directory, then create the database directory and files, start the mongos process, configure several mongod processes of the replica set (three processes by default), and several mongod processes of several fragmented replica sets (the author specifies three fragmented replica sets, and each replica set defaults to one process), and continuously output the logs of all nodes.

    var sharding = new ShardingTest({shards: 3, chunksize: 1});
    
  4. Start the shell and connect to mongos (localhost::20006) to perform query operations.

    mongo mongodb://localhost:20006
    

    The shell prompt is "mongos".

  5. Enable sharding for the database (the author uses the test database).

    sh.enableSharding("test");
    
  6. Index the slice key (the author uses the username field of the users Collection).

    db.users.createIndex({username: 1});
    
  7. Slice the set (the users set used by the author is the set of database test) according to the slice key (the author uses the username field). Later queries can use sharding.

    sh.shardCollection("test.users", {username: 1});
    
  8. Stop the sharded cluster in the first shell. It will stop the mongos process, configure all mongod processes in the replica set, and all mongod processes in all partitioned replica sets.

    sharding.stop();
    

Create shards under multiple servers

This method can also create shards under a single server (the author uses it in this way).

  1. Start configuration server

    1. Ensure that the data directory (the author's replica set has three nodes, using / data/db/config0, / data/db/config1, / data/db/config2) exists and that the current system user has read and write permissions. For example (permission needs to be set according to the actual situation):

      su root
      mkdir -p /data/db/config0
      mkdir -p /data/db/config1
      mkdir -p /data/db/config2
      chmod 0777 /data /data/db /data/db/config0 /data/db/config1 /data/db/config2
      
    2. Use different shell s to start the replica set node (the author's replica set name is config). You must specify the bound IP (the author uses 0.0.0.0). If you need to specify different data directories (as the author described in the previous step) and ports (the author uses 40000, 40001 and 40002) under a single server. The database file is created.

      mongod --configsvr --replSet config --dbpath /data/db/config0 --port 40000 --bind_ip 0.0.0.0
      mongod --configsvr --replSet config --dbpath /data/db/config1 --port 40001 --bind_ip 0.0.0.0
      mongod --configsvr --replSet config --dbpath /data/db/config2 --port 40002 --bind_ip 0.0.0.0
      
    3. Start the shell to connect to any node (the author uses the node on port 40000).

      mongo mongodb://localhost:40000
      
    4. After initializing the replica set, the primary node will be selected from the backup node (the author's replica set name is config, and each node is localhost:40000, localhost:40001 and localhost:40002). If the replica set has been initialized, you do not need to perform this step.

      rs.initiate({
      	_id: "config",
      	members: [
      		{
      			"_id": 0,
      			"host": "localhost:40000"
      		},
      		{
      			"_id": 1,
      			"host": "localhost:40001"
      		},
      		{
      			"_id": 2,
      			"host": "localhost:40002"
      		}
      	]
      });
      
  2. Start mongos

    The configuration server needs to be specified (the name of the replica set used by the author is config, and the nodes are localhost:40000, localhost:40001 and localhost:40002. In addition, the author's mongos uses port 60000).

    mongos --configdb config/localhost:40000,localhost:40001,localhost:40002 --port 60000
    
  3. Start several replica sets

    1. Ensure that the data directory (the author uses three replica sets, each replica set has one node, using / data/db/shard0-0, / data/db/shard1-0, / data/db/shard2-0) exists and that the current system user has read and write permissions. For example (permission needs to be set according to the actual situation):

      su root
      mkdir -p /data/db/shard0-0
      mkdir -p /data/db/shard1-0
      mkdir -p /data/db/shard2-0
      chmod 0777 /data /data/db /data/db/shard0-0 /data/db/shard1-0 /data/db/shard2-0
      
    2. Use different shell s to start the replica set node (the author's replica set names are shard0, shard1 and shard2). You must specify the bound IP (the author uses 0.0.0.0). If you need to specify different data directories (as described in the previous step) and ports (the author uses 50000, 50001 and 50002) under a single server. The database file is created.

      mongod --shardsvr --replSet shard0 --dbpath /data/db/shard0-0 --port 50000 --bind_ip 0.0.0.0
      mongod --shardsvr --replSet shard1 --dbpath /data/db/shard1-0 --port 50001 --bind_ip 0.0.0.0
      mongod --shardsvr --replSet shard2 --dbpath /data/db/shard2-0 --port 50002 --bind_ip 0.0.0.0
      
    3. Initialize all replica sets (the author only lists the steps of replica sets named shard0. Replica sets named shard1 and shard2 are the same). If the replica set has been initialized, you do not need to perform this step.

      1. Start the shell and connect to the replica set node (the author's example uses a replica set with the name shard0, and the port of its node is 50000. The same is true for nodes with ports 50001 and 50002).

        mongo mongodb://localhost:50000
        
      2. When initializing the replica set, the primary node will be selected from the backup node (the author's example uses the replica set named shard0, whose node is localhost:50000. The node of the replica set named shard1 is localhost:50001, and the node of the replica set named shard2 is localhost:50002, the same).

        rs.initiate({
        	_id: "shard0",
        	members: [
        		{
        			"_id": 0,
        			"host": "localhost:50000"
        		}
        	]
        });
        
  4. Convert all replica sets to shards

    1. Start the shell and connect to mongos (localhost:60000).

      mongo mongodb://localhost:60000
      
    2. Add all replica sets (shard0/localhost:50000, shard1/localhost:50001, shard2/localhost:50002 used by the author) as Shards.

      sh.addShard("shard0/localhost:50000");
      sh.addShard("shard1/localhost:50001");
      sh.addShard("shard2/localhost:50002");
      
  5. Data slicing

    1. Start the shell and connect to mongos (localhost:60000).

      mongo mongodb://localhost:60000
      
    2. Enable sharding for the database (the author uses the test database).

      sh.enableSharding("test");
      
    3. Index the slice key (the author uses the username field of the users Collection).

      db.users.createIndex({username: 1});
      
    4. Slice the set (the users set used by the author is the set of database test) according to the slice key (the author uses the username field). Later queries can use sharding.

      sh.shardCollection("test.users", {username: 1});
      

Restart and connect to shards

For the shard created in the above way, if you need to restart and connect to the shard, just start a different shell and perform several steps, as follows:

mongod --configsvr --replSet config --dbpath /data/db/config0 --port 40000 --bind_ip 0.0.0.0
mongod --configsvr --replSet config --dbpath /data/db/config1 --port 40001 --bind_ip 0.0.0.0
mongod --configsvr --replSet config --dbpath /data/db/config2 --port 40002 --bind_ip 0.0.0.0
mongos --configdb config/localhost:40000,localhost:40001,localhost:40002 --port 60000
mongod --shardsvr --replSet shard0 --dbpath /data/db/shard0-0 --port 50000 --bind_ip 0.0.0.0
mongod --shardsvr --replSet shard1 --dbpath /data/db/shard1-0 --port 50001 --bind_ip 0.0.0.0
mongod --shardsvr --replSet shard2 --dbpath /data/db/shard2-0 --port 50002 --bind_ip 0.0.0.0
mongo mongodb://localhost:60000

Keywords: MongoDB

Added by nosti on Tue, 01 Feb 2022 22:27:57 +0200