MongoDB中创建root的角色失败:Error couldn’t add user No role named root@test

问题描述

使用Django操作MongoDB,在创建用户的时候,使用下面操作:

> db.createUser({user: 'abc', pwd: 'abc', roles: ['root']})
Successfully added user: { "user" : "abc", "roles" : [ "root" ] }

但是在Django中,执行 makemigrations 操作却提示

Error: Authentication failed.

在MongoDB的Shell里执行auth操作也提示类似的错误;

> db.auth('root_1', 'root')
Error: Authentication failed.
0

解决

1

Google找到了说是新版的MongoDB需要改一个设置,如下

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

但是执行后还是同样的问题…

2

继续找,发现了这篇文章,文章提到是用户角色导致的问题

原来是:
之前的dbAdmin,只是针对于Database Administration Roles这些种类的权限
而root本身来说,貌似?(反正没有完全看懂)只是针对于admin数据库的,或者说只能在admin数据库中授权root这个role
-》因为root权限太大了

于是尝试赋予 dbAdmin 角色

> db.createUser({user: 'abc', pwd: 'abc', roles: ['dbAdmin']})
Successfully added user: { "user" : "abc", "roles" : [ "dbAdmin" ] }

再次执行 makemigrations 以及 migrate 操作,不再提示问题,遂解决

3

官网关于角色这块写的感觉有点晦涩…

参考

https://www.crifan.com/mongodb_create_normal_gridfs_root_user_fail_error_couldnt_add_user_no_role_named_root_gridfs/

posted @ 2020-03-09 11:02  wswang  阅读(5566)  评论(0编辑  收藏  举报