[错误:无法从NodeJS连接到[localhost:27017]]到mongodb [英] [Error: failed to connect to [localhost:27017]] from NodeJS to mongodb

查看:82
本文介绍了[错误:无法从NodeJS连接到[localhost:27017]]到mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下示例代码从NodeJS连接到MongoDB时遇到问题.我尝试运行带有或不带有sudo的"mongod",但是nodejs代码仍然无法连接.我能够使用"mongo"成功连接到数据库.

I am having problem connecting to MongoDB from NodeJS using following sample code. I tried running "mongod" with or without sudo but nodejs code still fail to connect. I am able to successfully connect to database using "mongo".

运行于:MAC OS 10.6.8

Running on : MAC OS 10.6.8

   var mongoClient = require('mongodb').MongoClient;
   mongoClient.connect("mongodb://localhost:27017/test", function(error, db) {
        if(!error){
             console.log("We are connected");
        }
        else
           console.dir(error);
    });

在代码上方运行以下错误:[错误:无法连接到[localhost:27017]]

get following error running above code : [Error: failed to connect to [localhost:27017]]

也尝试过猫鼬,但出现类似错误:

Also tried mongoose but similar error:

    var mongoose = require('mongoose');
    mongoose.connect('mongodb://localhost/test');
    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function callback () {
          console.log("DB connected");
    // yay!
    });

输出:连接错误:[错误:无法连接到[localhost:27017]]

Output: connection error: [Error: failed to connect to [localhost:27017]]

这是mongod的日志

Here is the log of mongod

mongod --help for help and startup options
2014-07-11T23:33:47.843-0700 kern.sched unavailable
2014-07-11T23:33:47.849-0700 [initandlisten] MongoDB starting : pid=29942 port=27017 dbpath=/data/db 64-bit host=usc8bcc8a0d0b1
2014-07-11T23:33:47.849-0700 [initandlisten]
2014-07-11T23:33:47.849-0700 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2014-07-11T23:33:47.849-0700 [initandlisten] db version v2.6.3
2014-07-11T23:33:47.849-0700 [initandlisten] git version: nogitversion
2014-07-11T23:33:47.849-0700 [initandlisten] build info: Darwin usc8bcc8a0d0b1 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:33:36 PDT 2011; root:xnu-1504.15.3~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_49
2014-07-11T23:33:47.849-0700 [initandlisten] allocator: system
2014-07-11T23:33:47.849-0700 [initandlisten] options: {}
2014-07-11T23:33:47.850-0700 [initandlisten] journal dir=/data/db/journal
2014-07-11T23:33:47.850-0700 [initandlisten] recover : no journal files present, no recovery needed
2014-07-11T23:33:47.901-0700 [initandlisten] waiting for connections on port 27017
2014-07-11T23:34:47.901-0700 [clientcursormon] mem (MB) res:48 virt:2810
2014-07-11T23:34:47.901-0700 [clientcursormon]  mapped (incl journal view):320
2014-07-11T23:34:47.901-0700 [clientcursormon]  connections:0

推荐答案

没关系,我能够通过使用127.0.0.1而不是localhost来解决此问题,不知道为什么我必须使用ip地址.我的tomcat,apache,redis甚至节点服务器都可以使用localhost来工作,但不能使用mongodb.是否需要更改配置才能使其在本地主机上工作?

Never mind, I was able to resolve the issue by using 127.0.0.1 instead of localhost, not sure why I have to use ip address. My tomcat, apache, redis, and even node server all works using localhost but not the mongodb. Is there is config I need change to make it work using local host?

这篇关于[错误:无法从NodeJS连接到[localhost:27017]]到mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