MongoDB 3.2 – Authentication failed while connecting to a mongod/mongo instance

MongoDB 3.2 – Authentication failed while connecting to a mongod/mongo instance

I needed to connect to a mongod process that started with –auth parameter, AKA Client Access Control.

root@testbm:~/data# mongo localhost:27024 admin -u user -p password
MongoDB shell version: 3.2.6
connecting to: localhost:27024/test
2016-06-02T15:07:55.521+0100 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1441:20
@(auth):6:1
@(auth):1:2

exception: login failed

Okay…Let’s try using db.auth method to authenticate!

root@testbm:~/data# mongo localhost:27024
MongoDB shell version: 3.2.6
connecting to: localhost:27024/test
> use admin
switched to db admin
> db.auth('user','password');
1
configsvr> exit

That’s weird, what is going on? A search in the MongoDB 3.2 manual showed me the right path….

root@testbm:~/data# mongo localhost:27024 -u 'user' -p 'password' --authenticationDatabase admin
MongoDB shell version: 3.2.6
connecting to: localhost:27024/test
Server has startup warnings:
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten]
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten]
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten]
2016-06-02T14:03:16.390+0100 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-06-02T14:03:16.391+0100 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2016-06-02T14:03:16.391+0100 I CONTROL [initandlisten]

Apparently, when we have CAC enabled, we need to pass 2 additional parameters:

  1. authenticationDatabase
  2. The authentication database, which is admin

 

Reference:

 

Regards,
Wei Shan

Advertisements

One thought on “MongoDB 3.2 – Authentication failed while connecting to a mongod/mongo instance”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s