无法作为副本连接到mongodb

时间:2019-06-12 18:25:05

标签: mongodb kubernetes

我在kubernetes上创建了一个mongodb副本集。

这是rs.status()的结果:

{
    "set" : "rs0",
    "date" : ISODate("2019-06-12T18:12:50.606Z"),
    "myState" : 1,
    "term" : NumberLong(2),
    "syncingTo" : "",
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1560363168, 1),
            "t" : NumberLong(2)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1560363168, 1),
            "t" : NumberLong(2)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1560363168, 1),
            "t" : NumberLong(2)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1560363168, 1),
            "t" : NumberLong(2)
        }
    },
    "lastStableCheckpointTimestamp" : Timestamp(1560363118, 1),
    "members" : [
        {
            "_id" : 0,
            "name" : "x.x.x.x:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 68328,
            "optime" : {
                "ts" : Timestamp(1560363168, 1),
                "t" : NumberLong(2)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1560363168, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-06-12T18:12:48Z"),
            "optimeDurableDate" : ISODate("2019-06-12T18:12:48Z"),
            "lastHeartbeat" : ISODate("2019-06-12T18:12:49.613Z"),
            "lastHeartbeatRecv" : ISODate("2019-06-12T18:12:49.697Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncingTo" : "y.y.y.y:27017",
            "syncSourceHost" : "y.y.y.y:27017",
            "syncSourceId" : 2,
            "infoMessage" : "",
            "configVersion" : 260577
        },
        {
            "_id" : 2,
            "name" : "y.y.y.y:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 68413,
            "optime" : {
                "ts" : Timestamp(1560363168, 1),
                "t" : NumberLong(2)
            },
            "optimeDate" : ISODate("2019-06-12T18:12:48Z"),
            "syncingTo" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "electionTime" : Timestamp(1560294784, 1),
            "electionDate" : ISODate("2019-06-11T23:13:04Z"),
            "configVersion" : 260577,
            "self" : true,
            "lastHeartbeatMessage" : ""
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1560363168, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1560363168, 1),
        "signature" : {
            "hash" : BinData(0,"rL34zviUDYPBOvAthmOgEYregkU="),
            "keyId" : NumberLong("6701412681397567489")
        }
    }
}

我对mongo知之甚少,但似乎配置良好。

例如,如果我尝试与nodejs建立连接:

const url = 'mongodb://admin:pass@y.y.y.y:27017';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

完全有效

const url = 'mongodb://admin:pass@x.x.x.x:27017';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

这也可以:

const url = 'mongodb://admin:pass@x.x.x.x:27017,y.y.y.y:27017?replicaSet=rs0';
const dbName = 'admin';
MongoClient.connect(url, { useNewUrlParser: true }, function(err, client) {
  console.log(err);
  client.close();
});

这种情况不起作用,我也不十分清楚为什么:

{ MongoNetworkError: failed to connect to server [y.y.y.y:27017] on first connect [MongoNetworkError: connection timed out]
    at Pool.<anonymous> (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/topologies/server.js:431:11)
    at Pool.emit (events.js:182:13)
    at connect (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/pool.js:557:14)
    at makeConnection (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:39:11)
    at callback (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:261:5)
    at Socket.err (/Volumes/Work/test/mongo/node_modules/mongodb-core/lib/connection/connect.js:286:7)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:182:13)
    at Socket._onTimeout (net.js:453:8)
    at ontimeout (timers.js:436:11)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

Studio 3T存在完全相同的问题:它在一台服务器上可以正常工作,但是在添加两台服务器时(超时)失败

编辑:

我还有更多线索可以通过命令行来完成:

mongo "mongodb://proxy:27017,proxy:27018/?replicaSet=rs0"

结果是

MongoDB shell version v4.0.3
connecting to: mongodb://proxy:27017,proxy:27018/?replicaSet=rs0
2019-06-12T14:31:36.272-0400 I NETWORK  [js] Starting new replica set monitor for rs0/proxy:27017,proxy:27018
2019-06-12T14:31:36.848-0400 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to proxy:27017 (1 connections now open to proxy:27017 with a 5 second timeout)
2019-06-12T14:31:36.855-0400 I NETWORK  [js] Successfully connected to proxy:27018 (1 connections now open to proxy:27018 with a 5 second timeout)
2019-06-12T14:31:37.079-0400 I NETWORK  [js] changing hosts to rs0/x.x.x.x:27017,y.y.y.y:27017 from rs0/proxy:27017,proxy:27018
2019-06-12T14:31:52.590-0400 W NETWORK  [js] Unable to reach primary for set rs0
2019-06-12T14:31:52.590-0400 I NETWORK  [js] Cannot reach any nodes for set rs0. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.
2019-06-12T14:31:52.599-0400 E QUERY    [js] Error: connect failed to replica set rs0/proxy:27017,proxy:27018 :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

问题在于该连接是changing hosts to个IP地址,这些地址无法直接访问。

0 个答案:

没有答案