简体   繁体   English

在 MongoDB 中启用访问控制不起作用

[英]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 .当我用作连接字符串时,一切正常: 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.使用连接字符串添加启用访问控制后: mongodb://user:pwd@0.0.0.0:27017/?authSource=data停止工作。

I'm connected to mongodb but in application I see on request This request has no response data available .我已连接到 mongodb,但在应用程序中我看到请求This request has no response data available But for connection string to mongodb://0.0.0.0:27017/data there was data.但是对于到mongodb://0.0.0.0:27017/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:我更改了 mongo 配置文件 - 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.我使用: mongodb://user:pwd@0.0.0.0:27017/?authSource=data作为连接字符串,但没有工作。 What am I doing wrong?我究竟做错了什么?

I solved my problem.我解决了我的问题。 The reason was authSource in connection string.原因是连接字符串中的authSource 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.现在它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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