在openshift上的nodejs中无法连接mongodb

时间:2015-01-04 10:00:28

标签: node.js mongodb openshift

我正在学习nodejs / mongodb。我使用openshift来托管我的nodejs + mongodb测试代码。我可以在我的本地设置上运行相同的操作。我做了基本的更改,并试图在我的openshift设置上运行它。但是我无法这样做。

我的基本代码是

var MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
CollectionDriver = require('./collectionDriver').CollectionDriver;

  var connection_string = 'mongodb://'  + 'admin:LLEasswrd@' +
  '127.12.12.130' + ':27017' +
  '/' +
  'bugmanish';


var mongoClient = new MongoClient(connection_string); //B
mongoClient.open(function(err, mongoClient) { //C
  if (!mongoClient) {
      console.error ("process.env.OPENSHIFT_MONGODB_DB_PASSWORD["+process.env.OPENSHIFT_MONGODB_DB_PASSWORD+"]");

      console.error("Error! Exiting... Must start MongoDB first connection_string ["+connection_string+"]");
      process.exit(1); //D
  }

获取错误

Error! Exiting... Must start MongoDB first connection_string [mongodb://admin:LLEasswrd@127.12.12.130:27017/bugmanish]

Mongodb记录最后几行

Sun Jan  4 10:38:32.131 [initandlisten] allocator: tcmalloc
Sun Jan  4 10:38:32.131 [initandlisten] options: { auth: true, bind_ip: "127.12.12.130", command: [ "run" ], config: "/var/lib/openshift/54a8f64de0b8cd608a000132/mongodb//conf/mongodb.conf", dbpath: "/var/lib/openshift/54a8f64de0b8cd608a000132/mongodb/data/", nohttpinterface: "true", noprealloc: "true", pidfilepath: "/var/lib/openshift/54a8f64de0b8cd608a000132/mongodb/pid/mongodb.pid", quiet: "true", smallfiles: "true" }
Sun Jan  4 10:38:32.135 [initandlisten] journal dir=/var/lib/openshift/54a8f64de0b8cd608a000132/mongodb/data/journal
Sun Jan  4 10:38:32.135 [initandlisten] recover : no journal files present, no recovery needed
Sun Jan  4 10:38:32.922 [initandlisten] preallocateIsFaster=true 2.12
Sun Jan  4 10:38:33.923 [initandlisten] preallocateIsFaster=true 12.94
Sun Jan  4 10:38:35.823 [initandlisten] preallocateIsFaster check took 3.61 secs
Sun Jan  4 10:38:35.917 [initandlisten] waiting for connections on port 27017
Sun Jan  4 10:38:37.014 [conn1]  authenticate db: admin { authenticate: 1, nonce: "d6319d8c9990fd23", user: "admin", key: "45a07cf6275ca202367c4bdbf66095c0" }

1 个答案:

答案 0 :(得分:1)

您似乎很难对连接信息进行编码,而不是将其从ENV中拉出来。

这样的事情应该有效:

var cc          = require('config-multipaas'),
    mongojs     = require('mongojs');

var config      = cc({ 
  collection_name : process.env.COLLECTION_NAME || process.env.OPENSHIFT_APP_NAME || 'parks'
})
var db_config        = config.get('MONGODB_DB_URL'),
    collection_name  = config.get('collection_name');
var db = mongojs(db_config + collection_name, [collection_name] );

function select_all(req, res, next){
  console.log(db);
  db[collection_name].find(function(err, rows){
    if(err) {
      res.send(500, {http_status:500,error_msg: err})
      return console.error('error running query', err);
    }
    res.send(rows);
    return rows;
  });
};

我还有一些选择器示例:https://github.com/ryanj/restify-mongodb-parks/blob/master/bin/db.js

您还可以尝试使用cloud-envconfig-multipaas模块自动配置您的应用。