問題描述
使用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
官網關於角色這塊寫的感覺有點晦澀…