mongodb副本集的验证

服务器信息

ip 系统 配置 服务 目录
172.24.32.201 centos7.7 2c4g mongo-primary /var/lib/mongo (the data directory) /var/log/mongodb (the log directory)
172.24.32.201 centos7.7 2c4g mongo-secondary /var/lib/mongo27018 (the data directory) /var/log/mongodb27018 (the log directory)
172.24.32.201 centos7.7 2c4g mongo-abiter /var/lib/mongo27019 (the data directory) /var/log/mongodb27019 (the log directory)

1、关闭 primary 节点,连接数据库,验证数据库主备切换状态,并再次进行查询操作,验证数据库可用性。

systemctl stop mongod

连接之前secondary节点,发现已经变成了主

mongo 172.24.32.201:27018
MongoDB shell version v3.4.24
connecting to: mongodb://172.24.32.201:27018/test
MongoDB server version: 3.4.24
lugotestrepl:PRIMARY> 

查看是否为主

lugotestrepl:PRIMARY> rs.isMaster();
{
    "hosts" : [
        "172.24.32.201:27017",
        "172.24.32.201:27018"
    ],
    "arbiters" : [
        "172.24.32.201:27019"
    ],
    "setName" : "lugotestrepl",
    "setVersion" : 3,
    "ismaster" : true,
    "secondary" : false,
    "primary" : "172.24.32.201:27018",
    "me" : "172.24.32.201:27018",
    "electionId" : ObjectId("7fffffff0000000000000003"),
    "lastWrite" : {
        "opTime" : {
            "ts" : Timestamp(1589446106, 1),
            "t" : NumberLong(3)
        },
        "lastWriteDate" : ISODate("2020-05-14T08:48:26Z")
    },
    "maxBsonObjectSize" : 16777216,
    "maxMessageSizeBytes" : 48000000,
    "maxWriteBatchSize" : 1000,
    "localTime" : ISODate("2020-05-14T08:48:31.108Z"),
    "maxWireVersion" : 5,
    "minWireVersion" : 0,
    "readOnly" : false,
    "ok" : 1
}

查询数据

switched to db lugotest
lugotestrepl:PRIMARY> db.auth('lugo','test123')
1
lugotestrepl:PRIMARY> db.movie.find().pretty()
{
    "_id" : ObjectId("5ebbbca5692adbface98b2af"),
    "moviename" : "大侦探福尔摩斯",
    "points" : "9.5"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b0"),
    "moviename" : "掠夺",
    "points" : "9.2"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b1"),
    "moviename" : "摇滚黑帮",
    "points" : "9.9"
}
{
    "_id" : ObjectId("5ebbbca5692adbface98b2b2"),
    "moviename" : "两杆大烟枪",
    "points" : "9.1"
}
{
    "_id" : ObjectId("5ebcff34df72ed1bb0505234"),
    "moviename" : "杀死比尔",
    "points" : "9.0"
}

数据可查

2、重新启用原先的 primary 节点,并使其重新被选为集群的主节点。

systemctl start mongod

进入mongo27017,查看主从状态

lugotestrepl:SECONDARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T08:52:19.050Z"),
    "myState" : 2,
    "term" : NumberLong(3),
    "syncingTo" : "172.24.32.201:27018",
    "syncSourceHost" : "172.24.32.201:27018",
    "syncSourceId" : 1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589446336, 1),
            "t" : NumberLong(3)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589446336, 1),
            "t" : NumberLong(3)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589446336, 1),
            "t" : NumberLong(3)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 51,
            "optime" : {
                "ts" : Timestamp(1589446336, 1),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2020-05-14T08:52:16Z"),
            "syncingTo" : "172.24.32.201:27018",
            "syncSourceHost" : "172.24.32.201:27018",
            "syncSourceId" : 1,
            "infoMessage" : "",
            "configVersion" : 3,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 49,
            "optime" : {
                "ts" : Timestamp(1589446336, 1),
                "t" : NumberLong(3)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589446336, 1),
                "t" : NumberLong(3)
            },
            "optimeDate" : ISODate("2020-05-14T08:52:16Z"),
            "optimeDurableDate" : ISODate("2020-05-14T08:52:16Z"),
            "lastHeartbeat" : ISODate("2020-05-14T08:52:17.939Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T08:52:18.081Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589445905, 1),
            "electionDate" : ISODate("2020-05-14T08:45:05Z"),
            "configVersion" : 3
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 49,
            "lastHeartbeat" : ISODate("2020-05-14T08:52:17.939Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T08:52:17.410Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 3
        }
    ],
    "ok" : 1
}

发现只是变成了从,没有重新变为主

我们这里先重启现在的主节点,让其变成从,恢复成以前的架构

systemctl restart mongod27018
  • 27017 primary
  • 27018 secondary
  • 27019 abiter

