node.js 连接 ECONNREFUSED 127.0.0.1:27017'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36400233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:05:57  来源:igfitidea点击:

connect ECONNREFUSED 127.0.0.1:27017'

node.jsmongodb

提问by gsiradze

I have that code:

我有那个代码:

var express = require('express'),
    stylus = require('stylus'),
    logger = require('morgan'),
    bodyParser = require('body-parser'),
    mongoose = require('mongoose');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

function compile(str, path){
    return stylus(str).set('filename', path);
}

app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(stylus.middleware(
        {
            src: __dirname + '/public',
            compile: compile
        }
    ));
app.use(express.static(__dirname + '/public'));

mongoose.connect('mongodb://localhost/multivision');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error..'));
db.once('open', function callback(){
    console.log('multivision db opened');
});

app.get('/partials/:partialPath', function(req, res){
    res.render('partials/' + req.params.partialPath);
});

app.get('*', function(req, res) {
  res.render('index');
});

var port = 3131;
app.listen(port);
console.log('Listening on port ' + port + '...');

but when I'm trying nodemon server.jsit throws an error:

但是当我尝试时nodemon server.js它会引发错误:

connection error.. { [MongoError: connect ECONNREFUSED 127.0.0.1:27017] name: 'MongoError' message: 'connect ECONNREFUSED 127.0.0.1:27017' }

连接错误.. { [MongoError: connect ECONNREFUSED 127.0.0.1:27017] name: 'MongoError' message: 'connect ECONNREFUSED 127.0.0.1:27017' }

how can I improve that? I've already installed mongoose using npm install mongoose --savein my directory

我该如何改进?我已经npm install mongoose --save在我的目录中安装了猫鼬

Yeah there are dozens question like this but none of these helped me.. I'm new at nodejs and probably missing something

是的,有很多这样的问题,但这些都没有帮助我..我是 nodejs 的新手,可能遗漏了一些东西

回答by Satheesh kumar

run services.mscand activate the Mongodbservice.

运行services.msc并激活Mongodb服务。

Now Mongodb will connect

现在 Mongodb 将连接

click here to view the image

单击此处查看图像

the status of the Mongodb indicated as Running

Mongodb 的状态指示为Running

回答by Alberto Centelles

Your mongodb service is probably down. Run sudo service mongod startto start the daemon process

您的 mongodb 服务可能已关闭。运行sudo service mongod start以启动守护进程

回答by froglegs

I was having the same problem, and found that it was a mongod issue.(I am running Ubuntu 16.04). Looking through the errors there was a directory missing. After adding the directory, I needed to change permissions, and finally, set the mongod service to start at boot.

我遇到了同样的问题,发现这是一个 mongod 问题。(我正在运行 Ubuntu 16.04)。查看错误,发现缺少一个目录。添加目录后,我需要更改权限,最后,将mongod服务设置为开机启动。

$ sudo mkdir -p /data/db
$ sudo chown -R $USER /data/db
$ sudo systemctl enable mongod.service

回答by Seven

try this:

尝试这个:

mongod.exe --dbpath c:\data\db

mongod.exe --dbpath c:\data\db

c:\data\dbis the place where you put your db.

c:\data\db是您放置数据库的地方。

and when you see something like this :

当你看到这样的东西时:

2016-08-18T10:22:31.020+0800 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-18T10:22:31.022+0800 I CONTROL  [initandlisten] MongoDB starting : pid=4356 port=27017 dbpath=c:\data\db 64-bit host=sevencai-PC0
2016-08-18T10:22:31.022+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-18T10:22:31.023+0800 I CONTROL  [initandlisten] db version v3.2.8
2016-08-18T10:22:31.023+0800 I CONTROL  [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0

......

......

and then node yourserver.js

然后节点 yourserver.js

maybe everything will be fine!

也许一切都会好起来的!

回答by Sibeesh Venu

I was also facing the same issue, when I was executing node serveron my project directory. For me the MongoDBservice was not started, that makes this issue.

当我node server在我的项目目录上执行时,我也面临着同样的问题。对我来说,MongoDB服务没有启动,这导致了这个问题。

So I had to run services.mscand activated the service.

所以我必须运行services.msc并激活该服务。

enter image description here

在此处输入图片说明

After that I was able to run my command.

之后我就可以运行我的命令了。

D:\SVenu\MyApp>node server
Saving User
App is listening on port: 3000
Already Exist
Already Exist
Already Exist
Already Exist
Done save

回答by Yilmaz

here is the problem:

这是问题所在:

mongoose.connect('mongodb://localhost/multivision') 

The default port for mongodb and mongos instances is 27017.

mongodb 和 mongos 实例的默认端口是 27017。

mongoose.connect('mongodb://localhost:27017/multivision') 

also make sure that mongod is running so u can communicate with the mongodb

还要确保 mongod 正在运行,以便您可以与 mongodb 通信