我已经尝试了2个多小时,试图找出这个数据库的问题所在。我已经尝试了所有方法。从重新安装服务器,重新启动进程,重新启动等等。在尝试连接时,它总是给我这个错误:
const serverSelectionError = new ServerSelectionError();
^
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
at NativeConnection.Connection.openUri (D:\TheShed\MX_\node_modules\mongoose\lib\connection.js:797:32)
at D:\TheShed\MX_\node_modules\mongoose\lib\index.js:330:10
at D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
at new Promise (<anonymous>)
at promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
at Mongoose._promiseOrCallback (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:1151:10)
at Mongoose.connect (D:\TheShed\MX_\node_modules\mongoose\lib\index.js:329:20)
at module.exports (D:\TheShed\MX_\other\DB\mong.js:4:20)
at D:\TheShed\MX_\app.js:195:37
at Object.<anonymous> (D:\TheShed\MX_\app.js:197:3) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'localhost:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
address: 'localhost:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 1421094,
lastWriteDate: 0,
error: MongoNetworkError: connect ECONNREFUSED ::1:27017
at connectionFailureError (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:293:20)
at Socket.<anonymous> (D:\TheShed\MX_\node_modules\mongodb\lib\cmap\connect.js:267:22)
at Object.onceWrapper (node:events:510:26)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
logicalSessionTimeoutMinutes: undefined
}
}
这个错误无论我怎么做都无法解决。MongoDB服务器正在运行,我已经通过〉show dbs检查过了,它列出了它们,而且很好。另外,C:/data/db也存在,也很好。我该怎么做?下面是我的连接代码:
(async () => {
await require('./other/DB/mong')();
console.log("Connected to Database.");
})();
这里是./other/DB/mong:
var mongoose = require("mongoose");
module.exports = async () => {
await mongoose.connect('mongodb://localhost:27017/MX', {
keepAlive: true,
useNewUrlParser: true,
useUnifiedTopology: true
});
return mongoose;
}
8条答案
按热度按时间5ktev3wc1#
我刚刚在互联网上找到了一个解决方案,如果你使用的是最新的nodejs(v17.x),然后尝试更新mongodb的网址从
localhost
到127.0.0.1
这对我很有效:微微微笑的脸:
pokxtpni2#
默认情况下,MongoDB不绑定到ipv6上的localhost。
如果您希望它侦听::1,则需要启用net.ipv6,并启用net.bindIpAll或在net.bindIp中显式列出环回地址
k0pti3hp3#
将我的mongodb网址从“mongodb://localhost:27017/student”更新为“mongodb://127.0.0.1:27017/student”,它对我来说运行得很好
hgb9j2n64#
常量选项= {
}
p4rjhz4m5#
您也可以使用mongodb:0.0.0.0:27017/database_name
z9zf31ra6#
更新自
将“mongodb://本地主机:27017/我的应用程序”更改为“mongodb://127.0.0.1:27017:27017/我的应用程序”
然后检查以确保其连接。
(进程环境数据库,{使用新URL解析器:false,使用统一拓扑:如果数据库已连接,则返回数据库连接状态。
vpfxa7rd7#
使用mongoose.connect(“mongodb:127.0.0.1:27017/databasename“);而不是本地主机
yks3o0rb8#
步骤1.检查MongoDB服务是否正在运行-〉如果正常。
下一步:步骤2.从localhost替换127.0.0.1-〉这应该100%有效