进入主节点

mongo 172.24.32.201:27017

修改priority

lugotestrepl:PRIMARY> config=rs.conf()
{
    "_id" : "lugotestrepl",
    "version" : 3,
    "protocolVersion" : NumberLong(1),
    "members" : [
        {
            "_id" : 0,
            "host" : "172.24.32.201:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "172.24.32.201:27018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "172.24.32.201:27019",
            "arbiterOnly" : true,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "catchUpTimeoutMillis" : 60000,
        "getLastErrorModes" : {
            
        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("5ebcb9e6ae7887a3e356b850")
    }
}
lugotestrepl:PRIMARY> config.members[0].priority = 33
33
lugotestrepl:PRIMARY> rs.reconfig(config)
{ "ok" : 1 }

查看修改后的priority信息

lugotestrepl:PRIMARY> rs.conf()
{
    "_id" : "lugotestrepl",
    "version" : 4,
    "protocolVersion" : NumberLong(1),
    "members" : [
        {
            "_id" : 0,
            "host" : "172.24.32.201:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 33,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "172.24.32.201:27018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "172.24.32.201:27019",
            "arbiterOnly" : true,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {
                
            },
            "slaveDelay" : NumberLong(0),
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatIntervalMillis" : 2000,
        "heartbeatTimeoutSecs" : 10,
        "electionTimeoutMillis" : 10000,
        "catchUpTimeoutMillis" : 60000,
        "getLastErrorModes" : {
            
        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        },
        "replicaSetId" : ObjectId("5ebcb9e6ae7887a3e356b850")
    }
}

我们现在关掉主27017

systemctl stop mongod

30s后启动

systemctl start mongod
lugotestrepl:PRIMARY> rs.status()
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:08:31.676Z"),
    "myState" : 1,
    "term" : NumberLong(6),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589447311, 1),
            "t" : NumberLong(6)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589447311, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589447311, 1),
            "t" : NumberLong(6)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 33,
            "optime" : {
                "ts" : Timestamp(1589447311, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:08:31Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589447289, 1),
            "electionDate" : ISODate("2020-05-14T09:08:09Z"),
            "configVersion" : 4,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 32,
            "optime" : {
                "ts" : Timestamp(1589447311, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589447311, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:08:31Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:08:31Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:08:31.654Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:08:30.045Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 4
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 32,
            "lastHeartbeat" : ISODate("2020-05-14T09:08:31.652Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:08:26.863Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 4
        }
    ],
    "ok" : 1
}

发现27017又恢复成了主

3、将 arbiter 替换为一个独立的 secondary 节点。并验证数据库集群状态。

先移除arbiter节点

[root@test02 lib]# mongo 172.24.32.201:27017
MongoDB shell version v3.4.24
connecting to: mongodb://172.24.32.201:27017/test
MongoDB server version: 3.4.24
lugotestrepl:PRIMARY> db.auth('root','test123')
1
lugotestrepl:PRIMARY> rs.remove('172.24.32.201:27019');
{ "ok" : 1 }

查看状态

lugotestrepl:PRIMARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:23:27.667Z"),
    "myState" : 1,
    "term" : NumberLong(6),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589448201, 1),
            "t" : NumberLong(6)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589448201, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589448201, 1),
            "t" : NumberLong(6)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 929,
            "optime" : {
                "ts" : Timestamp(1589448201, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:23:21Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589447289, 1),
            "electionDate" : ISODate("2020-05-14T09:08:09Z"),
            "configVersion" : 5,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 928,
            "optime" : {
                "ts" : Timestamp(1589448201, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589448201, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:23:21Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:23:21Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:23:27.117Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:23:27.116Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 5
        }
    ],
    "ok" : 1
}

停止27019实例

systemctl stop mongod27019

初始化这个实例,即删除data

