无法通过 mongo 的官方 node.js 驱动程序进行身份验证,只能通过 mongo shell。 (MongoDB Cloud Manager 副本部署)

Can not authenticate via mongo's official node.js driver, only mongo shell. (MongoDB Cloud Manager replica deployment)

我正在尝试对我的副本集进行身份验证,该副本集是通过 Mongo Cloud Manager 部署的,但我无法弄清楚失败的原因。

这是我在数据库中的用户 abcd:

abcdRepliaSet:PRIMARY> use abcd
switched to db abcd
show users
abcdRepliaSet:PRIMARY> show users
{
 "_id" : "abcd.abcdAppUser",
 "user" : "abcdAppUser",
 "db" : "abcd",
 "roles" : [
 {
 "role" : "readWrite",
 "db" : "abcd"
 }
 ]
}

我可以通过 mongo shell 成功连接到它:

mongo adbcd-mongo-mms.cloudapp.net:57002 -u adbcd AppUser -p hidden --authenticationDatabase adbcd

但是,我不能使用这个简单的代码:

MongoClient.connect("mongodb://abcdAppUser:hidden@abcd-mongo-mms.cloudapp.net:57001,abcd-mongo-mms.cloudapp.net:57002,abcd-mongo-mms.cloudapp.net:57003/abcd?replicaSet=abcdRepliaSet&authSource=abcd&authMechanism=SCRAM-SHA-1",
 function(err, db) {
 if(err) throw err;
 else console.log(db);
});

我也很确定我的 authMechanism 是 SCRAM-SHA-1,因为我使用 mongoldb v. 3.0.7 添加了用户并且我还使用以下方法确认了它:

db.system.users.find({user:"abcdAppUser"}).pretty()

这是 mongod 在我尝试通过 nodejs 应用程序进行身份验证时的日志:

2015-12-22T13:31:33.823+0000 I ACCESS   [conn30286] SCRAM-SHA-1 authentication failed for abcdAppUser on abcd from client 10.0.0.10 ; AuthenticationFailed SCRAM-SHA-1 authentication failed, storedKey mismatch
2015-12-22T13:31:33.894+0000 I ACCESS   [conn30288] SCRAM-SHA-1 authentication failed for abcdAppUser on abcd from client 10.0.0.10 ; AuthenticationFailed SCRAM-SHA-1 authentication failed, storedKey mismatch
2015-12-22T13:31:34.051+0000 I ACCESS   [conn30287] SCRAM-SHA-1 authentication failed for abcdAppUser on abcd from client 10.0.0.10 ; AuthenticationFailed SCRAM-SHA-1 authentication failed, storedKey mismatch
2015-12-22T13:31:34.124+0000 I ACCESS   [conn30289] SCRAM-SHA-1 authentication failed for abcdAppUser on abcd from client 10.0.0.10 ; AuthenticationFailed SCRAM-SHA-1 authentication failed, storedKey mismatch
2015-12-22T13:31:34.206+0000 I ACCESS   [conn30290] SCRAM-SHA-1 authentication failed for abcdAppUser on abcd from client 10.0.0.10 ; AuthenticationFailed SCRAM-SHA-1 authentication failed, storedKey mismatch
2015-12-22T13:31:34.263+0000 I NETWORK  [conn30288] end connection 94.61.85.76:53102 (17 connections now open)
2015-12-22T13:31:34.264+0000 I NETWORK  [conn30286] end connection 94.61.85.76:61293 (17 connections now open)
2015-12-22T13:31:34.263+0000 I NETWORK  [conn30287] end connection 94.61.85.76:58194 (17 connections now open)
2015-12-22T13:31:34.263+0000 I NETWORK  [conn30289] end connection 94.61.85.76:58537 (17 connections now open)
2015-12-22T13:31:34.264+0000 I NETWORK  [conn30290] end connection 94.61.85.76:57720 (13 connections now open)
2015-12-22T13:31:37.185+0000 I NETWORK  [conn30281] end connection 10.0.0.8:34322 (12 connections now open)

我还确保我的 node.js 驱动程序与 SCRAM 兼容,确实如此。在这一点上,我真的不明白为什么它会失败......非常欢迎任何帮助!

提前谢谢你。

此致, 佩德罗

好吧,我不确定我做错了什么,但我现在已经升级了 authSchema(很确定它已经在 v.5 上了,不管怎样),删除了同一个用户,然后用完全相同的密码和角色。现在可以了。去看看...此外,这可能是由于 https://docs.mongodb.org/v3.0/release-notes/3.0-scram/#upgrade-mongodb-cr-to-scram 但我不确定,因为我现在知道之前安装了什么版本(开发环境,所以请原谅我缺少正确的日志记录:P)

无论如何,认为这个答案最终会对将来的人有所帮助...