简体   繁体   English

MongooseServerSelectionError:连接 ECONNREFUSED::1:27017

[英]MongooseServerSelectionError: connect ECONNREFUSED ::1:27017

I have a problem when I try to connect my app with my database with Mongoose. Already tried following solutions that I found on google:当我尝试使用 Mongoose 将我的应用程序与我的数据库连接时出现问题。已经尝试了以下我在谷歌上找到的解决方案:

  • restarting MongoDB service on windows在 windows 上重启 MongoDB 服务
  • manually open db with cmd located on bin file of mongodb使用位于 mongodb 的 bin 文件中的 cmd 手动打开数据库

But I can't solve it.但我无法解决它。 Can anyone help me?谁能帮我?

//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));

And throw's me, this error抛出我,这个错误

MongooseServerSelectionError: connect ECONNREFUSED::1:27017 at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32) at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5 at new Promise () at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10) at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20) at Object. (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10) MongooseServerSelectionError:连接 ECONNREFUSED::1:27017 在 NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32) 在 C:\Users \ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 在 C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback。 js:32:5 在新的 Promise () 在 promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) 在 Mongoose._promiseOrCallback (C :\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10) 在 Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\ mongoose\lib\index.js:329:20) 在 Object。 (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)
at Module._compile (node:internal/modules/cjs/loader:1095:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {在 Module._compile (node:internal/modules/cjs/loader:1095:14) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } }原因:TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } }

I try to put the port on my connection code like this我尝试像这样将端口放在我的连接代码上

//my connection
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));

and it throw's me another error它给我带来了另一个错误

MongooseServerSelectionError: Invalid message size: 1347703880, max allowed: 67108864 at NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32) at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5 at new Promise () at promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) at Mongoose._promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10) at Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:329:20) at Object. (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10) MongooseServerSelectionError:无效的消息大小:1347703880,最大允许:67108864 在 NativeConnection.Connection.openUri (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\connection.js:797:32) 在 C :\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:330:10 at C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers \promiseOrCallback.js:32:5 在新的 Promise () 在 promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10) 在 Mongoose。 _promiseOrCallback (C:\Users\ivan\Desktop\NodeJS\notes-app\node_modules\mongoose\lib\index.js:1151:10) 在 Mongoose.connect (C:\Users\ivan\Desktop\NodeJS\notes-app \node_modules\mongoose\lib\index.js:329:20) 在 Object。 (C:\Users\ivan\Desktop\NodeJS\notes-app\src\db.js:3:10)
at Module._compile (node:internal/modules/cjs/loader:1095:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {在 Module._compile (node:internal/modules/cjs/loader:1095:14) 在 Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) {
reason: TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:3000' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } }原因:TopologyDescription { type: 'Unknown', servers: Map(1) { 'localhost:3000' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, logicalSessionTimeoutMinutes: undefined } }

const uri = 'mongodb://localhost:27017/test';

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
}

const connectWithDB = () => {
    mongoose.connect(uri, options, (err, db) => {
      if (err) console.error(err);
      else console.log("database connection")
    })
}

connectWithDB()

I finally solved it.我终于解决了。

Enabling the IPV6 that MongoDB has disabled by default.启用 MongoDB 默认禁用的 IPV6。 Using the following command line on CMD:在 CMD 上使用以下命令行:

mongod --ipv6 

And then try again the connection and it works!然后再试一次连接就可以了!

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true
})
.then(db => console.log('DB is connected'))
.catch(err => console.log(err));

Posted on behalf of the question asker代表提问者发布

Simply pass third parameter family:4 ie.只需传递第三个参数family:4即。

mongoose.connect('mongodb://localhost/notes-db-app',{
    useNewUrlParser: true, 
    useUnifiedTopology: true,
    family: 4,
})

If the Error states:如果错误状态:

connect() Error :MongooseServerSelectionError: connect ECONNREFUSED ::1:27017连接()错误:MongooseServerSelectionError:连接 ECONNREFUSED ::1:27017

Then the connection to localhost is refused on the IPv6 address ::1 .然后在 IPv6 地址::1上拒绝与localhost的连接。 Mongoose per default uses IPv6 .. Mongoose 默认使用 IPv6 ..

For a quick check you can set the IPv4 address explicit:为了快速检查,您可以显式设置 IPv4 地址:

 mongoose.connect('mongodb://127.0.0.1/test')

Probably the hostname/IP of the server to which you want to connect is not correctly set.可能您要连接的服务器的主机名/IP 设置不正确。
I'm used to see that error as:我习惯于将该错误视为:

MongooseServerSelectionError: connect ECONNREFUSED <hostname/hostIP>:<port>

