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

React+NextJS项目连接MongoDB Atlas出现ETIMEDOUT错误求助

MongoDB Atlas连接超时(MongoNetworkError/MongoServerSelectionError)问题

问题描述

开发React+Next.js应用已一月,使用免费版MongoDB Atlas作为数据库,通过Next.js的api文件夹实现后端。此前一切正常,近期突然出现两类连接超时错误,且旧的课程项目也出现同样问题,未使用VPN修改IP。

错误1:MongoNetworkError

error - MongoNetworkError: connect ETIMEDOUT 18.166.253.100:27017
    at connectionFailureError (C:\Users\asus\Desktop\mymart-seller\node_modules\mongodb\lib\cmap\connect.js:383:20)
    at TLSSocket.<anonymous> (C:\Users\asus\Desktop\mymart-seller\node_modules\mongodb\lib\cmap\connect.js:307:22)
    at Object.onceWrapper (node:events:628:26)
    at TLSSocket.emit (node:events:513:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  cause: Error: connect ETIMEDOUT 18.166.253.100:27017
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) {
    errno: -4039,
    code: 'ETIMEDOUT',
    syscall: 'connect',
    address: '18.166.253.100',
    port: 27017
  },
  connectionGeneration: 0,
  page: '/64374cbc7f69b08c1cd4e68c/categories/Category/product%201',
  [Symbol(errorLabels)]: Set(1) { 'ResetPool' }

错误2:MongoServerSelectionError

error - MongoServerSelectionError: connect ETIMEDOUT 18.166.253.100:27017
    at Timeout._onTimeout (C:\Users\asus\Desktop\mymart-seller\node_modules\mongodb\lib\sdam\topology.js:277:38)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-rtwqyt0-shard-00-01.cln6x4h.mongodb.net:27017' => [ServerDescription],
      'ac-rtwqyt0-shard-00-02.cln6x4h.mongodb.net:27017' => [ServerDescription],
      'ac-rtwqyt0-shard-00-00.cln6x4h.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-hup2a6-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: 30
  },
  code: undefined,
  page: '/api/new-variation',
  [Symbol(errorLabels)]: Set(0) {}
}

询问:错误由何导致?是否有需要调整的设置?MongoDB是否正在维护或资源已满?


可能的原因及解决方案

1. IP白名单配置问题

  • 检查MongoDB Atlas控制台的网络访问设置,确认当前公网IP已加入白名单。即使没开VPN,ISP可能动态更换了你的公网IP,导致原白名单IP失效。
  • 临时测试可将白名单设置为0.0.0.0/0(允许所有IP访问),但测试后务必改回真实IP,避免安全风险。

2. 网络环境限制

  • 本地网络可能存在防火墙、路由器规则或运营商限制,阻断了27017端口的出站连接。可尝试切换网络(比如手机热点)测试是否能正常连接。
  • 检查本地防火墙是否允许Node.js进程访问外部网络。

3. MongoDB Atlas状态或资源问题

  • 查看MongoDB Atlas控制台的集群状态,确认集群是否正常运行,没有处于维护或异常状态。免费版集群偶尔会有短暂的维护窗口,但通常会提前通知。
  • 免费版有存储和连接数限制:存储上限512MB,并发连接数上限100。可在控制台查看监控面板,确认是否达到资源限制(比如存储已满、连接数超标)。

4. 连接字符串配置问题

  • 确认Next.js项目中使用的MongoDB连接字符串是否正确,特别是集群地址、用户名密码、认证数据库(默认是admin)。如果连接字符串有误,也会导致超时。
  • 尝试从MongoDB Atlas控制台重新生成连接字符串,替换项目中的配置。

5. 驱动版本兼容性

  • 检查项目中mongodb驱动的版本,是否与MongoDB Atlas的服务器版本兼容。部分旧版本驱动可能存在连接问题,尝试升级到最新稳定版。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:04:55