mkdir -p /tmp/27919data
mv /var/lib/mongo27019/* /tmp/27919data/

重启这个实例

systemctl start mongod27019

变成seconday

[root@test02 lib]# mongo 172.24.32.201:27017
MongoDB shell version v3.4.24
connecting to: mongodb://172.24.32.201:27017/test
MongoDB server version: 3.4.24
lugotestrepl:PRIMARY> db.auth('root','test123')
1
lugotestrepl:PRIMARY> rs.add('172.24.32.201:27019');
{ "ok" : 1 }

查看集群状态

lugotestrepl:PRIMARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:30:32.881Z"),
    "myState" : 1,
    "term" : NumberLong(6),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589448631, 1),
            "t" : NumberLong(6)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589448631, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589448631, 1),
            "t" : NumberLong(6)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 1354,
            "optime" : {
                "ts" : Timestamp(1589448631, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:30:31Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589447289, 1),
            "electionDate" : ISODate("2020-05-14T09:08:09Z"),
            "configVersion" : 6,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1353,
            "optime" : {
                "ts" : Timestamp(1589448631, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589448631, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:30:31Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:30:31Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:30:31.498Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:30:32.538Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 6
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 39,
            "optime" : {
                "ts" : Timestamp(1589448631, 1),
                "t" : NumberLong(6)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589448631, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:30:31Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:30:31Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:30:31.498Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:30:31.024Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 6
        }
    ],
    "ok" : 1
}

4、关闭数据库的两个 secondary 节点,并验证数据库可用性。

systemctl stop mongod27018
systemctl stop mongod27019

进入主节点27017

lugotestrepl:SECONDARY> rs.status()
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:36:18.312Z"),
    "myState" : 2,
    "term" : NumberLong(6),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589448701, 1),
            "t" : NumberLong(6)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589448711, 1),
            "t" : NumberLong(6)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589448711, 1),
            "t" : NumberLong(6)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 1700,
            "optime" : {
                "ts" : Timestamp(1589448711, 1),
                "t" : NumberLong(6)
            },
            "optimeDate" : ISODate("2020-05-14T09:31:51Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "could not find member to sync from",
            "configVersion" : 6,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:36:14.717Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:31:48.577Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "Connection refused",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : -1
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:36:14.717Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:31:49.067Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "Connection refused",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : -1
        }
    ],
    "ok" : 1
}

发现剩的主变成了从了,数据库不可用

5、重新启用一个 secondary 节点,并验证数据库可用性

systemctl start mongod27019
lugotestrepl:PRIMARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:51:59.187Z"),
    "myState" : 1,
    "term" : NumberLong(7),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589449912, 1),
            "t" : NumberLong(7)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589449912, 1),
            "t" : NumberLong(7)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589449912, 1),
            "t" : NumberLong(7)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2641,
            "optime" : {
                "ts" : Timestamp(1589449912, 1),
                "t" : NumberLong(7)
            },
            "optimeDate" : ISODate("2020-05-14T09:51:52Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1589449901, 1),
            "electionDate" : ISODate("2020-05-14T09:51:41Z"),
            "configVersion" : 6,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 0,
            "state" : 8,
            "stateStr" : "(not reachable/healthy)",
            "uptime" : 0,
            "optime" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(0, 0),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
            "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:51:59.030Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:31:48.577Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "Connection refused",
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : -1
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 28,
            "optime" : {
                "ts" : Timestamp(1589449912, 1),
                "t" : NumberLong(7)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589449912, 1),
                "t" : NumberLong(7)
            },
            "optimeDate" : ISODate("2020-05-14T09:51:52Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:51:52Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:51:59.014Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:51:58.448Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 6
        }
    ],
    "ok" : 1
}

27017实例变成了主,数据库可用。

6、重新启用第二个 sencondary 节点,并验证数据库可用性。

启动第二个从节点

systemctl start mongod27018
lugotestrepl:PRIMARY> rs.status();
{
    "set" : "lugotestrepl",
    "date" : ISODate("2020-05-14T09:53:46.174Z"),
    "myState" : 1,
    "term" : NumberLong(7),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1589450022, 1),
            "t" : NumberLong(7)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1589450022, 1),
            "t" : NumberLong(7)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1589450022, 1),
            "t" : NumberLong(7)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "172.24.32.201:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 2748,
            "optime" : {
                "ts" : Timestamp(1589450022, 1),
                "t" : NumberLong(7)
            },
            "optimeDate" : ISODate("2020-05-14T09:53:42Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1589449901, 1),
            "electionDate" : ISODate("2020-05-14T09:51:41Z"),
            "configVersion" : 6,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "172.24.32.201:27018",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 63,
            "optime" : {
                "ts" : Timestamp(1589450022, 1),
                "t" : NumberLong(7)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589450022, 1),
                "t" : NumberLong(7)
            },
            "optimeDate" : ISODate("2020-05-14T09:53:42Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:53:42Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:53:45.152Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:53:44.451Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27019",
            "syncSourceHost" : "172.24.32.201:27019",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 6
        },
        {
            "_id" : 2,
            "name" : "172.24.32.201:27019",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 135,
            "optime" : {
                "ts" : Timestamp(1589450022, 1),
                "t" : NumberLong(7)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1589450022, 1),
                "t" : NumberLong(7)
            },
            "optimeDate" : ISODate("2020-05-14T09:53:42Z"),
            "optimeDurableDate" : ISODate("2020-05-14T09:53:42Z"),
            "lastHeartbeat" : ISODate("2020-05-14T09:53:45.061Z"),
            "lastHeartbeatRecv" : ISODate("2020-05-14T09:53:44.498Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "172.24.32.201:27017",
            "syncSourceHost" : "172.24.32.201:27017",
            "syncSourceId" : 0,
            "infoMessage" : "",
            "configVersion" : 6
        }
    ],
    "ok" : 1
}

数据库可用

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容