mongodb Mongoose服务器选择错误:连接ECONNREFUSED::1:27017无法修复

yvgpqqbh  于 2022-11-03  发布在  Go
关注(0)|答案(8)|浏览(377)

我已经尝试了2个多小时,试图找出这个数据库的问题所在。我已经尝试了所有方法。从重新安装服务器,重新启动进程,重新启动等等。在尝试连接时,它总是给我这个错误:

  1. const serverSelectionError = new ServerSelectionError();
  2. ^
  3. MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
  4. at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
  5. at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
  6. at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
  7. at new Promise (<anonymous>)
  8. at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
  9. at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
  10. at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
  11. at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
  12. at D:\TheShed\MX_\app.js:195:37
  13. at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
  14. reason: TopologyDescription {
  15. type: 'Unknown',
  16. servers: Map(1) {
  17. 'localhost:27017' => ServerDescription {
  18. _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
  19. address: 'localhost:27017',
  20. type: 'Unknown',
  21. hosts: [],
  22. passives: [],
  23. arbiters: [],
  24. tags: {},
  25. minWireVersion: 0,
  26. maxWireVersion: 0,
  27. roundTripTime: -1,
  28. lastUpdateTime: 1421094,
  29. lastWriteDate: 0,
  30. error: MongoNetworkError: connect ECONNREFUSED ::1:27017
  31. at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
  32. at Socket.<anonymous> (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:267:22)
  33. at Object.onceWrapper (node:events:510:26)
  34. at Socket.emit (node:events:390:28)
  35. at emitErrorNT (node:internal/streams/destroy:164:8)
  36. at emitErrorCloseNT (node:internal/streams/destroy:129:3)
  37. at processTicksAndRejections (node:internal/process/task_queues:83:21)
  38. }
  39. },
  40. stale: false,
  41. compatible: true,
  42. heartbeatFrequencyMS: 10000,
  43. localThresholdMS: 15,
  44. logicalSessionTimeoutMinutes: undefined
  45. }
  46. }

这个错误无论我怎么做都无法解决。MongoDB服务器正在运行,我已经通过〉show dbs检查过了,它列出了它们,而且很好。另外,C:/data/db也存在,也很好。我该怎么做?下面是我的连接代码:

  1. (async () => {
  2. await require('./other/DB/mong')();
  3. console.log("Connected to Database.");
  4. })();

这里是./other/DB/mong:

  1. var mongoose = require("mongoose");
  2. module.exports = async () => {
  3. await mongoose.connect('mongodb://localhost:27017/MX', {
  4. keepAlive: true,
  5. useNewUrlParser: true,
  6. useUnifiedTopology: true
  7. });
  8. return mongoose;
  9. }
5ktev3wc

5ktev3wc1#

我刚刚在互联网上找到了一个解决方案,如果你使用的是最新的nodejs(v17.x),然后尝试更新mongodb的网址从localhost127.0.0.1
这对我很有效:微微微笑的脸:

pokxtpni

pokxtpni2#

默认情况下,MongoDB不绑定到ipv6上的localhost。
如果您希望它侦听::1,则需要启用net.ipv6,并启用net.bindIpAll或在net.bindIp中显式列出环回地址

k0pti3hp

k0pti3hp3#

将我的mongodb网址从“mongodb://localhost:27017/student”更新为“mongodb://127.0.0.1:27017/student”,它对我来说运行得很好

hgb9j2n6

hgb9j2n64#

常量选项= {

  1. useNewUrlParser: true,
  2. useUnifiedTopology: true,
  3. serverSelectionTimeoutMS: 5000,
  4. autoIndex: false, // Don't build indexes
  5. maxPoolSize: 10, // Maintain up to 10 socket connections
  6. serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
  7. socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
  8. family: 4 // Use IPv4, skip trying IPv6

}

p4rjhz4m

p4rjhz4m5#

您也可以使用mongodb:0.0.0.0:27017/database_name

z9zf31ra

z9zf31ra6#

更新自
将“mongodb://本地主机:27017/我的应用程序”更改为“mongodb://127.0.0.1:27017:27017/我的应用程序”
然后检查以确保其连接。
(进程环境数据库,{使用新URL解析器:false,使用统一拓扑:如果数据库已连接,则返回数据库连接状态。

vpfxa7rd

vpfxa7rd7#

使用mongoose.connect(“mongodb:127.0.0.1:27017/databasename“);而不是本地主机

yks3o0rb

yks3o0rb8#

步骤1.检查MongoDB服务是否正在运行-〉如果正常。
下一步:步骤2.从localhost替换127.0.0.1-〉这应该100%有效

相关问题