简体   繁体   中英

Enable Access Control in MongoDB doesn't work

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 instructionEnable 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM