【问题标题】:Cannot connect with authentication to mongodb?无法通过身份验证连接到 mongodb?
【发布时间】:2017-01-09 02:27:32
【问题描述】:

这里还有另一个答案:Can't connect to MongoDB with authentication enabled。我试过了,但仍然无法弄清楚我的配置有什么问题。

我使用作为服务安装的 Ubuntu 14.04、Mongo 3.4.1(最新)

安装后我首先运行这个命令,就像它的文档here

mongo --port 27017
use admin
db.createUser({user: "adminUser",pwd: "abc123",roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]})

它返回Successfully added user。然后我重新配置/etc/mongod.conf

storage:
   dbPath: /var/lib/mongodb
   journal:
     enabled: true

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 27017
  bindIp: 127.0.0.1

security:
  authorization: enabled

保存并重启 mongod 服务器:sudo service mongod restart

尝试联系:mongo -u "adminUser" -p "abc123" --authenticationDatabase "admin"

成功了,那么如果我用命令use testDatabase切换到另一个数据库,我就不能对其进行任何操作了。

use testDatabase
db.createCollection("people")

结果:

{
    "ok" : 0,
    "errmsg" : "not authorized on testDatabase to execute command { create: \"people\" }",
    "code" : 13,
    "codeName" : "Unauthorized"
}

这是我数据库中的注册用户

use admin
db.system.users.find()
{ "_id" : "admin.adminUser", 
  "user" : "adminUser", 
  "db" : "admin", 
  "credentials" : { "SCRAM-SHA-1" : {....} }, 
  "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ]
}

userAdminAnyDatabase 角色似乎不再起作用或者我的设置有什么问题?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    内置角色UserAdmin & UserAdminAnyDatabase 角色允许您在数据库中创建用户和角色。

    对于数据库的读/读操作,您必须为该数据库创建具有read/readWrite 角色的用户。

    其他选项是将角色添加到您拥有的当前用户。

    例如这样的事情。

    use test
    db.createUser(
      {
        user: "myTester",
        pwd: "xyz123",
        roles: [ { role: "readWrite", db: "test" },
                 { role: "read", db: "reporting" } ]
      }
    )
    

    【讨论】:

    猜你喜欢
    • 2016-03-20
    • 1970-01-01
    • 2019-06-22
    • 2018-01-25
    • 2015-06-02
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多