无法连接到mongoDB null。<anonymous> </anonymous>

时间:2015-03-25 20:35:28

标签: node.js mongodb

所以node.js不断抛出:

Error: failed to connect to [domainName.io:27017]
    at null.<anonymous> (/var/www/domainName.io/public_html/programming/projects/timeTracker/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/server.js:555:74)
    at emit (events.js:106:17)
    at null.<anonymous> (/var/www/domainName.io/public_html/programming/projects/timeTracker/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:156:15)
    at emit (events.js:98:17)
    at Socket.<anonymous> (/var/www/domainName.io/public_html/programming/projects/timeTracker/node_modules/mongojs/node_modules/mongodb/lib/mongodb/connection/connection.js:534:10)
    at Socket.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:442:13

我使用以下server.js文件生成http服务器并连接到mongoDB

 var
  2     http = require("http"),
  3     mongojs = require("mongojs");
  4
  5                 var
  6     uri ="mongodb://userName:password@domainName.io/mydb",
  7     db = mongojs.connect(uri, ["activities"]);
  8
  9 var
 10     server = http.createServer(requestHandler);
 11
 12
 13 function requestHandler(request, response) {
 14         response.writeHead(200, {"Content-Type": "text/html"});
 15         db.activities.find({"name":"mongoDB"}, function(err, records) {
 16             if(err) {
 17                 console.log("There was an error executing the database quer    y.");
 18                 response.end();
 19                 return;
 20             }
 21
 22         var
 23             html = '<h2> Activities </h2>',
 24             i = records.length;
 25
 26         while (i--){
 27                 html += '<p><b>Name:</b> '
 28                      + rescords[i].name
 29                      + ' <br /><b>Time:</b> '
 30                      + records[i].time;
 31         }
 32         response.write(html);
 33         response.end();
 34 });
 35 };
 36
 37 server.listen(8888);

这是我第一次尝试使用nodeJS运行mongoDB,所以不太确定如何解决这个问题。我正在使用默认的mongodb端口27017并创建了我认为正确的用户:

> use mydb
>db.createUser({user:"userName",pwd:"password"...})

任何帮助或方向去尝试调试都会很棒。

1 个答案:

答案 0 :(得分:0)

问题解决了! 我正在运行Debian 7,只需转到/etc/mongod.conf并发表评论 出了以下一行:

&#13;
&#13;
# Listen to local interface only. Comment out to listen on all interfaces.
#bind_ip = 127.0.0.1
&#13;
&#13;
&#13;

现在一切正常。 希望这有助于将来的某个人!