无法将猫鼬连接到 Atlas-MongoDB

Fail to connect mongoose to Atlas-MogoDB

我已将我的IP列入白名单,并且我已正确替换URI字符串中的用户名和密码(包括密码中的特殊字符),但它不起作用。我也试过允许从任何地方访问,但结果是一样的。我收到此错误:

Server running on port 5000
MongooseError [MongooseServerSelectionError]: Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.
    at new MongooseServerSelectionError (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\error\serverSelection.js:24:11)
    at NativeConnection.Connection.openUri (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\connection.js:823:32)
    at Mongoose.connect (C:\Users\Stella\Desktop\apps\mern-auth\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (C:\Users\Stella\Desktop\apps\mern-auth\server.js:11:10)
    at Module._compile (internal/modules/cjs/loader.js:1139:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1159:10)
    at Module.load (internal/modules/cjs/loader.js:988:32)
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  message: "Could not connect to any servers in your MongoDB Atlas cluster. Make sure your current IP address is on your Atlas cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/.",
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map(3) {
      'mern-stuff-shard-00-02-1tqpi.mongodb.net:27017' => [ServerDescription],
      'mern-stuff-shard-00-00-1tqpi.mongodb.net:27017' => [ServerDescription],
      'mern-stuff-shard-00-01-1tqpi.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

这是我的代码:

require("dotenv").config();
const express = require('express');
const mongoose = require('mongoose');

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

app.use(express.json());

mongoose.connect(ATLAS_URI, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true
   })
  .then(() => console.log('mongoDB connected'))
  .catch(err => console.log(err));

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

我看过帖子,看过视频,也花了几个小时,但我无法解决这个问题。有什么想法吗?

检查您的连接凭据字符串的格式是否正确,因此它包含 HTML 代码而不只是文本,即 %24。

这是我不久前用过的一个例子。我也有一个错误,它找不到测试数据库,所以也改变了 URI 中的那个。

mongodb+srv://USERNAME:PASSWORD@cluster0-feebe.mongodb.net/DATABASE_NAME?retryWrites=true&w=majority

原来是我的密码导致了这个问题。我选择了一个带有一些特殊字符的字符,尽管我遵循 instructions in the documentation 如何对它们进行编码,但不知何故它不起作用。我终止了集群,使用不包含特殊字符的密码创建了一个集群,并且成功了。