show dbs gives “Not Authorized to execute command” error

前端 未结 7 1380
暖寄归人
暖寄归人 2020-12-24 13:42

I\'ve spent some time trying to figure out what is wrong but as I could not find out, I decided to ask here.

I am running MongoDB(Windows 64-bit 2008 R2+) version 3.

7条回答
  •  别那么骄傲
    2020-12-24 14:18

    one more, after you create user by following cmd-1, please assign read/write/root role to the user by cmd-2. then restart mongodb by cmd "mongod --auth".

    The benefit of assign role to the user is you can do read/write operation by mongo shell or python/java and so on, otherwise you will meet "pymongo.errors.OperationFailure: not authorized" when you try to read/write your db.

    cmd-1:

    use admin
    db.createUser({
      user: "newUsername",
      pwd: "password",
      roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
    })
    

    cmd-2:

    db.grantRolesToUser('newUsername',[{ role: "root", db: "admin" }])
    

提交回复
热议问题