MongoDB 3.2 authentication failed

Well, you’ll need to take couple of steps in sequence to create user successfully.

First of all, you need to create an administrator user. I prefer creating super user.

> use admin
> db.createUser({user: "root", pwd: "123456", roles:["root"]})

Restart your MongoDB server and enable authentication with --auth flag.

> mongod --auth --port 27017 --dbpath /var/lib/mongodb

Once your server is up, connect to it as administrator

> mongo <host:port> -u "root" -p "123456" --authenticationDatabase "admin"

Once you are connected, create normal user. Assuming your user database name is cd2.

> use cd2
> db.createUser({user: "cd2", pwd: "cd2", roles:["dbOwner"]})

If you see success messsage, disconnect from mongo shell and reconnect with new user
credentials.

> mongo <host:port>/cd2 -u "cd2" -p "cd2"

Leave a Comment