Mongodb – Cannot log in to Mongo from command line – using MongoDB 3.0 and WiredTiger

mongodbmongodb-3.0

NOTE: I have tried not setting bind_ip and also setting it to 0.0.0.0 — neither one fixed my issue.

This is my mongod.conf file:

systemLog:
    destination: file
    path: "/root/mongodb.log"
    logAppend: true

net:
    port: 25015

processManagement:
    fork: true

setParameter:
    enableLocalhostAuthBypass: true

storage:
    dbPath: /root/mongo_data
    engine: wiredTiger
  1. The first thing I did was SSH in to the DigitalOcean droplet where it is running and do the following:

    mongo --port 25019
    

I was able to connect, so I know it picked up the options in the mongod.conf file.

  1. The second thing I did was issue the following command:

    > use admin;
    switched to db admin
    > db.createUser(
        {
            user: "siteUserAdmin",
            pwd: "eqt-dfts-mad-ghbr-ovd-g",
            roles: [{role: "userAdminAnyDatabase", db: "admin"}]
        }
    )
    Successfully added user: {
        "user" : "siteUserAdmin",
        "roles" : [
            {
                "role" : "userAdminAnyDatabase",
                "db" : "admin"
            }
        ]
    }
    

So that was apparently a success!

  1. Next I exited from the mongo shell and then logged in to the mongo shell again as siteUserAdmin with credential from above.

  2. Then I created a user and a database for the app I'm building:

    > use MyDatabase;
    switched to db MyDatabase
    > db.createUser(
        {
            user: "my_user",
            pwd: "cu-npp-re-jpor-mtw-e-s",
            roles: [
                {role: "readWrite", db: "MyDatabase"},
                {role: "dbAdmin", db: "MyDatabase"}
            ]
        }
        )
    Successfully added user: {
        "user" : "vantage",
        "roles" : [
            {
                "role" : "readWrite",
                "db" : "AdVantage"
            },
            {
                "role" : "dbAdmin",
                "db" : "AdVantage"
            }
        ]
    }
    

So that was also apparently a success!

  1. I then exited the mongo shell and then logged in to the mongo shell again as my_user with credentials from (4). I confirmed that I was able to create a collection called demotest in MyDatabase:

    > use MyDatabase;
    switched to db MyDatabse
    > db.createCollection("demotest")
    { "ok" : 1 }
    > show collections;
    demotest
    > show dbs;
    MyDatabase 0.000GB
    admin      0.000GB
    local      0.000GB
    > exit
    bye
    

So far so good.

  1. I then switched to a terminal on my local machine. My goal is to be able to connect to this MongoDB instance from my local machine and from an app that is running somewhere else on the internet. So I issued the following command on my local machine:

    PROMPT> mongo --host 102.216.73.164 -u my_user -p cu-npp-re-jpor-mtw-e-s --authenticationDatabase MyDatabase --port 25019
    MongoDB shell version: 2.6.6
    connecting to: 102.216.73.164:25019/test
    2015-05-13T21:03:18.963-0400 Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:1210
    exception: login failed
    

Here is what I don't understand:

  1. why did it try to connect to the test database?

  2. why doesn't it work?

Best Answer

I've had the same authentication issue on an MMS deployed db (though it has nothing to do with it).

  1. it tries to connect to /test by default. If you want another database, just specify it, e.g. 102.216.73.164/foo.

  2. upgrade your mongo shell to 3.0.3 (you currently use 2.6.6). That solved the problem for me.