Node MongoDb { err: 'connection to [127.0.0.1:27017] timed out' }

眉间皱痕 提交于 2019-12-25 08:00:05

问题


I can connect to my mongo database from the command line by doing this:

$ mongo 127.0.0.1:27017/my_database

But when I try and connect with my node.js code I am getting back the error:

{ err: 'connection to [127.0.0.1:27017] timed out' }

Here is my code:

var mongo = require('mongodb');
var MongoClient = mongo.MongoClient;

var DB_NAME = 'my_database';
var connectionString = 'mongodb://127.0.0.1:27017/' + DB_NAME;


MongoClient.connect( connectionString, function(err, conn)
{
    console.log(err, conn);
});

I'm not finding anything to explain this error, and I don't understand why my code should be failing when I can connect from the command line.


回答1:


The issue here was the version of mongodb.
Version 1.3.23 has this error.
I upgraded mongo in my repo, thus :

$ npm uninstall mongodb --save
$ npm install mongodb --save

And that gave me version 2.2.11, and this has fixed the problem.




回答2:


I resolved this issue by restarting mongod and then restarting the Node process.



来源:https://stackoverflow.com/questions/40825680/node-mongodb-err-connection-to-127-0-0-127017-timed-out

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!