更新时间:2022-07-29 GMT+08:00

恢复shardsvr2副本集

准备目录

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

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

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

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

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

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

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

操作步骤

  1. 准备单节点配置文件和目录,以单节点方式启动进程。

    1. 配置文件如下(restoreconfig/single_40309.yaml)。
      net:
        bindIp: 127.0.0.1
        port: 40309
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/shd21/mongod.pid}
      storage:
        dbPath: /compile/cluster-restore/shd21/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/shd21/log/mongod.log}
    1. 准备数据,将解压后的shardsvr2文件拷贝到单节点dbPath目录下。

      cp -aR

      /compile/download/backups/cac1efc8e65e42ecad8953352321bfeein02_92b196d2401041a7af869a2a3cab7079no02/* /compile/cluster-restore/shd21/data/db/

    2. 启动进程。

      ./mongod -f restoreconfig/single_40309.yaml

  2. 连接单节点,执行配置命令。

    连接命令:./mongo --host 127.0.0.1 --port 40309

    1. 执行如下命令,修改副本集配置信息。

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

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

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

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

      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. 执行如下命令,清理内置账号。

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

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

    3. 执行如下命令,更新configsvr信息。

      var vs = db.getSiblingDB('admin').system.version.find();

      while (vs.hasNext()) {

      var curr = vs.next();

      if (curr.hasOwnProperty('configsvrConnectionString')) {

      db.getSiblingDB('admin').system.version.update({'_id' : curr._id}, {$set: {'configsvrConnectionString': 'config/127.0.0.1:40303,127.0.0.1:40304,127.0.0.1:40305'}});

      }

      }

    4. 执行如下命令,关闭单节点进程。

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

  3. 搭建shardsvr2副本集。

    1. 准备副本集配置文件和目录,将shardsvr2节点的dbPath文件拷贝到其他两个节点目录下。

      cp -aR /compile/cluster-restore/shd21/data/db/ /compile/cluster-restore/shd22/data/db/

      cp -aR /compile/cluster-restore/shd21/data/db/ /compile/cluster-restore/shd23/data/db/

    2. 修改shardsvr2-1节点配置文件,增加副本集配置属性(restoreconfig/shardsvr_40309.yaml)。

      --- replication.replSetName 的值,参考该章节中的shard的_id信息。

      net:
        bindIp: 127.0.0.1
        port: 40309
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/shd21/mongod.pid}
      replication: {replSetName: shard_2}
      sharding: {archiveMovedChunks: false, clusterRole: shardsvr}
      storage:
        dbPath: /compile/cluster-restore/shd21/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/shd21/log/mongod.log}
    3. 启动进程。

      ./mongod -f restoreconfig/shardsvr_40309.yaml

    4. 修改shardsvr2-2节点配置文件,增加副本集配置属性(restoreconfig/shardsvr_40310.yaml)。

      --- replication.replSetName 的值,参考该章节中的shard的_id信息。

      net:
        bindIp: 127.0.0.1
        port: 40310
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/shd22/mongod.pid}
      replication: {replSetName: shard_2}
      sharding: {archiveMovedChunks: false, clusterRole: shardsvr}
      storage:
        dbPath: /compile/cluster-restore/shd22/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/shd22/log/mongod.log}
    5. 启动进程。

      ./mongod -f restoreconfig/shardsvr_40310.yaml

    6. 修改shardsvr2-3节点配置文件,增加副本集配置属性(restoreconfig/shardsvr_40311.yaml)。

      --- replication.replSetName 的值,参考该章节中的shard的_id信息。

      net:
        bindIp: 127.0.0.1
        port: 40311
        unixDomainSocket: {enabled: false}
      processManagement: {fork: true, pidFilePath: /compile/cluster-restore/shd23/mongod.pid}
      replication: {replSetName: shard_2}
      sharding: {archiveMovedChunks: false, clusterRole: shardsvr}
      storage:
        dbPath: /compile/cluster-restore/shd23/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/shd23/log/mongod.log}
    7. 启动进程。

      ./mongod -f restoreconfig/shardsvr_40311.yaml

  4. 等待选主成功。

    ./mongo --host 127.0.0.1 --port 40309

    执行命令rs.status(),查看是否已存在主节点Primary。