我已将我的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}`));
我看过帖子,看过视频,花了几个小时,但无法解决此问题。有什么想法吗?
答案 0 :(得分:1)
原来是我的密码引起了问题。我选择了带有一些特殊字符的代码,即使我遵循instructions in the documentation进行编码的方式,但还是无法正常工作。我终止了群集,并使用不包含特殊字符的密码创建了一个群集,它可以正常工作。
答案 1 :(得分:0)
请检查您的连接凭据字符串的格式设置是否正确,以便它包含HTML代码,而不仅仅是文本,即%24。
这是我前一段时间使用的示例。另外,我在无法找到测试数据库的地方遇到了一个错误,因此也要在URI中进行更改。
mongodb+srv://USERNAME:PASSWORD@cluster0-feebe.mongodb.net/DATABASE_NAME?retryWrites=true&w=majority