简体   繁体   English

MongoError:首次连接时无法连接到服务器[localhost:27017](ECONNREFUSED)

[英]MongoError: failed to connect to server [localhost:27017] on first connect (ECONNREFUSED)

I just added MongoDB to the dependencies of my Node.js project created with the npm init command. 我刚刚将MongoDB添加到使用npm init命令创建的Node.js项目的依赖项中。 My index.js code is: 我的index.js代码是:

var MongoClient = require('mongodb').MongoClient
, assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/mydatabase';

// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("Connected successfully to server");

db.close();
});

But when I execute the code it throws the following error: 但是,当我执行代码时,它将引发以下错误:

AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]

I really don't know how to fix it. 我真的不知道该如何解决。 I followed some guides that reported the same error but I couldn't fix it. 我遵循了一些报告相同错误的指南,但无法修复。

Should I poen a port on my modem? 我应该在调制解调器上插入端口吗? Should I replace "localhost" with my IP? 我应该用我的IP替换“ localhost”吗? Should I do anything else? 我还应该做其他事情吗?

Please help me! 请帮我!

UPDATE: 更新:

I installed a MongoDB server on my Android device and replacing the url variable with mongodb://ANDROID-DEVICE-LOCAL-IP:27017/mydatabase it now works. 我在Android设备上安装了MongoDB服务器,并用mongodb://ANDROID-DEVICE-LOCAL-IP:27017/mydatabase替换了url变量,它现在可以工作了。

How can I accomplish it placing the database on my computer? 我如何完成将数据库放置在计算机上的工作? Is the firewall blocking incoming connections? 防火墙是否阻止传入连接? Is this why it works on Android but not on Windows? 这就是为什么它可以在Android上但不能在Windows上运行的原因?

I fixed the issue! 我已解决问题!

I ran mongod -dbpath "MY-PATH" in the cmd and now it works. 我在cmd中运行mongod -dbpath "MY-PATH" ,现在可以使用了。

The error occurred because nothing was listening on 27017 port. 发生错误是因为27017端口上没有监听任何内容。 Now the mongod program is listening for connections on that port and so the connection from my project is no more refused. 现在mongod程序正在侦听该端口上的连接,因此不再拒绝来自我的项目的连接。

Make a configuration file like config.js 制作一个像config.js这样的配置文件

module.exports = {
    'secretKey': '12345-67890-09876-54321',
    'mongoUrl' : 'mongodb://localhost:27017/cubs'
}

And require that file in your server code 并在服务器代码中要求该文件

var config = require('./config');
var mongoose = require('mongoose');
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    console.log("Connected correctly to server");
});

暂无
暂无

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

相关问题 MongoError:首次连接时无法连接到服务器localhost:27017 - Mongoerror: failed to connect to server localhost:27017 on first connect MongoError:无法连接到服务器[localhost:27017] - MongoError: failed to connect to server [localhost:27017] MongoError:无法连接到服务器[localhost:27017] - MongoError: failed to connect to server [localhost:27017] node.js-错误:首次连接时无法连接到服务器[localhost:27017] [MongoNetworkError:连接ECONNREFUSED 127.0.0.1:27017] - node.js - Error: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] MongoNetworkError: 第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] - MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017] 连接到MongoDB时出错= MongoError:首次连接时无法连接到服务器[localhost:27017] - Error when connecting to MongoDB = MongoError: failed to connect to server [localhost:27017] on first connect MongoError:连接 ECONNREFUSED 127.0.0.1:27017 - MongoError: connect ECONNREFUSED 127.0.0.1:27017 故障 - MongoError:无法连接到服务器 [未定义:27017] - Glitch - MongoError: failed to connect to server [undefined:27017] UnhandledPromiseRejectionWarning: MongoNetworkError: 在第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError] - UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError MongoNetworkError:首次连接时无法连接到服务器[localhost:27017] - MongoNetworkError: failed to connect to server [localhost:27017] on first connect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM