如何重命名MongoDB中的replica set

Posted jerryzh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何重命名MongoDB中的replica set相关的知识,希望对你有一定的参考价值。

虚拟机里的MongoDB集群全部停掉之后,使用--replSet rs2参数重新启动。mongo shell连进去之后查看状况,

rs1:OTHER> rs.status()

        "operationTime" : Timestamp(1559293558, 1),
        "ok" : 0,
        "errmsg" : "Our replica set config is invalid or we are not a member of it",
        "code" : 93,
        "codeName" : "InvalidReplicaSetConfig",
        "$clusterTime" : 
                "clusterTime" : Timestamp(1559293558, 1),
                "signature" : 
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                
        

重新初始化,还是不行,rs.status()还是老样子

rs.initiate(
   _id : "rs2",
   members: [
       _id: 0, host: "localhost:51001" ,
       _id: 1, host: "localhost:51002" ,
       _id: 2, host: "localhost:51003" 
   ]
)  

配置信息看起来还正常

rs1:OTHER> rs.conf()

        "_id" : "rs1",
        "version" : 1,
        "protocolVersion" : NumberLong(1),
        "writeConcernMajorityJournalDefault" : true,
        "members" : [
                
                        "_id" : 0,
                        "host" : "localhost:51001",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : 

                        ,
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                ,
                
                        "_id" : 1,
                        "host" : "localhost:51002",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : 

                        ,
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                ,
                
                        "_id" : 2,
                        "host" : "localhost:51003",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : 

                        ,
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                
        ],
        "settings" : 
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "catchUpTimeoutMillis" : -1,
                "catchUpTakeoverDelayMillis" : 30000,
                "getLastErrorModes" : 

                ,
                "getLastErrorDefaults" : 
                        "w" : 1,
                        "wtimeout" : 0
                ,
                "replicaSetId" : ObjectId("5cf0d661fb9b2ca10d607eca")
        

  

看一下log,有这么一段

2019-06-01T11:03:17.078+0800 W REPL [replexec-0] Local replica set configuration document reports set name of rs1, but command line reports rs2; waiting for reconfig or remote heartbeat
2019-06-01T11:03:17.078+0800 I REPL [replexec-0] New replica set config in use: _id: "rs1", version: 1, protocolVersion: 1, writeConcernMajorityJournalDefault: true, members: [ _id: 0, host: "localhost:51001", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: , slaveDelay: 0, votes: 1 , _id: 1, host: "localhost:51002", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: , slaveDelay: 0, votes: 1 , _id: 2, host: "localhost:51003", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: , slaveDelay: 0, votes: 1 ], settings: chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, catchUpTimeoutMillis: -1, catchUpTakeoverDelayMillis: 30000, getLastErrorModes: , getLastErrorDefaults: w: 1, wtimeout: 0 , replicaSetId: ObjectId(‘5cf0d661fb9b2ca10d607eca‘)
2019-06-01T11:03:17.078+0800 I REPL [replexec-0] This node is not a member of the config
2019-06-01T11:03:17.078+0800 I REPL [replexec-0] transition to REMOVED from STARTUP
2019-06-01T11:03:17.078+0800 I REPL [replexec-0] Starting replication storage threads

 

仔细一看,shell里的提示符,也是rs1。奇怪

那就改下名字吧

https://stackoverflow.com/questions/33400607/how-do-i-rename-a-mongodb-replica-set

 

1. 停止所有实例,使用非复制模式启动

  重新启动,去掉--replSet参数

2. 清除复制集信息缓存数据库local

  使用mongo shell连接到服务器,对3个实例分别执行以下命令  

use local
db.dropDatabase()

3. 停止所有实例,使用复制模式启动

  重新启动,使用--replSet参数指定新的复制集名字

4. 初始化复制集

  a. 使用mongo shell连接到其中一个实例,这个实例将作为新的PRIMARY

  b. 执行rs.initiate()。不要指定参数,不然会报错。其他配置参数可以在随后使用rs.reconfig()方法来设置

  c. 对每一个secondary,使用rs.add("localhost:51002")添加到复制集

  d. 等待secondary同步

 

这种方式不需要重新导入数据

 

以上是关于如何重命名MongoDB中的replica set的主要内容,如果未能解决你的问题,请参考以下文章

MongoDB——MongoDB副本集(Replica Sets)

MongoDB——MongoDB副本集(Replica Sets)

MongoDB 维护Replica Set

MongoDB Replica Set搭建集群

MongoDB 维护Replica Set

How To Setup MongoDB 4.0 Replica Set