连接数据库时出现 ERR_SOCKET_CONNECTION_TIMEOUT

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

我已经在 mongoDb Atlas 中创建了我的 mongoDb 数据库,并尝试连接到服务器(node.js)。但我无法连接到我的数据库。

ERR_SOCKET_CONNECTION_TIMEOUT
服务器日志中显示错误。请查找下面的错误日志。

return new error_1.MongoNetworkError(err);
                   ^

MongoNetworkError: Socket connection timeout
    at connectionFailureError (D:\MERN\Project\social-media-api\node_modules\mongodb\lib\cmap\connect.js:370:20)
    at TLSSocket.<anonymous> (D:\MERN\Project\social-media-api\node_modules\mongodb\lib\cmap\connect.js:293:22)
    at Object.onceWrapper (node:events:626:26)
    at TLSSocket.emit (node:events:511:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  cause: Error [ERR_SOCKET_CONNECTION_TIMEOUT]: Socket connection timeout
      at new NodeError (node:internal/errors:399:5)
      at internalConnectMultiple (node:net:1099:20)
      at Timeout.internalConnectMultipleTimeout (node:net:1638:3)
      at listOnTimeout (node:internal/timers:575:11)
      at process.processTimers (node:internal/timers:514:7) {
    code: 'ERR_SOCKET_CONNECTION_TIMEOUT'
  },
  connectionGeneration: 18,
  [Symbol(errorLabels)]: Set(1) { 'ResetPool' }
}

Node.js v20.1.0
[nodemon] app crashed - waiting for file changes before starting...

MONGODB_URL = "mongodb+srv://<username here>:<password here>@clustername.bwmogva.mongodb.net/<database name>?retryWrites=true&w=majority"

我尝试过以下选项:

useNewUrlParser : true, socketTimeoutMS : 3000,

还尝试了socketTimeout到

30000
。检查
Atlas
中的网络设置。授予所有网络的网络访问权限。仍然遇到同样的错误。我可以通过
MongoDB compass
连接到数据库。如果有人能帮助我解决这个问题,那就太好了。

node.js mongodb hosting mern mongodb-atlas
2个回答
0
投票

问题解决了!我认为这是节点版本的错误。我已将节点版本更新至 v20.7.0。我使用的是旧版本。


0
投票

我也遇到了同样的问题,但是更新到最新的节点版本后问题就解决了

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