MongoError:auth在openshift上使用mongoose失败

时间:2016-07-06 04:40:14

标签: node.js mongodb mongoose openshift

我有以下连接模式并收到“MongoError:auth failed”。也许是猫鼬版的问题? Openshift有MongoDB 2.4和RockMongo 1.1谢谢。

// Connection URL
var connection_string = 'mongodb://127.0.0.1:27017/my_app';

if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
  connection_string = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ":" +
  process.env.OPENSHIFT_MONGODB_DB_PASSWORD + "@" +
  process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
  process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
  process.env.OPENSHIFT_APP_NAME;
}

// Connect to mongodb
var connect = function () {
    mongoose.connect(connection_string);
};
connect();
var db = mongoose.connection;
db.on('disconnected', connect);

1 个答案:

答案 0 :(得分:0)

我通过卸载RockMongo 1.1并直接从mongodb shell再次创建数据库来解决了这个问题。并将连接字符串更改为:

 var db_name = 'database_name';
if(process.env.OPENSHIFT_MONGODB_DB_URL){
  var mongodb_connection_string = process.env.OPENSHIFT_MONGODB_DB_URL + db_name;
}
 // Connect to mongodb
var connect = function () {
    mongoose.connect(mongodb_connection_string);
};
connect();

var db = mongoose.connection;

db.on('error', function(error){
    console.log("Error loading the db - "+ error);
});

db.on('disconnected', connect);