【发布时间】:2020-10-12 19:04:49
【问题描述】:
我正在使用 A2 共享托管服务器来托管我的 node express 应用程序。我提供以下 URI 以连接到 mongoose。
const uri = mongodb+srv://<username>:<password>@cluster0-gwff9.gcp.mongodb.net/customer-data?retryWrites=true&w=majority
猫鼬连接功能是:
mongoose.connect(uri, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true}).then(() => {
console.log("Successfully connected to the database"); }).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();});
我已将主机的公共和私有 IP 地址列入白名单。然后当我运行npm start 时也会出现以下错误:
Could not connect to the database. Exiting now... { 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://docs.atlas.mongodb.com/security-whitelist/
at NativeConnection.Connection.openUri (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/connection.js:826:32)
at Mongoose.connect (/home/cleanlab/apps/prelaunch/node_modules/mongoose/lib/index.js:335:15)
at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/model/mongodb.js:5:10)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
at Module.require (internal/modules/cjs/loader.js:650:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/home/cleanlab/apps/prelaunch/server/server.js:22:1)
at Module._compile (internal/modules/cjs/loader.js:702:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
at Module.load (internal/modules/cjs/loader.js:612:32)
at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
at Function.Module._load (internal/modules/cjs/loader.js:543:3)
message:
'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://docs.atlas.mongodb.com/security-whitelist/',
reason:
TopologyDescription {
type: 'ReplicaSetNoPrimary',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers:
Map {
'cluster0-shard-00-02-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-01-gwff9.gcp.mongodb.net:27017' => [ServerDescription],
'cluster0-shard-00-00-gwff9.gcp.mongodb.net:27017' => [ServerDescription] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null } }
我还将笔记本电脑的公共 IP 列入白名单。当我在本地运行相同的代码时,它会连接到数据库。它只是不能在共享服务器上工作并给出上述错误。如何纠正这个问题?
【问题讨论】:
-
您的传出IP地址是否可能与传入IP地址不同?在某些情况下,您可能无法获得静态传出 IP 地址。如果您对主机有 shell 访问权限,请尝试运行
npx --package public-ip-cli public-ip以查看 ip -
我在您发布的输出中的任何地方都看不到实际错误。如果您使用的库没有提供它,则这是这些库中的错误。
标签: node.js mongodb mongoose shared-hosting mongodb-atlas