我正在为我的项目使用nodeJS,并使用mongoose模块连接到mongo db。使用用户名/密码方法的连接工作正常。但是,当更改为X509身份验证时,我无法连接到数据库。
连接到数据库时引发错误-"err":{"name":"MongoError","message":"auth failed","ok":0,"errmsg":"auth failed","code":18}}
node version - v8.11.3
npm version - 5.6.0
Mongo Db version - 3.4.14
mongoose version in package json - "^4.8.7"
下面的代码段
var mongoose = require("mongoose");
mongoose.Promise = global.Promise
var connection = null;
var dbString = "mongodb://host1:10901,host2:1902/dbname?replicaSet=replset&readPreference=secondaryPreferred";
var fs = require('fs')
connection = mongoose.createConnection(dbString, {
user: encodeURIComponent("C=US,ST=California,O=Company Inc.,OU=management:group.123456,CN=dbname-role-user-repl.mydomain.com"),
auth: { authMechanism: "MONGODB-X509", authSource: '$external' },
server: {
ssl: true,
sslValidate: true,
sslCA: fs.readFileSync(path to CA file),
sslCert: fs.readFileSync(path to cert file),
sslKey: fs.readFileSync(path to kep file),
sslPass: "password"
}
});
connection.once("open", function () {
console.log("App is connected to DB -- " + dbString);
});
connection.once("close", function () {
console.log("App disconnected from DB -- " + dbString);
});
connection.on("error", function (err) {
console.error("App failed to connect to DB -- " + dbString, err);
});
module.exports = {
connection: connection,
mongoose: mongoose
}
还有其他人遇到类似的问题吗?
答案 0 :(得分:1)
如果对其他人有帮助,我找到了解决方法,下面是解决方法。
将猫鼬更新到版本5.2.5并删除用户字符串上的编码(即encodingURIComponent)解决了该问题。