mongodb 非 admin 库 认证登陆失败 原因(百度好多都 是渣)db.addUser() 请走开。

首先先晒一下log 日志错误信息

2016-07-13T22:19:43.667+0800 I ACCESS   [conn4]  authenticate db: finddemo { aut
henticate: 1, nonce: "xxx", user: "user1", key: "xxx" }
2016-07-13T22:19:43.668+0800 I ACCESS   [conn4] Failed to authenticate user1@fin
ddemo with mechanism MONGODB-CR: AuthenticationFailed: UserNotFound: Could not f
ind user user1@finddemo
2016-07-13T22:20:12.555+0800 I ACCESS   [conn2] SCRAM-SHA-1 authentication faile
d for user1 on finddemo from client 127.0.0.1 ; UserNotFound: Could not find use
r user1@finddemo
2016-07-13T22:20:17.127+0800 I NETWORK  [initandlisten] connection accepted from
 127.0.0.1:56103 #5 (4 connections now open)
2016-07-13T22:20:17.127+0800 I ACCESS   [conn5]  authenticate db: admin { authen
ticate: 1, nonce: "xxx", user: "root", key: "xxx" }
2016-07-13T22:20:17.146+0800 I NETWORK  [initandlisten] connection accepted from
 127.0.0.1:56104 #6 (5 connections now open)
2016-07-13T22:20:17.146+0800 I ACCESS   [conn6]  authenticate db: admin { authen
ticate: 1, nonce: "xxx", user: "root", key: "xxx" }

着重看一下 这个报错信息,

SCRAM-SHA-1 authentication failed for user1 on finddemo

这是什么鸟?   

mongodb加入了SCRAM-SHA-1校验方式,需要第三方工具配合进行验证,下面给出具体解决办法: 

首先关闭认证,修改system.version文档里面的authSchema版本为3,初始安装时候应该是5,命令行如下: 

> use admin 
switched to db admin 
>  var schema = db.system.version.findOne({"_id" : "authSchema"}) 
> schema.currentVersion = 3 
3 
> db.system.version.save(schema) 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })  

下面是获取到的用户的相关信息

{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "97a156792bd180dc82cec9a56d838991" }, "roles" : [ { "role" : "__system", "db" : "admin" } ] }
{ "_id" : "finddemo.finddemo", "user" : "finddemo", "db" : "finddemo", "credentials" : { "MONGODB-CR" : "4b8dce39de95d553473c1998f43aa165" }, "roles" : [ { "role" : "dbOwner", "db" : "finddemo" } ] }

重新连接,成功。

可以查看一下链接介绍:

https://docs.mongodb.com/master/release-notes/3.0-scram/

还有国外友人的回答:(越来越鄙视百度党)

http://stackoverflow.com/questions/29006887/mongodb-cr-authentication-failed

  

原文地址:https://www.cnblogs.com/timelesszhuang/p/5668589.html