MongoDB learning notes: copy set

Posted by aeboi80 on Sun, 30 Jan 2022 10:20:30 +0100

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

Create replica sets 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 replica set (the author's replica set has three nodes named repl).

    var replset = new ReplSetTest({nodes: 3, name: "repl"});
    
  4. Start the replica set. The original database directory will be deleted first, then the database directory and files will be created, all replica set processes will be started, and the logs of all nodes will be continuously output.

    replset.startSet();
    

    Each element of the return value contains the address of the node (the author is localhost::20000, localhost::20001, localhost::20002).

  5. Initialize replica set. The primary node is selected from the backup node.

    replset.initiate();
    
  6. Start the shell and connect to the node of the replica set (the author uses localhost::20000) to perform the query operation.

    mongo mongodb://localhost:20000
    

    The shell prompt is "repl:PRIMARY" or "repl:SECONDARY", preceded by the replica set name and followed by the primary or backup node.

  7. Stop the replica set in the first shell. All replica set processes are stopped.

    replset.stopSet();
    

Create replica sets under multiple servers

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

  1. Ensure that the data directory (the author's replica set has three nodes, using / data/db/rs0, / data/db/rs1, / data/db/rs2) 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/rs0
    mkdir -p /data/db/rs1
    mkdir -p /data/db/rs2
    chmod 0777 /data /data/db /data/db/rs0 /data/db/rs1 /data/db/rs2
    
  2. Use different shell s to start the replica set node (the author's replica set name is replica). If you need to specify different data directories (as described by the author in the previous step) and ports (the author uses 30000, 30001 and 30002) under a single server. The database file is created.

    mongod --replSet replica --dbpath /data/db/rs0 --port 30000
    mongod --replSet replica --dbpath /data/db/rs1 --port 30001
    mongod --replSet replica --dbpath /data/db/rs2 --port 30002
    
  3. Start the shell to connect to any node (the author uses the node on port 30000).

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

    rs.initiate({
    	_id: "replica",
    	members: [
    		{
    			"_id": 0,
    			"host": "localhost:30000"
    		},
    		{
    			"_id": 1,
    			"host": "localhost:30001"
    		},
    		{
    			"_id": 2,
    			"host": "localhost:30002"
    		}
    	]
    });
    
  5. Start the shell and connect to the replica set (the author's replica set name is replica, and each node is localhost:30000, localhost:30001, localhost:30002). You can perform query operations.

    mongo mongodb://localhost:30000,localhost:30001,localhost:30002/?replicaSet=replica
    
  6. You can also start a shell to connect to any node (the author uses localhost:30000).

    mongo mongodb://localhost:30000
    

Restart and connect to replica set

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

mongod --replSet replica --dbpath /data/db/rs0 --port 30000
mongod --replSet replica --dbpath /data/db/rs1 --port 30001
mongod --replSet replica --dbpath /data/db/rs2 --port 30002
mongo mongodb://localhost:30000,localhost:30001,localhost:30002/?replicaSet=replica

Maintain replica set nodes

  1. Stop the mongod service of the node to be maintained.

  2. Start the node in stand-alone mode (the author uses localhost:30000 in the above example and modifies the port to 10000).

    mongod --dbpath /data/db/rs0 --port 10000
    
  3. Start the shell and connect to this node (as mentioned above, the author uses localhost:10000) for maintenance.

    mongo mongodb://localhost:10000
    
  4. After maintenance, start this node with the original replica set parameters (as mentioned above, the author uses the original replica set name replica and the original port 30000).

    mongod --replSet replica --dbpath /data/db/rs0 --port 30000
    

Topics: MongoDB