MongoError:连接ECONNREFUSED 127.0.0.1:27017 [英] MongoError: connect ECONNREFUSED 127.0.0.1:27017
问题描述
我正在使用MongoDB软件包的MongoDB使用NodeJS.当我运行mongod命令时,它工作正常,并显示等待端口27017上的连接".因此,mongod似乎正在工作.但是,当我运行节点index.js命令时,MongoClient无法正常工作并给出错误-
I'm using NodeJS wih MongoDB using mongodb package. When I run mongod command it works fine and gives "waiting for connection on port 27017". So, mongod seems to be working. But MongoClient does not work and gives error when I run node index.js command-
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
我已经安装了mongo db 3.4,我的代码是-
I have install mongo db 3.4 and my code is-
var MongoClient = require('mongodb').MongoClient;
var dburl = "mongodb://localhost:27017/test";
MongoClient.connect(dburl, function(err, db) {
if (err) {
throw err;
}
console.log('db connected');
db.close();
});
我已经在root上创建了data/db目录,并赋予了写许可权. mongod.conf文件将数据库路径作为-
I have created data/db directories on root and given write permissions. mongod.conf file takes db path as-
存储: dbPath:/var/lib/mongo
storage: dbPath: /var/lib/mongo
但是似乎它实际上是将db路径作为data/db而不是var/lib/mongo
But it seems that it is actually taking db path as data/db and not var/lib/mongo
它工作较早,但突然停止了.
It working earlier but suddenly stopped.
推荐答案
发生这种情况的原因可能是未启动 MongoDB 服务.请按照以下步骤启动它:
This happened probably because the MongoDB service isn't started. Follow the below steps to start it:
- 转到控制面板,然后单击管理工具.
- 双击服务.一个新窗口打开.
- 搜索 MongoDB.exe .右键单击它,然后选择开始.
- Go to Control Panel and click on Administrative Tools.
- Double click on Services. A new window opens up.
- Search MongoDB.exe. Right click on it and select Start.
服务器将启动.现在再次执行npm start
,这一次代码可能会起作用.
The server will start. Now execute npm start
again and the code might work this time.
这篇关于MongoError:连接ECONNREFUSED 127.0.0.1:27017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!