MongoDB报错"not authorized on root to execute command"问题解决一例
用root用户在修改MongoDB认证机制的时候报错:
>use admin
>db.auth('root','123456')
> var schema=db.system.version.findOne({"_id" : "authSchema"})
Y> schema.currentVersion = 3
3
> db.system.version.save(schema)
WriteResult({
"writeError" : {
"code" : 13,
"errmsg" : "not authorized on root to execute command { update: \"system.version\", updates: [ { q: { _id: \"authSchema\" }, u: { _id: \"authSchema\", currentVersion: 3 }, multi: false, upsert: true } ], ordered: true }"
}
})
此错误是因为没有授权给admin用户对system.version表执行命令的权限,解决方法如下:
> db.grantRolesToUser ( "root", [ { role: "__system", db: "admin" } ] )
再次执行命令成功:
> db.system.version.findOne({"_id" : "authSchema"})
{ "_id" : "authSchema", "currentVersion" : 3 }
修改需求:MongoDB3.0当前认证机制为SCRAM-SHA-1,需要改为老的认证机制MONGODB-CR
然后删除以前的root用户 db.dropUser("root")
use centralDatabase
db.createUser(
{
user: "root",
pwd:"12346",
roles:[
{ role: "readWrite", db:"centralDatabase" }
]
}
)
然后
use admin
查看认证机制
db.system.users.find()