Ningappa
Ningappa

Reputation: 1329

mongoose connection to mongodb 2.6.1 replica set auth failed

I'm trying to connect mongodb from node.js using mongoose. I'm facing problem while connecting to multiple servers. I'm able to connect to single server. If I try for connecting multiple servers (as I want to connect to replicasets ) it's throwing error auhtfailed.

var options = {
'db': {
    'native_parser': true
},
'server': {
    'auto_reconnect': true,
    'poolSize': 5,
    'socketOptions' : { 'keepAlive': 1 }
},
'replset': {
    'readPreference': 'nearest',
    'strategy': 'ping',
    'rs_name': 'rs01',
    'socketOptions' : { 'keepAlive': 1 }
}
};

var connect = mongoose.connect('mongodb://adminname:adminpassword@host1:27017,host2:27017,host3:27017/myDatabase', options , function (err) {
"use strict";
if (err) {
    console.log(err);
}else{
    console.log("connected")
}

It's showing auth failed throwing this error

{ [MongoError: auth failed] name: 'MongoError', message: 'auth failed', ok: 0, errmsg: 'auth failed', code: 18 }

Upvotes: 1

Views: 985

Answers (1)

Ningappa
Ningappa

Reputation: 1329

 mongoConnectionString : "mongodb://dbusername:dbpassword@localhost:27017/db,host2:27017/db,host3:27017/db",

mongoDbOptions : {
    db: {
        native_parser: true
    },
    server: {
        poolSize: 5
    },
    replset: {
        rs_name: 'rplname',
        readPreference: 'ReadPreference.secondaryPreferred'
    },
    pluralization: false,
    host: 'localhost',
    port : '27017'
},
mongoose.connect(mongoConnectionString, mongoDbOptions, function (error) {
    "use strict";
    if (error) {
        console.log(error);
    }
    else {
        console.log("Connection to mongo successful");
    }
});

this works for me!! Thanks;..

Upvotes: 0

Related Questions