What MongoDB user privileges do I need to add a user to a new/another mongo database?

后端 未结 2 1027
生来不讨喜
生来不讨喜 2020-12-23 15:33

I have enabled authentication in the MongoDB config file after adding one admin user with the following privileges: userAdmin and userAdminAnyDatabase

2条回答
  •  伪装坚强ぢ
    2020-12-23 15:46

    The mistake made was that I was using an userAdminAnyDatabase user that was NOT in the admin database (see this note). So there MUST be a database called admin on your server! As the documentation says for the "AnyDatabase" privileges:

    If you add any of these roles to a user privilege document outside of the admin database, the privilege will have no effect.

    So you must:

    • add a userAdminAnyDatabase to the admin db

      $ mongo admin
      > db.createUser({ user: "myadmin", pwd: "1234", roles: ["userAdminAnyDatabase"] })
      
    • turn on authentication

      auth = true
      setParameter = enableLocalhostAuthBypass=0
      
    • connect using the new myadmin user to any database you want and add further users:

      $ mongo another -u myadmin -p 1234
      > db.createUser({ user: "user", pwd: "1234", roles: ["readWrite"] })
      

      or

      > use another
      > db.createUser({ user: "user", pwd: "1234", roles: ["readWrite"] })
      

提交回复
热议问题