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

前端 未结 6 842
暗喜
暗喜 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.

提交回复
热议问题