How to create a user in MongoDB v3.0.5

I need to create a user for my database in mongodb, but it seems that I can't get it to work.

I have installed mongoDb v3.0.5 on my windows 7 machine. according to this article, I connected to my mongo instance using:

mongo -u siteUserAdmin -p password

and then I created first user via:

use admin
db.createUser(
  {
    user: "siteUserAdmin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

then I created a user for my nodejs application on exampleDb:

use admin
db.createUser(
  {
    user: "myUser",
    pwd: "123456",
    roles: [ { role: "readWrite", db: "exampleDb" } ]
  }
)

and finally in my nodejs application, when I try to connect to my mongo instance using this connection string:

var connString = "mongodb://myUser:123456@127.0.0.1:27017/exampleDb";

an Authentication failed error happens:

{ name: 'MongoError',
  message: 'Authentication failed.',
  ok: 0,
  code: 18,
  errmsg: 'Authentication failed.'
}

Leave a Reply

Your email address will not be published. Required fields are marked *