更新时间:2022-05-09 GMT+08:00
分享

恢复configsvr副本集

准备目录

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

操作步骤

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

    1. 配置文件如下(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. 准备数据,将解压后的configsvr文件拷贝到单节点dbPath目录下。

      cp -aR

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

    3. 启动进程。

      ./mongod -f restoreconfig/single_40303.yaml

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

    连接命令:./mongo --host 127.0.0.1 --port 40303
    1. 执行如下命令,修改副本集配置信息。

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

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

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

    3. 执行如下命令,更新mongos和shard信息。

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

      先查询config.shards表中的多个shard的_id信息,用于下面语句的_id的查询条件。逐个更新每条记录。

      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. 执行如下命令,关闭单节点进程。

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

  3. 搭建configsvr副本集。

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

      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. 修改configsvr-1节点配置文件,增加副本集配置属性(restoreconfig/configsvr_40303.yaml)。
      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. 启动进程。

      ./mongod -f restoreconfig/configsvr_40303.yaml

    2. 修改configsvr-2节点配置文件,增加副本集配置属性(restoreconfig/configsvr_40304.yaml)。
      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. 启动进程

      ./mongod -f restoreconfig/configsvr_40304.yaml

    4. 修改configsvr-3节点配置文件,增加副本集配置属性(restoreconfig/configsvr_40305.yaml)。
      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. 启动进程。

      ./mongod -f restoreconfig/configsvr_40305.yaml

  4. 等待选主成功。

    ./mongo --host 127.0.0.1 --port 40303

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

分享:

    相关文档

    相关产品

close