I create a user with the following set of commands. This should create user in both admin
db as well as my target db (c2d):
# mongo 127
Our experience with this issue on MongoDB (3.2.4) is that it seems a bug or a undocumented feature. If you attempt to create the users and roles from a remote shell (not localhost), the users are created but as soon as you leave the shell they are gone (they are transient to that session) and not really persisted.
Solution: Just try to create your users (without authentication enabled initially on MongoDB) and do it directly on the console of your DB server (localhost).
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"
try --authenticationDatabase=admin
option in your terminal mongo command, if admin is the authentication database.
For example:
insteed of: mongo 127.0.0.1:27017/c2d -u c2d -p c2d
use: mongo 127.0.0.1:27017/c2d -u c2d -p c2d --authenticationDatabase=admin
If you log in through shell, make sure your create user under db "admin", NOT under a customized db. In your case you switched to "c2d".
Here is what I have tried (Log in as "admin")
$ mongo -u admin -p --authenticationDatabase "admin"
> use admin
> db.createUser(
{
user: "user007",
pwd: "YourP@ssw0rd",
roles: [
{ role: "readWrite", db: "yourdb" },
]
}
)
root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
MongoDB server version: 4.0.6
----
> use admin
switched to db admin
> db.createUser(
... {
... user: "user007",
... pwd: "YourP@ssw0rd",
... roles: [
... { role: "readWrite", db: "yourdb" },
... ]
... }
... )
Successfully added user: {
"user" : "user007",
"roles" : [
{
"role" : "readWrite",
"db" : "yourdb"
}
]
}
root@mongo-server:/# mongo -u user007 -p YourP@ssw0rd
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ceabf440-c584-4518-90f5-cc845eaec3b3") }
MongoDB server version: 4.0.6
---
>
$ mongo -u admin -p --authenticationDatabase "admin"
> use yourdb
> db.createUser(
{
user: "user007",
pwd: "YourP@ssw0rd",
roles: [
{ role: "readWrite", db: "yourdb" },
]
}
)
root@mongo-server:/# mongo -u admin -p --authenticationDatabase "admin"
MongoDB shell version v4.0.6
Enter password:
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
MongoDB server version: 4.0.6
----
> use yourdb
switched to db yourdb
> db.createUser(
... {
... user: "user007",
... pwd: "YourP@ssw0rd",
... roles: [
... { role: "readWrite", db: "yourdb" },
... ]
... }
... )
Successfully added user: {
"user" : "user007",
"roles" : [
{
"role" : "readWrite",
"db" : "yourdb"
}
]
}
>
root@mongo-server:/# mongo -u user007 -p YourP@ssw0rd
MongoDB shell version v4.0.6
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2019-12-06T04:28:34.630+0800 E QUERY [js] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:343:13
@(connect):1:6
exception: connect failed