Updated on 2022-09-16 GMT+08:00

Restoring the configsvr Replica Set

Preparing Directories

rm -rf /compile/cluster-restore/cfg*

mkdir -p /compile/cluster-restore/cfg1/data/db

mkdir -p /compile/cluster-restore/cfg1/log

mkdir -p /compile/cluster-restore/cfg2/data/db

mkdir -p /compile/cluster-restore/cfg2/log

mkdir -p /compile/cluster-restore/cfg3/data/db

mkdir -p /compile/cluster-restore/cfg3/log

Procedure

  1. Prepare the configuration file and data directory of a single node and start the process in single-node mode.

    1. The configuration file is as follows (restoreconfig/single_40303.yaml):
      net:
        bindIp: 127.0.0.1
        port: 40303
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg1/configsvr.pid}
      storage:
        dbPath: /compile/cluster-restore/cfg1/data/db/ 
        directoryPerDB: true
        engine: wiredTiger
        wiredTiger:
          collectionConfig: {blockCompressor: snappy}
          engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
          indexConfig: {prefixCompression: true}
      systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg1/log/configsingle.log}
    2. Copy the decompressed configsvr file to the dbPath directory on the single node.

      cp -aR

      /compile/download/backups/cac1efc8e65e42ecad8953352321bfeein02_41c8a32fb10245899708dea453a8c5c9no02/* /compile/cluster-restore/cfg1/data/db/

    3. Start the process.

      ./mongod -f restoreconfig/single_40303.yaml

  2. Connect to the single node and run the following configuration command:

    ./mongo --host 127.0.0.1 --port 40303
    1. Run the following commands to modify the replica set configuration:

      var cf=db.getSiblingDB('local').system.replset.findOne();

      cf['members'][0]['host']='127.0.0.1:40303';

      cf['members'][1]['host']='127.0.0.1:40304';

      cf['members'][2]['host']='127.0.0.1:40305';

      cf['members'][0]['hidden']=false;

      cf['members'][1]['hidden']=false;

      cf['members'][2]['hidden']=false;

      cf['members'][0]['priority']=1;

      cf['members'][1]['priority']=1;

      cf['members'][2]['priority']=1;

      db.getSiblingDB('local').system.replset.remove({});

      db.getSiblingDB('local').system.replset.insert(cf)

    2. Run the following commands to clear the built-in accounts:

      db.getSiblingDB('admin').dropAllUsers();

      db.getSiblingDB('admin').dropAllRoles();

    3. Run the following command to update the mongos and shard information:

      db.getSiblingDB('config').mongos.remove({});

      Query the _id information about multiple shards in the config.shards table. The _id information is used as the query condition of _id in the following statements. Update records in sequence.

      db.getSiblingDB('config').shards.update({'_id' : 'shard_1'},{$set: {'host': 'shard_1/127.0.0.1:40306,127.0.0.1:40307,127.0.0.1:40308'}})

      db.getSiblingDB('config').shards.update({'_id' : 'shard_2'},{$set: {'host': 'shard_2/127.0.0.1:40309,127.0.0.1:40310,127.0.0.1:40311'}})

      db.getSiblingDB('config').mongos.find({});

      db.getSiblingDB('config').shards.find({});

    4. Run the following command to stop the single-node process:

      db.getSiblingDB('admin').shutdownServer();

  3. Create a configsvr replica set.

    1. Copy the dbPath file of the configsvr1 node to the directories of the other two configsvr nodes.

      cp -aR /compile/cluster-restore/cfg1/data/db/ /compile/cluster-restore/cfg2/data/db/

      cp -aR /compile/cluster-restore/cfg1/data/db/ /compile/cluster-restore/cfg3/data/db/

    2. Add the replica set configuration attribute to the configuration file (restoreconfig/configsvr_40303.yaml) of the configsvr-1 node.
      net:
        bindIp: 127.0.0.1
        port: 40303
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg1/configsvr.pid}
      replication: {replSetName: config}
      sharding: {archiveMovedChunks: false, clusterRole: configsvr}
      storage:
        dbPath: /compile/cluster-restore/cfg1/data/db/ 
        directoryPerDB: true
        engine: wiredTiger
        wiredTiger:
          collectionConfig: {blockCompressor: snappy}
          engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
          indexConfig: {prefixCompression: true}
      systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg1/log/configsvr.log}
    1. Start the process.

      ./mongod -f restoreconfig/configsvr_40303.yaml

    2. Add the replica set configuration attribute to the configuration file (restoreconfig/configsvr_40304.yaml) of the configsvr-2 node.
      net:
        bindIp: 127.0.0.1
        port: 40304
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg2/configsvr.pid}
      replication: {replSetName: config}
      sharding: {archiveMovedChunks: false, clusterRole: configsvr}
      storage:
        dbPath: /compile/cluster-restore/cfg2/data/db/ 
        directoryPerDB: true
        engine: wiredTiger
        wiredTiger:
          collectionConfig: {blockCompressor: snappy}
          engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
          indexConfig: {prefixCompression: true}
      systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg2/log/configsvr.log}
    3. Start the process.

      ./mongod -f restoreconfig/configsvr_40304.yaml

    4. Add the replica set configuration attribute to the configuration file (restoreconfig/configsvr_40305.yaml) of the configsvr-3 node.
      net:
        bindIp: 127.0.0.1
        port: 40305
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/cfg3/configsvr.pid}
      replication: {replSetName: config}
      sharding: {archiveMovedChunks: false, clusterRole: configsvr}
      storage:
        dbPath: /compile/cluster-restore/cfg3/data/db/ 
        directoryPerDB: true
        engine: wiredTiger
        wiredTiger:
          collectionConfig: {blockCompressor: snappy}
          engineConfig: {directoryForIndexes: true, journalCompressor: snappy}
          indexConfig: {prefixCompression: true}
      systemLog: {destination: file, logAppend: true, logRotate: reopen, path: /compile/cluster-restore/cfg3/log/configsvr.log}
    5. Start the process.

      ./mongod -f restoreconfig/configsvr_40305.yaml

  4. Wait until the primary node is selected.

    ./mongo --host 127.0.0.1 --port 40303

    Run the rs.status() command to check whether the primary node exists.