mongodb副本集强制其中一个节点为主库并重做副本集集群

在mongodb副本集集群中,若是丢失了n/2+1个节点,也就是失去了多数节点时候,在集群内无法投票表决剩下的节点谁能当主库,那就需要强制其中一个节点为主库.
下面是操作步骤:
1.执行cfg=rs.conf()
2.需要设置需要的_id为主库,
cfg.members=[cfg.members[3]]
3.执行集群重新设置
4.rs.reconfig(cfg, {force: true});
5.rs.status()

下面是详细操作步骤,该集群是四个节点的副本集,其中三个节点已经丢失,使剩下的节点为主节点,单实例运行.
执行cfg=rs.conf(),获取集群的节点信息:

cms:SECONDARY>  cfg=rs.conf() 
{
        "_id" : "cms",
        "version" : 32948,
        "protocolVersion" : NumberLong(1),
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.1.193:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 1,
                        "host" : "192.168.1.195:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 2,
                        "host" : "192.168.1.198:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                },
                {
                        "_id" : 3,
                        "host" : "192.168.1.199:27017",
                        "arbiterOnly" : false,
                        "buildIndexes" : true,
                        "hidden" : false,
                        "priority" : 1,
                        "tags" : {

                        },
                        "slaveDelay" : NumberLong(0),
                        "votes" : 1
                }
        ],
        "settings" : {
                "chainingAllowed" : true,
                "heartbeatIntervalMillis" : 2000,
                "heartbeatTimeoutSecs" : 10,
                "electionTimeoutMillis" : 10000,
                "getLastErrorModes" : {

                },
                "getLastErrorDefaults" : {
                        "w" : 1,
                        "wtimeout" : 0
                },
                "replicaSetId" : ObjectId("59cab0d69b86b9e815cfc02b")
        }
}

下面指定199为主节点

cms:SECONDARY> cfg.members=[cfg.members[3]]
[
        {
                "_id" : 3,
                "host" : "192.168.1.199:27017",
                "arbiterOnly" : false,
                "buildIndexes" : true,
                "hidden" : false,
                "priority" : 1,
                "tags" : {

                },
                "slaveDelay" : NumberLong(0),
                "votes" : 1
        }
]

cms:SECONDARY>  rs.reconfig(cfg, {force: true})
{ "ok" : 1 }

查看更改后的集群状态,操作完成,变成了单实例节点.然后通过添加从节点来实现副本集

cms:SECONDARY> rs.status()
{
        "set" : "cms",
        "date" : ISODate("2018-09-04T01:54:12.445Z"),
        "myState" : 1,
        "term" : NumberLong(6),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 3,
                        "name" : "192.168.1.199:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 3152420,
                        "optime" : {
                                "ts" : Timestamp(1536026052, 24),
                                "t" : NumberLong(6)
                        },
                        "optimeDate" : ISODate("2018-09-04T01:54:12Z"),
                        "infoMessage" : "could not find member to sync from",
                        "electionTime" : Timestamp(1536026047, 1),
                        "electionDate" : ISODate("2018-09-04T01:54:07Z"),
                        "configVersion" : 137682,
                        "self" : true
                }
        ],
        "ok" : 1
}

添加新的节点:

config = { _id:"cms", members:
[
{_id:0,host:"192.168.1.193:27017"},
{_id:1,host:"192.168.1.194:27017"},
{_id:2,host:"192.168.1.198:27017"},
{_id:3,host:"192.168.1.199:27017"}
]
}       

rs.reconfig(config);

然后在新节点启动mongod进程,必须要把新节点数据文件清空.
节点启动以后,会自动拉取主库数据,然后创建索引.
mongod --fork --config /data0/mongodb/mongod.conf

你可能感兴趣的:(MongoDB)