[英]MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 wont get fixed
I've been trying for over 2 hours now trying to figure out what's wrong with this database.我已经尝试了 2 个多小时,试图弄清楚这个数据库出了什么问题。 I've tried everything.
我已经尝试了一切。 From reinstalling the server, restarting the processes, rebooting and so much more.
从重新安装服务器、重新启动进程、重新启动等等。 It keeps giving me this error when trying to connect:
尝试连接时它一直给我这个错误:
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
}
}
This error wont resolve no matter what I do.无论我做什么,这个错误都不会解决。 The MongoDB server is running, I've checked by doing >show dbs and it lists them perfectly fine.
MongoDB 服务器正在运行,我已经通过执行 >show dbs 进行了检查,它完美地列出了它们。 Also, C:/data/db exists and its fine too.
此外,C:/data/db 存在并且也很好。 What do I do?
我该怎么办? Here's my connection code:
这是我的连接代码:
(async () => {
await require('./other/DB/mong')();
console.log("Connected to Database.");
})();
and here's./other/DB/mong:这是./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;
}
MongoDB does not bind to localhost on ipv6 by default.默认情况下,MongoDB 不会绑定到 ipv6 上的 localhost。
If you want it to listen on ::1 you will need to enable net.ipv6 and either enable net.bindIpAll or explicitly list the loopback address in net.bindIp如果您希望它在 ::1 上侦听,则需要启用net.ipv6并启用net.bindIpAll或在net.bindIp中明确列出环回地址
I just found a solution in the internet , If you are using latest nodejs (v17.x) , then try updating mongodb url from localhost
to 127.0.0.1
我刚刚在互联网上找到了一个解决方案,如果您使用的是最新的 nodejs (v17.x),请尝试将 mongodb url 从
localhost
更新为127.0.0.1
This worked for me :slightly_smiling_face:这对我有用:slightly_smiling_face:
将我的 mongodb url 从 'mongodb://localhost:27017/student' 更新为 'mongodb://127.0.0.1:27017/student' 对我来说效果很好
你也可以使用 mongodb://0.0.0.0:27017/database_name
update from更新自
'mongodb://localhost:27017/myapp' to 'mongodb://127.0.0.1:27017:27017/myapp' 'mongodb://localhost:27017/myapp' 到 'mongodb://127.0.0.1:27017:27017/myapp'
then check to be sure it connects.然后检查以确保它连接。
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true, useUnifiedTopology: true }).then(() => console.log('DB Connected')); mongoose.connect(process.env.DATABASE, { useNewUrlParser: true, useUnifiedTopology: true }).then(() => console.log('DB Connected'));
const options = {常量选项 = {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
autoIndex: false, // Don't build indexes
maxPoolSize: 10, // Maintain up to 10 socket connections
serverSelectionTimeoutMS: 5000, // Keep trying to send operations for 5 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
} }
use mongoose.connect("mongodb://127.0.0.1:27017/databasename");使用 mongoose.connect("mongodb://127.0.0.1:27017/databasename"); instead of localhost
而不是本地主机
step 1. check MongoDB service is running -> if OK.步骤 1. 检查 MongoDB 服务是否正在运行 -> 如果正常。
try next: step 2. replace 127.0.0.1 from localhost -> this should work 100%尝试下一步:第 2 步。从本地主机替换 127.0.0.1 -> 这应该可以 100%
just change this, mongoose.connect('mongodb://localhost:27017/Database-name');只需更改此,mongoose.connect('mongodb://localhost:27017/Database-name'); ---> mongoose.connect("mongodb://127.0.0.1:27017/Database-name");
---> mongoose.connect("mongodb://127.0.0.1:27017/Database-name");
If you are using latest nodejs.如果您使用的是最新的 nodejs。 If connecting fails on your machine, try using 127.0.0.1 instead of localhost like mongoose.connect('mongodb://127.0.0.1:27017/myapp');
如果在您的计算机上连接失败,请尝试使用 127.0.0.1 而不是本地主机,例如mongoose.connect('mongodb://127.0.0.1:27017/myapp');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.