1

Everything worked fine when I used as a connection string: mongodb://0.0.0.0:27017/data. After add Enable Access Control with connection string: mongodb://user:pwd@0.0.0.0:27017/?authSource=data stops working.

I'm connected to mongodb but in application I see on request This request has no response data available. But for connection string to mongodb://0.0.0.0:27017/data there was data.

To add authentication I used this instruction Enable Access Control:

I created admin:

> use admin
switched to db admin
> db.createUser(
...   {
...     user: 'admin',
...     pwd: 'password',
...     roles: [ { role: 'root', db: 'admin' } ]
...   }
... );
Successfully added user: {
        "user" : "admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}
> exit;

I changed mongo config file - mongod.cfg:

security:
    authorization: enabled

I logged in as admin:

> use admin
> db.auth('admin','password');
1

I created new user:

use data
db.createUser(
  {
    user: "user",
    pwd: "pwd",
    roles: [
       { role: "dbAdmin", db: "data" },
    ]
  }
)
db.grantRolesToUser(
   "user",
   [ "readWrite" , { role: "read", db: "data" } ],
   { w: "majority" , wtimeout: 4000 }
)

And I used: mongodb://user:pwd@0.0.0.0:27017/?authSource=data as a connection string, but there is not working. What am I doing wrong?

Weronika
  • 368
  • 3
  • 24

1 Answers1

0

I solved my problem. The reason was authSource in connection string. Before:

 "connectionString": "mongodb://user:pwd@0.0.0.0:27017/?authSource=data"

After:

 "connectionString": "mongodb://user:pwd@0.0.0.0:27017/data"

Now it's working fine.

Weronika
  • 368
  • 3
  • 24
  • No, authSource is fine, what it probably means is that you created the user under `admin` – Minsky Jul 01 '21 at 08:52
  • But now it's working fine. I tried also created user with `role: "dbAdmin"` and finally it is not `root` but `dbAdmin` as user. – Weronika Jul 01 '21 at 08:52
  • It doesn't matter, you need to get what's wrong or will repeat the error. Try with this and you get what I say: `mongodb://user:pwd@0.0.0.0:27017/data?authSource=admin` The question is correct anyways (+1). – Minsky Jul 01 '21 at 08:54
  • After used your connection string I get error [Function: Error] { stackTraceLimit: 16, prepareStackTrace: undefined } Invalid connection string – Weronika Jul 01 '21 at 08:58
  • @Minsky I use it on another pc and I do not create admin there, just user `role: "dbAdmin"` using this connection string `user:pwd@0.0.0.0:27017/data` and works fine. So maybe creating an admin is unnecessary to this? – Weronika Jul 01 '21 at 09:02
  • 1
    Maybe check this one: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 And host should be rather `localhost` or `127.0.0.1` or the real hostname rather than `0.0.0.0` – Wernfried Domscheit Jul 01 '21 at 12:05