连接到mlab失败

时间:2016-03-18 13:32:05

标签: javascript mongodb mlab

尝试使用 this article

连接到mlab
var mongo = require('mongodb');
var Server = mongo.Server;
var Db = mongo.Db;
var server = new Server('ds1234.mongolab.com', 12345, {auto_reconnect : true});
var db = new Db('db-name', server);
db.open(function(err, client) {
    client.authenticate('username', 'password', function(err, success) {
    // Do Something ...
    });
});

但有错误:

  

“connect ETIMEDOUT 40.78.24.54:12345”

并且客户端未定义,因此无法读取客户端的“身份验证”属性 我需要连接到mlab并创建一些集合,我该怎么办?

2 个答案:

答案 0 :(得分:2)

您已复制字符串" ds1234.mongolab.com"从教程。但是您无法连接到该地址,因为它不属于您。您需要在MongoLab上创建一个帐户并Follow their instructions进行连接。

答案 1 :(得分:0)

 //install mongodb: npm install mongodb --save
    var mongodb = require("mongodb");

     var db;
// Connect to the database before starting the application server.
//Create User on Mlab and replace the URL
     mongodb.MongoClient.connect("URL", function (err, database) { 
             if (err) {
              console.log(err);
              process.exit(1);
      }

      db = database;
      console.log("Database connection ready");