我正在尝试使用猫鼬从node.js连接mongodb。我在连接到多个服务器时遇到问题。我可以连接到单个服务器。如果我尝试连接多个服务器(因为我想连接到副本集),它会抛出错误 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")
}
它显示身份验证失败抛出此错误
{ [MongoError: auth failed] name: 'MongoError', message: 'auth failed',ok: 0, errmsg: 'auth failed', code: 18 }
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");
}
});
这对我有用!!谢谢;。。