First I logged in as administrator to my mongodb:
sudo mongo admin --username root --password XXXXXXXX
then I created another user with root role:
> use admin
> db.createUser(
{
user: "myuser",
pwd: "myPassw",
roles: [ "root" ]
}
)
then I wrote:
> db.getUsers()
and my user was there:
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "clusterAdmin",
"db" : "admin"
}
]
},
{
"_id" : "admin.myuser",
"user" : "myuser",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
Now when I try to log in:
sudo mongo admin --username myuser --password myPassw
MongoDB shell version: 3.2.1
connecting to: /opt/bitnami/mongodb/tmp/mongodb-27017.sock:27017/admin
2016-02-10T21:23:53.964+0000 E QUERY [thread1] Error: Authentication failed. :
DB.prototype._authOrThrow@src/mongo/shell/db.js:1395:16
@(auth):6:1
@(auth):1:2
exception: login failed
What is the reason of that? I restarted the mongodb process but it didn't help.
=======EDIT:
after following Robbie advice I ran the command without sudo
and unfortunately this is the result:
bitnami@ip-172-31-47-102:~/stack$ mongo admin -u myuser -p myPassw
MongoDB shell version: 3.2.1
connecting to: /opt/bitnami/mongodb/tmp/mongodb-27017.sock:27017/admin
2016-02-10T21:56:15.615+0000 W NETWORK [thread1] Failed to connect to /opt/bitnami/mongodb/tmp/mongodb-27017.sock:0, reason: errno:13 Permission denied
2016-02-10T21:56:15.615+0000 E QUERY [thread1] Error: couldn't connect to server /opt/bitnami/mongodb/tmp/mongodb-27017.sock:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:226:14
@(connect):1:6
exception: connect failed
I found the solution ,
Symbol in password was the issue as i had $ sign , changing password with no symbol worked for me !!