Not authorized for query on admin.system.namespaces on mongodb

前端 未结 6 828
暗喜
暗喜 2021-02-02 00:07

I start a new mongo instance, create a user, authorize it, but when I run \"show collections\", the system says that the id is not authorized. I do not know why?



        
相关标签:
6条回答
  • 2021-02-02 00:46

    As Robert says, admin users has only rights to admin, not to write in databases. So you have to create a custom user for your database. There's different ways. I have choose the dbOwner way.

    (I use Ubuntu Server, mongo 2.6.3 and Robomongo)

    So to do this, fisrt create your admin user like mongo says :

    type mongo in your linux shell

    and these command in the mongo shell :

    use admin
    db.createUser({user:"mongoadmin",pwd:"chooseyouradminpassword",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
    db.auth("mongoadmin","chooseyouradminpassword")
    exit
    

    edit the mongo conf file with :

    nano /etc/mongod.conf
    

    You can use vi if nano is not installed.

    activate authentication by uncommented/adding these line auth=true

    if you want to use Robomongo from other machine change the line bind_ip=127.0.0.1 by bind_ip=0.0.0.0 (maybe you should add more protection in production).

    type in linux shell :

    service mongod restart
    mongo
    

    And in mongo shell :

    use admin
    db.auth("mongoadmin","pwd:"chooseyouradminpassword")
    use doomnewdatabase
    db.createUser({user:"doom",pwd:"chooseyourdoompassword",customData:{desc:"Just me as I am"},roles : [{role:"dbOwner",db:"doomnewdatabase"}]})
    db.auth("doom","chooseyourdoompassword")
    show collections
    

    (customData is not required).

    If you want to try if it works, type this in the mongo shell :

    db.products.insert( { item: "card", qty: 15 } )
    show collections
    db.products.find()
    

    Good luck ! Hope it will help you and others !

    I have search this informations for hours.

    0 讨论(0)
  • 2021-02-02 00:53

    I had the same problem and this is how I solved it:

    db = db.getSiblingDB('admin')
    db.addUser( 
        { user: "mongoadmin", 
          pwd: "adminpass", 
          roles: ['clusterAdmin', 'userAdminAnyDatabase', 'readAnyDatabase'] } )
    
    0 讨论(0)
  • 2021-02-02 00:53

    I would try granting the read role to the user. userAdminAnyDatabase grants the ability to administer users.

    0 讨论(0)
  • 2021-02-02 00:54

    For MongoDB version 2.6 use:

    db.createUser(
    {
       user: "testUser"
       pwd: "password",
       roles: [{role: "readWrite", db:"yourdatabase"}]
    })
    

    See the docs

    0 讨论(0)
  • 2021-02-02 00:57

    I had the same problem, but I found this tutorial and it helped me.

    http://www.hacksparrow.com/mongodb-add-users-and-authenticate.html

    use:

    db.addUser('sa', 'sa')
    

    instead of

    db.addUser({user:"sa",pwd:"sa",roles:["userAdminAnyDatabase"]})
    {
            "user" : "sa",
            "pwd" : "75692b1d11c072c6c79332e248c4f699",
            "roles" : [
                    "userAdminAnyDatabase"
            ],
            "_id" : ObjectId("519deedff788eb914bc429b5")
    }
    
    0 讨论(0)
  • 2021-02-02 01:06

    I solved it like so for mongoDB 2.6 + currently 3

    db.createUser(
      {
        user: "username",
        pwd: "password",
        roles: [ { role: "root", db: "admin" } ]
      }
    )
    

    note that for the role filed instead of userAdminAnyDatabase we use root

    0 讨论(0)
提交回复
热议问题