How do I add an admin user to Mongo in 2.6?

前端 未结 3 1763
忘掉有多难
忘掉有多难 2020-12-25 15:20

I upgraded from 2.4 to 2.6 and authentication broke. This tutorial seems pretty straightforward but I keep getting locked out of my own database. My situation is pretty si

相关标签:
3条回答
  • 2020-12-25 15:49

    Apparently the "system user administrator" isn't enough. Create a root user:

    > db.createUser({user:"someadmin",pwd:"secret", roles:[{role:"root",db:"admin"}]})
    

    Then add your database user:

    > use some_db
    > db.createUser(
        {
          user: "mongouser",
          pwd: "someothersecret",
          roles: ["readWrite"]
        }
    )
    

    More details on this gist. Comments on gist and better answers on SO welcome - I'm not a sys admin

    0 讨论(0)
  • 2020-12-25 15:56

    Even after following @Tony's method I was getting a

    `com.mongodb.CommandFailureException:`
    

    Adding

    compile 'org.mongodb:mongo-java-driver:2.13.1'
    

    in Dependency section of BuildConfig.groovy however fixed the issue.

    0 讨论(0)
  • 2020-12-25 16:16

    1) The role that you assign the admin user- userAdminAnyDatabase - doesn't have unlimited privileges. It's just a role that is allowed to create and manage users on any database. Apparently, by default it is restricted from executing certain commands that are not directly related to managing database users (such as fetching the startup warnings from the log, querying the server status, etc.).

    You can use the 'root' role instead as Tony suggests. If you are going to use the root account to do setup and management and then just have a few basic read/write privileged accounts talking to the database, this probably makes the most sense.

    2) In general, connecting on the client side just requires calling the db.authenticate() function after connecting from your client code. There are different ways to do this depending on the driver/language that you are using for a client. The node.js driver code is pretty typical: http://mongodb.github.io/node-mongodb-native/api-generated/db.html#authenticate

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