AWS ECS MongoDB Error: Node primary did not become available

AWS ECS MongoDB Error: Node primary did not become available

错误:

Node mongodb-primary.ecs.endpoint did not become available

详情:

我已经使用 https://github.com/bitnami/bitnami-docker-mongodb 作为具有副本集功能的 Docker 映像在 AWS ECS 上部署了 MongoDB,这意味着我在同一个服务器上还有 2 个服务 运行除了主实例之外的 EC2 实例是次要实例和仲裁实例。

部署后,所有 3 个服务都会启动它们的任务,并且主要服务的任务保持 运行 成功,而次要服务和仲裁服务的任务失败。

这是日志:

2021-11-28 11:59:58[38;5;6mmongodb [38;5;5m07:59:58.89 [0m[38;5;2mINFO [0m ==> Stopping MongoDB...
2021-11-28 11:59:58[38;5;6mmongodb [38;5;5m07:59:58.89 [0m[38;5;1mERROR[0m ==> Node mongodb-primary.ecs.endpoint did not become available
2021-11-28 11:57:00[38;5;6mmongodb [38;5;5m07:57:00.34 [0m[38;5;2mINFO [0m ==> Found MongoDB server listening at mongodb-primary.ecs.endpoint:27017 !
2021-11-28 11:57:00[38;5;6mmongodb [38;5;5m07:57:00.33 [0m[38;5;2mINFO [0m ==> Trying to connect to MongoDB server mongodb-primary.ecs.endpoint...
2021-11-28 11:56:58[38;5;6mmongodb [38;5;5m07:56:58.20 [0m[38;5;2mINFO [0m ==> Stopping MongoDB...
2021-11-28 11:56:58[38;5;6mmongodb [38;5;5m07:56:58.20 [0m[38;5;2mINFO [0m ==> Configuring MongoDB replica set...

有人知道如何解决这个问题吗?

问题:

缺少副本集配置。

解法:

为了解决这个问题,我使用 MongoDB Compass 登录到主实例,然后使用 mongosh 到 运行 以下命令:rs.initiate()。一分钟之内,其他节点(辅助节点和仲裁节点)将自己注册到副本集中,主节点成为主节点。

输出:

下面是 rs.status() 命令的日志:

rs.status()
{
  set: 'rs0',
  date: 2021-11-28T09:34:27.181Z,
  myState: 1,
  term: Long("1"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 2,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1638092057, i: 1 }), t: Long("1") },
    lastCommittedWallTime: 2021-11-28T09:34:17.823Z,
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1638092057, i: 1 }), t: Long("1") },
    appliedOpTime: { ts: Timestamp({ t: 1638092057, i: 1 }), t: Long("1") },
    durableOpTime: { ts: Timestamp({ t: 1638092057, i: 1 }), t: Long("1") },
    lastAppliedWallTime: 2021-11-28T09:34:17.823Z,
    lastDurableWallTime: 2021-11-28T09:34:17.823Z
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1638092047, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: 2021-11-28T09:30:17.783Z,
    electionTerm: Long("1"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 1638091817, i: 1 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1638091817, i: 1 }), t: Long("-1") },
    numVotesNeeded: 1,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    newTermStartDate: 2021-11-28T09:30:17.807Z,
    wMajorityWriteAvailabilityDate: 2021-11-28T09:30:17.820Z
  },
  members: [
    {
      _id: 0,
      name: 'ip-x-x-x-x.eu-central-1.compute.internal:27017',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 463,
      optime: [Object],
      optimeDate: 2021-11-28T09:34:17.000Z,
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1638091817, i: 2 }),
      electionDate: 2021-11-28T09:30:17.000Z,
      configVersion: 3,
      configTerm: 1,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'mongodb-secondary.ecs.endpoint:27017',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 190,
      optime: [Object],
      optimeDurable: [Object],
      optimeDate: 2021-11-28T09:34:17.000Z,
      optimeDurableDate: 2021-11-28T09:34:17.000Z,
      lastHeartbeat: 2021-11-28T09:34:25.244Z,
      lastHeartbeatRecv: 2021-11-28T09:34:25.292Z,
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'ip-x-x-x-x.eu-central-1.compute.internal:27017',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 3,
      configTerm: 1
    },
    {
      _id: 2,
      name: 'mongodb-arbiter.ecs.endpoint:27017',
      health: 1,
      state: 7,
      stateStr: 'ARBITER',
      uptime: 183,
      lastHeartbeat: 2021-11-28T09:34:25.243Z,
      lastHeartbeatRecv: 2021-11-28T09:34:25.388Z,
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      configVersion: 3,
      configTerm: 1
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1638092057, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("aaf0912cce77ca5f6013c6a5add706c28d0beed3", "hex"), 0),
      keyId: Long("7035550781860216838")
    }
  },
  operationTime: Timestamp({ t: 1638092057, i: 1 })
}

结论:

在 AWS ECS 上创建 MongoDB 集群后,使用默认或具有所有节点端点的自定义配置启动副本集。