简体   繁体   English

连接 ECONNREFUSED 127.0.0.1:27017'

[英]connect ECONNREFUSED 127.0.0.1:27017'

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.js it 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 --save in my directory我已经使用npm install mongoose --save in my directory

Yeah there are dozens question like this but none of these helped me.. I'm new at nodejs and probably missing something是的,有很多这样的问题,但这些都没有帮助我..我是 nodejs 的新手,可能遗漏了一些东西

I was also facing the same issue, when I was executing node server on my project directory.当我在我的项目目录上执行node server时,我也面临同样的问题。 For me the MongoDB service was not started, that makes this issue.对我来说, MongoDB服务没有启动,这就是这个问题。

So I had to run services.msc and activated the service.所以我不得不运行services.msc并激活服务。

在此处输入图像描述

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

run services.msc and activate the Mongodb service.运行services.msc并激活Mongodb服务。

Now Mongodb will connect现在 Mongodb 将连接

the status of the Mongodb indicated as Running Mongodb 的状态指示为Running

在此处输入图像描述

Your mongodb service is probably down.您的 mongodb 服务可能已关闭。 Run sudo service mongod start to start the daemon process运行sudo service mongod start启动守护进程

I was having the same problem, and found that it was a mongod issue.(I am running Ubuntu 16.04).我遇到了同样的问题,发现这是一个 mongod 问题。(我正在运行 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服务设置为开机启动。

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

I know this issue is old, but i came across a similar issue and the above solutions did not work for me, I'm using Ubuntu 20.04 LTS.我知道这个问题很老,但我遇到了类似的问题,上述解决方案对我不起作用,我使用的是 Ubuntu 20.04 LTS。

What i did to make it work was just running mongo service using this command:我所做的只是使用以下命令运行 mongo 服务:

$ mongod

Then everything worked fine然后一切正常

如果您在 Windows 中并且遇到此问题,只需转到 mongodb 的安装程序 exe 应用程序,然后单击“修复”这对我有用

try this:尝试这个:

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

c:\data\db is 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!也许一切都会好起来的!

Follow as below:如下:

=> run services.msc and start mondodb server => 运行 services.msc 并启动 mondodb 服务器

and save the file in node project, you can see the server connected!!并将文件保存在节点项目中,您可以看到服务器已连接!

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通信

在此处输入图像描述

In Most of the case you will get the error bcz the service MongoDB Database Server (MongoDB) Might be stopped.在大多数情况下,您将收到错误 bcz 服务MongoDB 数据库服务器 (MongoDB)可能已停止。

Just start the service to be connected with DB只需启动要与数据库连接的服务在此处输入图像描述

it's doesn't work as localhost but IP address works 127.0.0.1 and solves this problem:它不能作为localhost工作,但 IP 地址工作127.0.0.1并解决了这个问题:

  // .connect("mongodb://localhost:27017/lofydb")

    const mongoose = require("mongoose");

    mongoose.connect("mongodb://127.0.0.1:27017/lofydb")
      // .connect("mongodb://127.0.0.1:27017")
    
      .then(() => {
        console.log("Connected to Database");
      })
      .catch((err) => {
        console.log("Not Connected to Database ERROR! ", err);
      });
  1. please check the MongoDB service is active and running.请检查 MongoDB 服务是否处于活动状态并正在运行。 Refer the running services参考正在运行的服务

  2. If it is running and available on MongoDB compass or on MongoDB shell and if you are working with node version >17.0.0 or so it will give error.如果它在 MongoDB compass 或 MongoDB shell 上运行并且可用,并且如果您使用的节点版本>17.0.0左右,它将给出错误。 So plz change to the stable version of node.所以请更改为稳定版本的节点。

Make sure MongdoDB is running确保 MongdoDB 正在运行

To run MongoDB as a macOS service, run:要将 MongoDB 作为 macOS 服务运行,请运行:

brew services start mongodb-community@5.0酿造服务启动 mongodb-community@5.0

And when you see:当你看到:

==> Successfully started mongodb-community (label: homebrew.mxcl.mongodb-commu ==> 成功启动mongodb-community (标签:homebrew.mxcl.mongodb-commu

OR type on command line, to check the mongo service.或者在命令行输入,检查 mongo 服务。

mongod蒙神

After run service, You can easily connect with mongdoDB localhost or remote connection.运行服务后,您可以轻松连接 mongdoDB localhost 或远程连接。

I had the same issue and it had to do with the fact that my MongoDB service wasn't running locally.我遇到了同样的问题,这与我的 MongoDB 服务没有在本地运行有关。 Make sure you followed all the correct installation steps for whatever OS you are trying to run MongoDB service here: https://www.mongodb.com/docs/manual/administration/install-community/确保您在此处尝试运行 MongoDB 服务的任何操作系统的所有正确安装步骤: https://www.mongodb.com/docs/install-community/community/community/administration

Then follow the respective guide for the running the service under "Run MongoDB Community Edition".然后按照“运行 MongoDB 社区版”下运行服务的相应指南。 So for example, for macOS you would do因此,例如,对于 macOS,您会这样做

brew services start mongodb-community@6.0

if you wanted to run as a macOS service, or如果您想作为 macOS 服务运行,或者

mongod --config /opt/homebrew/etc/mongod.conf --fork

if you wanted to run manually as a background service on Apple M1 processor.如果您想在 Apple M1 处理器上作为后台服务手动运行。 The same page I shared above also has commands you can run for verifying if your MongoDB process is running.我在上面分享的同一个页面也有你可以运行的命令来验证你的 MongoDB 进程是否正在运行。

Step 1: open conf file to edit.第一步:打开conf文件进行编辑。

sudo vim /etc/mongod.conf

Step 2: find port and change it.第2步:找到端口并更改它。

net: port: 27017 -> default port before any change net: port: 27017 -> 任何更改前的默认端口

After changing port, my issue solved:更改端口后,我的问题解决了:

暂无
暂无

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

相关问题 MongoError:连接 ECONNREFUSED 127.0.0.1:27017 - MongoError: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 NodeJS和Heroku:连接ECONNREFUSED 127.0.0.1:27017 - NodeJS and Heroku: connect ECONNREFUSED 127.0.0.1:27017 MongooseServerSelectionError:在 windows 7 中连接 ECONNREFUSED 127.0.0.1:27017 - MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 in windows 7 UnhandledPromiseRejectionWarning:MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017 - UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017 无法连接到节点客户端。 错误:连接ECONNREFUSED 127.0.0.1:27017 - Can not Connect to Node Client. Error: connect ECONNREFUSED 127.0.0.1:27017 无法在部署中连接到 MongoDb (Heroku):连接 ECONNREFUSED 127.0.0.1:27017 - Can´t connect to MongoDb in deployment (Heroku): connect ECONNREFUSED 127.0.0.1:27017 节点app.js返回错误:连接ECONNREFUSED 127.0.0.1:27017 - Node app.js returns Error: connect ECONNREFUSED 127.0.0.1:27017 在 Docker Compose 中获取“MongooseServerSelectionError:连接 ECONNREFUSED 127.0.0.1:27017” - Getting "MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017" in Docker Compose 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]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM