Cannot authenticate into mongo, “auth fails”

前端 未结 12 1228
无人及你
无人及你 2020-11-30 21:40

I\'ve created an admin user for mongo using these directions:

http://docs.mongodb.org/manual/tutorial/add-user-administrator/

From the mongo client it looks

相关标签:
12条回答
  • 2020-11-30 22:17

    In MongoDB 3.0, it now supports multiple authentication mechanisms.

    1. MongoDB Challenge and Response (SCRAM-SHA-1) - default in 3.0
    2. MongoDB Challenge and Response (MONGODB-CR) - previous default (< 3.0)

    If you started with a new 3.0 database with new users created, they would have been created using SCRAM-SHA-1.

    So you will need a driver capable of that authentication:

    http://docs.mongodb.org/manual/release-notes/3.0-scram/#considerations-scram-sha-1-drivers

    If you had a database upgraded from 2.x with existing user data, they would still be using MONGODB-CR, and the user authentication database would have to be upgraded:

    http://docs.mongodb.org/manual/release-notes/3.0-scram/#upgrade-mongodb-cr-to-scram

    Now, connecting to MongoDB 3.0 with users created with SCRAM-SHA-1 are required to specify the authentication database (via command line mongo client), and using other mechanisms if using a driver.

    $> mongo -u USER -p PASSWORD --authenticationDatabase admin

    In this case, the "admin" database, which is also the default will be used to authenticate.

    0 讨论(0)
  • 2020-11-30 22:17

    It appears the problem is that a user created via the method described in the mongo docs does not have permission to connect to the default database (test), even if that user was created with the "userAdminAnyDatabase" and "dbAdminAnyDatabase" roles.

    0 讨论(0)
  • 2020-11-30 22:17

    The proper way to login into mongo shell is

    mongo localhost:27017 -u 'uuuuu' -p '>xxxxxx' --authenticationDatabase dbname

    0 讨论(0)
  • 2020-11-30 22:21

    I also received this error, what I needed was to specify the database where the user authentication data was stored:

    mongo -u admin -p SECRETPASSWORD --authenticationDatabase admin

    Update Nov 18 2017:

    mongo admin -u admin -p
    

    is a better solution. Mongo will prompt you for your password, this way you won't put your cleartext password into the shell history which is just terrible security practice.

    0 讨论(0)
  • 2020-11-30 22:22

    Another possibility: When you created the user, you may have accidentally been useing a database other than admin, or other than the one you wanted. You need to set --authenticationDatabase to the database that the user was actually created under.

    mongodb seems to put you in the test database by default when you open the shell, so you'd need to write --authenticationDatabase test rather than --authenticationDatabase admin if you accidentally were useing test when you ran db.createUser(...).

    Assuming you have access to the machine that's running the mongodb instance, y could disable authorization in /etc/mongod.conf (comment out authorization which is nested under security), and then restart your server, and then run:

    mongo
    show users
    

    And you might get something like this:

    {
        "_id" : "test.myusername",
        "user" : "myusername",
        "db" : "test",
        "roles" : [
            {
                "role" : "dbOwner",
                "db" : "mydatabasename"
            }
        ],
        "mechanisms" : [
            "SCRAM-SHA-1",
            "SCRAM-SHA-256"
        ]
    }
    

    Notice that the db value equals test. That's because when I created the user, I didn't first run use admin or use desiredDatabaseName. So you can delete the user with db.dropUser("myusername") and then create another user under your desired database like so:

    use desiredDatabaseName
    db.createUser(...)
    

    Hopefully that helps someone who was in my position as a noob with this stuff.

    0 讨论(0)
  • 2020-11-30 22:23

    You can also try this :-

    mongo localhost:27017/admin -u admin -p SECRETPASSWORD
    

    Found it in this post

    Here obviously the localhost can be some other host and the /admin can be some other database on which authentication has been applied

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