跳到主内容

ubuntu mongodb认证失败,报错了SCRAM-SHA-1 authentication failed, storedKey mismatch

· 2分钟阅读

问题

我的服务器是 ubuntu 系统,原先安装的是 3.0.2,但是今天发现无法连上数据库,认证失败,报错了 SCRAM-SHA-1 authentication failed, storedKey mismatch

复现步骤

  1. 新增一个用户
>mongo admin
MongoDB shell version: 3.2.7
connecting to: admin
> db.createUser({createUser:"admin",pwd:"123456",roles:["root"]})
Successfully added user: { "createUser" : "admin", "roles" : [ "root" ] }
  1. 打开认证配置 mongod.cfg
systemLog:
destination: file
path: c:\data\log\mongod.log
storage:
dbPath: c:\data\db
security:
authorization: enabled
  1. 重启 mongodb

  2. 使用账号和密码连接 mongodb 数据库

>mongo admin -u admin -p 123456
MongoDB shell version: 3.2.7
connecting to: admin
2022-06-14T12:25:02.376+0200 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2
exception: login failed
  1. 通过查看日志,看到日志有一行错误。
SCRAM-SHA-1 authentication failed for admin on admin from client 127.0.0.1 ; AuthenticationFailed: SCRAM-SHA-1 authentication failed, storedKey mismatch

原因

经过几天几夜排查,发现错误的原因竟然是。。。

db.createUser() 的时候创建错了,应该是

db.createUser({ createUser : "admin", pwd : "admin", roles : ["root"] })
^^^^ not `createUser`

解决

最后改下 createUseruser 就可以了,真的是超级大傻逼

db.createUser({ user : "admin", pwd : "admin", roles : ["root"] })
^^^^ not `createUser`