and in the console log you've posted, the <hostname/hostIP> part is malformed/missing.在您发布的控制台日志中, <hostname/hostIP>部分格式错误/丢失。

Example - for a mongodb server running locally on port 27017 this is the error when server is down:示例- 对于在端口27017本地运行的 mongodb 服务器,这是服务器关闭时的错误:

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

If you're using mongodb URI to connect to the db make sure that it looks like this如果您使用 mongodb URI 连接到数据库,请确保它看起来像这样

"mongodb://<hostname/hostIP>:<port>"
  1. Open your terminal and type this command: mongod打开终端并输入以下命令: mongod
  2. Then use your app.js to establish a connection by writing this code :然后使用您的 app.js 通过编写以下代码来建立连接:
     const mongoose=require("mongoose"); mongoose.connect('mongodb://localhost/notes-db-app',{ useNewUrlParser: true, useUnifiedTopology: true, family: 4, })
  3. It's done now.现在已经完成了。 Simply open your mongo shell or your mongodb compass and look for whatever you have added.只需打开您的 mongo shell 或 mongodb 指南针并查找您添加的任何内容。

Problem is, the localhost alias resolves to IPv6 address ::1 instead of 127.0.0.1问题是, localhost别名解析为 IPv6 地址::1而不是127.0.0.1

However, net.ipv6 defaults to false .但是, net.ipv6默认为false

The best option would be to start the MongoDB with this configuration:最好的选择是使用以下配置启动 MongoDB:

net:
  ipv6: true
  bindIpAll: true

or或者

net:
  ipv6: true
  bindIp: localhost

Then all variants should work:然后所有变体都应该起作用:

C:\>mongosh "mongodb://localhost:27017" --quiet --eval "db.getMongo()"
mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0

C:\>mongosh "mongodb://127.0.0.1:27017" --quiet --eval "db.getMongo()"
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0

C:\>mongosh "mongodb://[::1]:27017" --quiet --eval "db.getMongo()"
mongodb://[::1]:27017/?directConnection=true&appName=mongosh+1.6.0

If you don't run MongoDB as a service then it would be如果您不将 MongoDB 作为服务运行,那么它将是

mongod --bind_ip_all --ipv6 <other options>

NB, I don't like configuration注意,我不喜欢配置

net:
  bindIp: <ip_address>

in my opinion this makes only sense on a computer with multiple.network interfaces.在我看来,这只在具有多个网络接口的计算机上才有意义。 Use bindIp: localhost if you need to prevent any connections from remote computer (eg while maintenance or when used as backend database for a web-service), otherwise use bindIpAll: true使用bindIp: localhost如果你需要阻止来自远程计算机的任何连接(例如,在维护时或用作 Web 服务的后端数据库时),否则使用bindIpAll: true

Use this at the beginning of your code:在代码的开头使用它:

const dns = require('dns');

// Set default result order for DNS resolution
dns.setDefaultResultOrder('ipv4first');

Is MongoDB running on PORT 27017? MongoDB 是否在 PORT 27017 上运行? Make sure it's running on the specified PORT.确保它在指定的端口上运行。 See if it solves your problem.看看它是否能解决你的问题。 Also check your credentials: Mongo username and password.还要检查您的凭据:Mongo 用户名和密码。

It would have been more helpful if you had posted your source code for database connection.如果您发布了数据库连接的源代码,那会更有帮助。

I also faced the same problem those commands worked for me(Ubuntu machine)我也遇到了同样的问题,这些命令对我有用(Ubuntu 机器)

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock

Then然后

sudo service mongod restart

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ECONNREFUSED::1:27017 不会得到修复 - MongooseServerSelectionError: connect ECONNREFUSED ::1:27017 wont get fixed 在 Docker Compose 中获取“MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017” - Getting "MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017" in Docker Compose (节点:16686)UnhandledPromiseRejectionWarning:MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 - (node:16686) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017。 我已经尝试过 StackOverflow 中给出的所有解决方案,但它不起作用 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017. I have tried all solutions given in the StackOverflow, but its not working MongooseServerSelectionError:连接 ETIMEDOUT 13.250.154.115:27017 - MongooseServerSelectionError: connect ETIMEDOUT 13.250.154.115:27017 节点app.js返回错误:连接ECONNREFUSED 127.0.0.1:27017 - Node app.js returns Error: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ETIMEDOUT - MongooseServerSelectionError: connect ETIMEDOUT 错误:无法连接到[localhost:27017] - Error: failed to connect to [localhost:27017] 连接ECONNREFUSED AdonisJs邮件 - connect ECONNREFUSED AdonisJs Mail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM