You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ngrok暴露MongoDB副本集远程连接失败及免费部署方案咨询

问题背景

我在Ubuntu虚拟机本地部署了一套包含3个节点的MongoDB副本集,配置信息如下:
ReplicaSet Configuration

三个节点分别监听同一主机的27018、27019、27020端口,当前均正常运行,执行rs.status()的输出结果如下:

{
  set: 'Dist',
  date: ISODate("2022-06-24T01:07:36.657Z"),
  myState: 1,
  term: Long("24"),
  syncSourceHost: '',
  syncSourceId: -1,
  heartbeatIntervalMillis: Long("2000"),
  majorityVoteCount: 2,
  writeMajorityCount: 2,
  votingMembersCount: 3,
  writableVotingMembersCount: 3,
  optimes: {
    lastCommittedOpTime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
    lastCommittedWallTime: ISODate("2022-06-24T01:07:28.662Z"),
    readConcernMajorityOpTime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
    appliedOpTime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
    durableOpTime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
    lastAppliedWallTime: ISODate("2022-06-24T01:07:28.662Z"),
    lastDurableWallTime: ISODate("2022-06-24T01:07:28.662Z")
  },
  lastStableRecoveryTimestamp: Timestamp({ t: 1656032848, i: 1 }),
  electionCandidateMetrics: {
    lastElectionReason: 'electionTimeout',
    lastElectionDate: ISODate("2022-06-23T23:45:44.849Z"),
    electionTerm: Long("24"),
    lastCommittedOpTimeAtElection: { ts: Timestamp({ t: 0, i: 0 }), t: Long("-1") },
    lastSeenOpTimeAtElection: { ts: Timestamp({ t: 1656027744, i: 1 }), t: Long("21") },
    numVotesNeeded: 2,
    priorityAtElection: 1,
    electionTimeoutMillis: Long("10000"),
    numCatchUpOps: Long("0"),
    newTermStartDate: ISODate("2022-06-23T23:45:44.968Z"),
    wMajorityWriteAvailabilityDate: ISODate("2022-06-23T23:45:47.178Z")
  },
  electionParticipantMetrics: {
    votedForCandidate: false,
    electionTerm: Long("23"),
    lastVoteDate: ISODate("2022-06-23T23:45:36.502Z"),
    electionCandidateMemberId: 2,
    voteReason: 'already voted for another candidate (localhost:27018) this term (23)',
    lastAppliedOpTimeAtElection: { ts: Timestamp({ t: 1656027744, i: 1 }), t: Long("21") },
    maxAppliedOpTimeInSet: { ts: Timestamp({ t: 1656027744, i: 1 }), t: Long("21") },
    priorityAtElection: 1
  },
  members: [
    {
      _id: 0,
      name: 'localhost:27018',
      health: 1,
      state: 1,
      stateStr: 'PRIMARY',
      uptime: 5067,
      optime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
      optimeDate: ISODate("2022-06-24T01:07:28.000Z"),
      lastAppliedWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      lastDurableWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      syncSourceHost: '',
      syncSourceId: -1,
      infoMessage: '',
      electionTime: Timestamp({ t: 1656027944, i: 1 }),
      electionDate: ISODate("2022-06-23T23:45:44.000Z"),
      configVersion: 1,
      configTerm: 24,
      self: true,
      lastHeartbeatMessage: ''
    },
    {
      _id: 1,
      name: 'localhost:27019',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 4933,
      optime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
      optimeDurable: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
      optimeDate: ISODate("2022-06-24T01:07:28.000Z"),
      optimeDurableDate: ISODate("2022-06-24T01:07:28.000Z"),
      lastAppliedWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      lastDurableWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      lastHeartbeat: ISODate("2022-06-24T01:07:36.097Z"),
      lastHeartbeatRecv: ISODate("2022-06-24T01:07:35.253Z"),
      pingMs: Long("0"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'localhost:27018',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 24
    },
    {
      _id: 2,
      name: 'localhost:27020',
      health: 1,
      state: 2,
      stateStr: 'SECONDARY',
      uptime: 4933,
      optime: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
      optimeDurable: { ts: Timestamp({ t: 1656032848, i: 1 }), t: Long("24") },
      optimeDate: ISODate("2022-06-24T01:07:28.000Z"),
      optimeDurableDate: ISODate("2022-06-24T01:07:28.000Z"),
      lastAppliedWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      lastDurableWallTime: ISODate("2022-06-24T01:07:28.662Z"),
      lastHeartbeat: ISODate("2022-06-24T01:07:35.263Z"),
      lastHeartbeatRecv: ISODate("2022-06-24T01:07:35.263Z"),
      pingMs: Long("178"),
      lastHeartbeatMessage: '',
      syncSourceHost: 'localhost:27018',
      syncSourceId: 0,
      infoMessage: '',
      configVersion: 1,
      configTerm: 24
    }
  ],
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1656032848, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1656032848, i: 1 })
}
已尝试操作

我需要将该副本集暴露到公网,供express.js项目中的mongoose实例连接调用。我尝试使用Ngrok做TCP隧道映射三个节点端口,使用的ngrok.yml配置如下:

version: "2"
authtoken: <my token here>
tunnels:
 db1:
  proto: tcp
  addr: 27018
 db2:
  proto: tcp
  addr: 27019
 db3:
  proto: tcp
  addr: 27020

执行ngrok start all命令启动所有隧道后,Ngrok终端输出正常:
Ngrok terminal output

遇到的问题

我在Windows端使用MongoDB Compass,通过URI mongodb://2.tcp.eu.ngrok.io:17012,0.tcp.eu.ngrok.io:16864,7.tcp.eu.ngrok.io:13873/dist1?replicaSet=Dist 测试连接时出现报错:
MongoDB Compass Error

我无法定位连接失败的原因,且暂时无法购买Ngrok付费服务获取固定TCP会话资源,现咨询是否有其他可行的替代方案,能够部署完全自主可控(支持自主启停节点)的MongoDB副本集,满足课程项目的使用需求。


内容的提问来源于stack exchange,提问作者miserylord

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 02:33:07