AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 185518
Accepted
Joey Yi Zhao
Joey Yi Zhao
Asked: 2017-09-10 17:34:56 +0800 CST2017-09-10 17:34:56 +0800 CST 2017-09-10 17:34:56 +0800 CST

如何设置没有 `admin` 数据库访问权限但将其用作身份验证数据库的用户?

  • 772

我想在 MongoDB 中设置一个用户。此用户将没有管理员数据库访问权限。但它使用 admin 作为身份验证数据库。此命令将无法连接 mongoDB mongo --host localhost admin。相反,它可以使用此命令连接到test数据库:mongo --host localhost --authenticationDatabases admin test. 在这种情况下如何限制权限?

我尝试了以下命令来创建用户:

db.createUser({user: 'testUser', pwd: '123456', roles: [{role:'readWrite', db: 'SampleCollections'}]})

当我使用该用户帐户登录 mongo shell 时,我可以列出admin数据库下的集合。我怎样才能将用户限制在SampleCollections数据库上而不是admin?

mongodb mongodb-3.0
  • 2 2 个回答
  • 14493 Views

2 个回答

  • Voted
  1. Best Answer
    JJussi
    2017-09-11T03:23:01+08:002017-09-11T03:23:01+08:00

    不需要。您只需向用户授予所需的非管理员数据库所需的权限。即使用户没有对管理数据库的读/写访问权限,用户也可以使用管理数据库作为身份验证数据库。

    use products
    db.grantRolesToUser("productsUser",[ "readWrite" ])
    

    grantRolesToUser文档。

    更新

    让我们使用 --auth 创建 mongodb 实例。以管理员身份登录,创建只能读写 test -db 的用户,使用该用户进行身份验证,检查可以使用列表管理员数据库集合以及该用户可以使用测试数据库做什么。

    #> mlaunch init --single --auth
    launching: mongod on port 27017
    Username "user", password "password"
    #> mongo -u user -p password admin
    MongoDB shell version v3.4.6
    connecting to: mongodb://127.0.0.1:27017/admin
    MongoDB server version: 3.4.6
    Mongo> db.createUser({user:"test", pwd:"testpwd", roles:[{role:"readWrite", db:"test"}]})
    Successfully added user: {
        "user" : "test",
        "roles" : [
            {
                "role" : "readWrite",
                "db" : "test"
            }
        ]
    }
    Mongo> db.auth("test","testpwd")
    1
    Mongo> db
    admin
    Mongo> show collections
    2017-09-11T19:06:22.001+0300 E QUERY    [thread1] Error: listCollections failed: {
        "ok" : 0,
        "errmsg" : "not authorized on admin to execute command { listCollections: 1.0, filter: {} }",
        "code" : 13,
        "codeName" : "Unauthorized"
    } :
    _getErrorWithCode@src/mongo/shell/utils.js:25:13
    DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:807:1
    DB.prototype.getCollectionInfos@src/mongo/shell/db.js:819:19
    DB.prototype.getCollectionNames@src/mongo/shell/db.js:830:16
    shellHelper.show@src/mongo/shell/utils.js:762:9
    shellHelper@src/mongo/shell/utils.js:659:15
    @(shellhelp2):1:1
    Mongo> use test
    switched to db test
    Mongo> db.coll.insert({})
    WriteResult({ "nInserted" : 1 })
    Mongo> show collections
    coll
    Mongo>  
    

    和外面一样:

    #> mongo -u test -p testpwd --authenticationDatabase test
    MongoDB shell version v3.4.6
    connecting to: mongodb://127.0.0.1:27017
    MongoDB server version: 3.4.6
    2017-09-11T19:17:48.614+0300 E QUERY    [thread1] Error: Authentication failed. :
    DB.prototype._authOrThrow@src/mongo/shell/db.js:1461:20
    @(auth):6:1
    @(auth):1:2
    exception: login failed
    #> mongo -u test -p testpwd --authenticationDatabase admin test
    MongoDB shell version v3.4.6
    connecting to: mongodb://127.0.0.1:27017/test
    MongoDB server version: 3.4.6
    Mongo> 
    
    • 2
  2. Md Haidar Ali Khan
    2017-09-11T05:06:01+08:002017-09-11T05:06:01+08:00

    @赵毅,你在看什么?我希望你能在这里找到你的解决方案

    例如,我附上一个示例,如下所示

    在此处输入图像描述

    代码的解释是:

    1. 第一步是指定需要创建的username和。password
    2. 第二步是为用户分配一个角色,在这种情况下,因为它需要是数据库管理员,所以分配给该 userAdmin角色。此角色允许用户仅对 db 选项中指定的数据库拥有管理权限。
    3. db 参数指定用户应该对其具有管理权限的数据库。

    注意:输出显示创建了一个名为Employeeadmin的用户,并且该用户仅对Employee数据库具有权限。

    • 1

相关问题

  • Mongo Map-Reduce 还是分片?

  • 使用集群设置 Mongo

  • MongoDB 的 find 和 findone 调用之间的区别

  • 分片对小集合有效吗?

  • 关于操作/管理 MongoDB 的良好资源

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve