连接到mongo副本集时出现“无法访问最新的主控主机”错误

时间:2019-12-26 19:17:20

标签: mongodb replicaset

我在k8s中配置了mongo副本集。 [MongoDB服务器版本:3.6.14] 这是副本集名称:

mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local 
mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local 
mongodb-replicaset-2.mongodb-replicaset.default.svc.cluster.local

当我使用以下URI连接到它

mongo --verbose  "mongodb://mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local/?replicaSet=rs0" -u <username> -p <password>

我能够连接,但在前几次尝试中均无法读取,并出现以下错误:

2019-12-26T18:46:02.773+0000 I NETWORK  [thread1] Marking host mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017 as failed :: caused by :: NotMaster: got not master from: mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017 of repl set: rs0
2019-12-26T18:46:02.775+0000 E QUERY    [thread1] Error: listCollections failed: {
        "operationTime" : Timestamp(1577385959, 1),
        "ok" : 0,
        "errmsg" : "not master and slaveOk=false",
        "code" : 13435,
        "codeName" : "NotMasterNoSlaveOk",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1577385959, 1),
                "signature" : {
                        "hash" : BinData(0,"Q6zckJKVYI5nTAz3JJkl5QCC9kQ="),
                        "keyId" : NumberLong("6774321924500619265")
                }
        }
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:941:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:953:19
DB.prototype.getCollectionNames@src/mongo/shell/db.js:964:16
shellHelper.show@src/mongo/shell/utils.js:853:9
shellHelper@src/mongo/shell/utils.js:750:15
@(shellhelp2):1:1
2019-12-26T18:46:02.911+0000 I NETWORK  [thread1] Marking host mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017 as failed :: caused by :: Location40657: Last known master host cannot be reached
2019-12-26T18:46:03.818+0000 D NETWORK  [thread1] creating new connection to:mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017
2019-12-26T18:46:03.951+0000 D NETWORK  [thread1] connected to server mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017 (10.132.0.26)
2019-12-26T18:46:04.086+0000 D NETWORK  [thread1] connected connection!
rs0:SECONDARY> 2019-12-26T18:46:24.532+0000 D NETWORK  [ReplicaSetMonitor-TaskExecutor-0] Refreshing replica set rs0 took 273 msec

此错误继续,直到重新连接到主节点。 当我连接时,这是rs.status:

{                                                                                                                                                                                                         
        "set" : "rs0",
        "date" : ISODate("2019-12-26T18:47:20.196Z"),
        "myState" : 2,
        "term" : NumberLong(3),
        "syncingTo" : "mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local:27017",
        "syncSourceHost" : "mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local:27017",
        "syncSourceId" : 1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1577386039, 1),
                        "t" : NumberLong(3)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1577386039, 1),
                        "t" : NumberLong(3)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1577386039, 1),
                        "t" : NumberLong(3)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1577386039, 1),
                        "t" : NumberLong(3)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "mongodb-replicaset-0.mongodb-replicaset.default.svc.cluster.local:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1852,
                        "optime" : {
                                "ts" : Timestamp(1577386029, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1577386029, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2019-12-26T18:47:09Z"),
                        "optimeDurableDate" : ISODate("2019-12-26T18:47:09Z"),
                        "lastHeartbeat" : ISODate("2019-12-26T18:47:18.867Z"),
                        "lastHeartbeatRecv" : ISODate("2019-12-26T18:47:19.003Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "mongodb-replicaset-2.mongodb-replicaset.default.svc.cluster.local:27017",
                        "syncSourceHost" : "mongodb-replicaset-2.mongodb-replicaset.default.svc.cluster.local:27017",
                        "syncSourceId" : 2,
                       "infoMessage" : "",
                        "configVersion" : 3
                },
                {
                        "_id" : 1,
                        "name" : "mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1883,
                        "optime" : {
                                "ts" : Timestamp(1577386029, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1577386029, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2019-12-26T18:47:09Z"),
                        "optimeDurableDate" : ISODate("2019-12-26T18:47:09Z"),
                        "lastHeartbeat" : ISODate("2019-12-26T18:47:18.645Z"),
                        "lastHeartbeatRecv" : ISODate("2019-12-26T18:47:19.960Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "",
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1577384177, 1),
                        "electionDate" : ISODate("2019-12-26T18:16:17Z"),
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "mongodb-replicaset-2.mongodb-replicaset.default.svc.cluster.local:27017",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1915,
                        "optime" : {
                                "ts" : Timestamp(1577386039, 1),
                                "t" : NumberLong(3)
                        },
                        "optimeDate" : ISODate("2019-12-26T18:47:19Z"),
                        "syncingTo" : "mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local:27017",
                        "syncSourceHost" : "mongodb-replicaset-1.mongodb-replicaset.default.svc.cluster.local:27017",
                        "syncSourceId" : 1,
                        "infoMessage" : "",
                        "configVersion" : 3,
                        "self" : true,
                                                "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1577386039, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1577386039, 1),
                "signature" : {
                        "hash" : BinData(0,"0cfmxLzpPXfsvCW2R2DlTnSxlm8="),
                        "keyId" : NumberLong("6774321924500619265")
                }
        }
}

因此,似乎所有副本集都正常。 我必须从主服务器读取,因此必须使用默认值“读取首选项”。

现在到了棘手的部分。根据错误: “无法访问最后一个已知的主主机” 并查看代码,看来mongo尝试转到“主要”,但失败了,这是mongo客户端中的代码

BClientConnection* DBClientReplicaSet::checkMaster() {
    ReplicaSetMonitorPtr monitor = _getMonitor();
    HostAndPort h = monitor->getMasterOrUassert();

    if (h == _masterHost && _master) {
        // a master is selected.  let's just make sure connection didn't die
        if (!_master->isFailed())
            return _master.get();

        monitor->failedHost(_masterHost,
                            {ErrorCodes::Error(40657), "Last known master host cannot be reached"});
        h = monitor->getMasterOrUassert();  // old master failed, try again.
    }

我在做什么错?还是我应该检查什么?

0 个答案:

没有答案