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

MongooseServerSelectionError:Express后端无法连接MongoDB求助

React+Express+MongoDB全栈应用数据库连接异常问题

我正在开发基于React+Express后端+MongoDB的全栈应用,但server.js无法连接数据库。使用.env中的同一URI可通过MongoDB Compass成功连接,因此我认为并非错误提示中的IP问题。

完整错误信息

MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. 
Make sure your current IP address is on your Atlas cluster's IP whitelist.
    at _handleConnectionErrors (C:\Users\evanf\Documents\projects\BartenderWebsite\backend\node_modules\mongoose\lib\connection.js:909:11)
    at NativeConnection.openUri (C:\Users\evanf\Documents\projects\BartenderWebsite\backend\node_modules\mongoose\lib\connection.js:860:11) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
    'name of address' => ServerDescription {
      address: 'name of address',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 771190951,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'name of address' => ServerDescription {
      address: 'name of address',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 771191064,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    },
    'name of address' => ServerDescription {
      address: 'name of address',
      type: 'Unknown',
      hosts: [],
      passives: [],
      arbiters: [],
      tags: {},
      minWireVersion: 0,
      maxWireVersion: 0,
      roundTripTime: -1,
      minRoundTripTime: 0,
      lastUpdateTime: 771190960,
      lastWriteDate: 0,
      error: [MongoNetworkError],
      topologyVersion: null,
      setName: null,
      setVersion: null,
      electionId: null,
      logicalSessionTimeoutMinutes: null,
      primary: null,
      me: null,
      '$clusterTime': null
    }
  },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-dqamup-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
[nodemon] clean exit - waiting for changes before restart

数据库连接代码

// Express app
const app = express()

// Middleware to parse JSON
app.use(express.json())

mongoose.connect(process.env.MONGO_URI)
    .then(() => {
        // Listen for requests
        app.listen(process.env.PORT, () => {
            console.log('Connected to DB & listening on port 4000!')
            })
    })
    .catch(err => {
        console.log(err)
    })

已尝试的解决方案

  • 直接在代码中写入MONGO_URI文本
  • 修改错误的用户名密码(仍出现相同错误)
  • 修改错误库名(出现不同错误)
  • 尝试连接本地MongoDB
  • 设置Atlas允许所有IP访问
  • 更新MongoDB相关依赖

内容的提问来源于stack exchange,提问作者Evan Fisher-Perez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 04:42:14