gpt4 book ai didi

node.js - cPanel Node.js 应用程序无法连接到 MongoDB Atlas 集群,但可以在 Heroku 上运行

转载 作者:行者123 更新时间:2023-12-03 12:13:29 31 4
gpt4 key购买 nike

我使用 Node.js 创建了一个网站,该网站使用 MongoDB Atlas 集群作为其数据库。
到目前为止,我在 Heroku 上托管并测试了所有内容,我没有遇到任何问题。
2 天前,我终于决定购买我想要的域名并支付适当的托管费用。
我在我的主机服务器上检查了我的 Git 存储库,并使用 cPanel -> Software -> Setup Node.js App 工具让我的网站上线。
enter image description here
我使用我 checkout 的存储库作为应用程序根,配置所有环境变量(不在图片上)并执行 NPM 安装。
但是由于某种原因,我无法建立到我的 MongoDB 数据库的连接,即使我将每个 IP 地址 (0.0.0.0/0) 都列入了白名单。
这是我的 app.js 中的代码

const connectionString = 'mongodb+srv://' + process.env.MONGODB_USER + ':' + process.env.MONGODB_PW + '@<REDACTED_APP>-y1llv.mongodb.net/<REDACTED_APP>';
mongoose
.connect(connectionString, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => console.log('Database connected.'))
.catch(err => console.log(err));
当我启动我的应用程序并打开 URL 时,我收到此错误:
App 793847 output: Server has started successfully.
App 793847 output: 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/
App 793847 output: at NativeConnection.Connection.openUri (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongoose/lib/connection.js:830:32)
App 793847 output: at Mongoose.connect (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongoose/lib/index.js:335:15)
App 793847 output: at Object.<anonymous> (/home/<REDACTED_USER>/repositories/<REDACTED_APP>/app.js:29:6)
App 793847 output: at Module._compile (internal/modules/cjs/loader.js:936:30)
App 793847 output: at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
App 793847 output: at Module.load (internal/modules/cjs/loader.js:790:32)
App 793847 output: at Function.Module._load (internal/modules/cjs/loader.js:703:12)
App 793847 output: at Module.require (internal/modules/cjs/loader.js:830:19)
App 793847 output: at Module.require (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:80:25)
App 793847 output: at require (internal/modules/cjs/helpers.js:68:18)
App 793847 output: at loadApplication (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:243:2)
App 793847 output: at setupEnvironment (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:214:2)
App 793847 output: at Object.<anonymous> (/opt/passenger-5.3.7-5.el6.cloudlinux/src/helper-scripts/node-loader.js:133:1)
App 793847 output: at Module._compile (internal/modules/cjs/loader.js:936:30)
App 793847 output: at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
App 793847 output: at Module.load (internal/modules/cjs/loader.js:790:32) {
App 793847 output: 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/",
App 793847 output: reason: TopologyDescription {
App 793847 output: type: 'ReplicaSetNoPrimary',
App 793847 output: setName: null,
App 793847 output: maxSetVersion: null,
App 793847 output: maxElectionId: null,
App 793847 output: servers: Map {
App 793847 output: '<REDACTED_APP>-shard-00-00-y1llv.mongodb.net:27017' => [ServerDescription],
App 793847 output: '<REDACTED_APP>-shard-00-02-y1llv.mongodb.net:27017' => [ServerDescription],
App 793847 output: '<REDACTED_APP>-shard-00-01-y1llv.mongodb.net:27017' => [ServerDescription]
App 793847 output: },
App 793847 output: stale: false,
App 793847 output: compatible: true,
App 793847 output: compatibilityError: null,
App 793847 output: logicalSessionTimeoutMinutes: null,
App 793847 output: heartbeatFrequencyMS: 10000,
App 793847 output: localThresholdMS: 15,
App 793847 output: commonWireVersion: null
App 793847 output: }
App 793847 output: }
经过一番搜索,我发现有些人在他们的连接选项中使用 useUnifiedTopology 标志时遇到问题。当我将其注释掉时,我会收到此错误:
App 811175 output: Server has started successfully.
App 811175 output: (node:811175) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
App 811175 output: MongoNetworkError: failed to connect to server [<REDACTED_APP>-shard-00-00-y1llv.mongodb.net:27017] on first connect [Error: connect ECONNREFUSED <REDACTED_IP>:27017
App 811175 output: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1056:14) {
App 811175 output: name: 'MongoNetworkError'
App 811175 output: }]
App 811175 output: at Pool.<anonymous> (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/topologies/server.js:438:11)
App 811175 output: at Pool.emit (events.js:209:13)
App 811175 output: at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/pool.js:562:14
App 811175 output: at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/pool.js:1009:9
App 811175 output: at /home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:31:7
App 811175 output: at callback (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:264:5)
App 811175 output: at TLSSocket.<anonymous> (/home/<REDACTED_USER>/nodevenv/repositories/<REDACTED_APP>/12/lib/node_modules/mongodb/lib/core/connection/connect.js:294:7)
App 811175 output: at Object.onceWrapper (events.js:297:20)
App 811175 output: at TLSSocket.emit (events.js:209:13)
App 811175 output: at emitErrorNT (internal/streams/destroy.js:91:8)
App 811175 output: at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
App 811175 output: at processTicksAndRejections (internal/process/task_queues.js:77:11) {
App 811175 output: name: 'MongoNetworkError'
App 811175 output: }
我不认为这是一个问题,但我可以在 cPanel 工具中配置的最新 Node.js 版本是 12.9.0,而我的应用程序使用 12.16.2。我仍然尝试更改 package.json 文件中的引擎字段,但没有结果。
我还注意到“服务器已成功启动”。日志仅在我打开 URL 后出现在我的passenger.log 文件中,而不是在我启动应用程序时立即出现(当我在我的 app.js 中调用“app.listen(port)”时会记录它)。
我不知道去哪里找什么了,尤其是因为我在 Heroku 上运行我的应用程序没有任何问题。
这让我认为问题可能出在 cPanel 设置中的某个地方。由于代码和一切都相同,这是与不存在 cPanel 的 Heroku 最明显的区别。也许有一些安全设置阻止我在 cPanel 内建立连接。
我真的希望这里有人知道如何解决这个问题:)

最佳答案

我联系了主机的支持,他们为我打开了 27017 端口。现在一切正常!我必须大声疾呼,Namecheap 有很棒的实时聊天支持员工。他们查看了我的日志文件和所有内容,并且非常耐心。

关于node.js - cPanel Node.js 应用程序无法连接到 MongoDB Atlas 集群,但可以在 Heroku 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62570924/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com