无法使用Mongoose和Nodemon连接到MongoDB

问题描述 投票:0回答:1

当我使用 VS Code 终端在项目的后端目录中运行

nodemon server
时,出现以下错误:

[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node server.js`
Server is running on port: 5000
H:\Project\project\backend\node_modules\mongoose\lib\connection.js:875
    err = new ServerSelectionError();
          ^

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: https://www.mongodb.com/docs/atlas/security-whitelist/
    at _handleConnectionErrors (H:\Project\project\backend\node_modules\mongoose\lib\connection.js:875:11)
    at NativeConnection.openUri (H:\Project\project\backend\node_modules\mongoose\lib\connection.js:826:11) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-zltfugd-shard-00-01.jmuakrd.mongodb.net:27017' => ServerDescription {
        address: 'ac-zltfugd-shard-00-01.jmuakrd.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 12802051,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ETIMEDOUT 35.188.3.14:27017
            at connectionFailureError (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:353:20)   
            at TLSSocket.<anonymous> (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:268:44)    
            at Object.onceWrapper (node:events:642:26)
            at TLSSocket.emit (node:events:527:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
          [cause]: Error: connect ETIMEDOUT 35.188.3.14:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
            errno: -4039,
            code: 'ETIMEDOUT',
            syscall: 'connect',
            address: '35.188.3.14',
            port: 27017
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      },
      'ac-zltfugd-shard-00-02.jmuakrd.mongodb.net:27017' => ServerDescription {
        address: 'ac-zltfugd-shard-00-02.jmuakrd.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 12802059,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ETIMEDOUT 35.192.144.244:27017
            at connectionFailureError (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:353:20)   
            at TLSSocket.<anonymous> (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:268:44)    
            at Object.onceWrapper (node:events:642:26)
            at TLSSocket.emit (node:events:527:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
          [cause]: Error: connect ETIMEDOUT 35.192.144.244:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
            errno: -4039,
            code: 'ETIMEDOUT',
            syscall: 'connect',
            address: '35.192.144.244',
            port: 27017
          }
        },
        topologyVersion: null,
        setName: null,
        setVersion: null,
        electionId: null,
        logicalSessionTimeoutMinutes: null,
        primary: null,
        me: null,
        '$clusterTime': null
      },
      'ac-zltfugd-shard-00-00.jmuakrd.mongodb.net:27017' => ServerDescription {
        address: 'ac-zltfugd-shard-00-00.jmuakrd.mongodb.net:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 12802092,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ETIMEDOUT 35.202.144.107:27017
            at connectionFailureError (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:353:20)   
            at TLSSocket.<anonymous> (H:\Project\project\backend\node_modules\mongodb\lib\cmap\connect.js:268:44)    
            at Object.onceWrapper (node:events:642:26)
            at TLSSocket.emit (node:events:527:28)
            at emitErrorNT (node:internal/streams/destroy:157:8)
            at emitErrorCloseNT (node:internal/streams/destroy:122:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21) {
          [Symbol(errorLabels)]: Set(1) { 'ResetPool' },
          [cause]: Error: connect ETIMEDOUT 35.202.144.107:27017
              at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16) {
            errno: -4039,
            code: 'ETIMEDOUT',
            syscall: 'connect',
            address: '35.202.144.107',
            port: 27017
          }
        },
        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-r9uvf5-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined
}
[nodemon] app crashed - waiting for file changes before starting...

这是我的

server.js
中的代码:

const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, {});

const connection = mongoose.connection;
connection.once('open', () => {
    console.log("MongoDB database connection established successfully");
})

app.listen(port, () => {
    console.log(`Server is running on port: ${port}`);
});

我在 MongoDB 中有一个数据库用户为

atlasAdmin@admin
,网络访问选项卡中的 IP 地址设置为
0.0.0.0/0
,但我仍然无法连接。为什么我无法使用 Mongoose 连接到我的集群?

node.js mongodb express mongoose nodemon
1个回答
0
投票

“无法连接到 MongoDB Atlas 集群中的任何服务器。一个常见原因是您尝试从未列入白名单的 IP 访问数据库。请确保您当前的 IP 地址位于 Atlas 集群的 IP 白名单中: ”

这部分错误消息表明您当前的 IP 地址未在您的 mogodb 图集数据库中列入白名单。因此,请转到您的 mongodb 图集并确保将您当前的 IP 地址添加到白名单中,或者您可以允许“从任何地方访问”选项,但不建议这样做。

© www.soinside.com 2019 - 2024. All rights reserved.