MongoDB + Rails 错误:命令查找需要身份验证

Posted

技术标签:

【中文标题】MongoDB + Rails 错误:命令查找需要身份验证【英文标题】:MongoDB + Rails Error: Command find requires authentication 【发布时间】:2020-11-26 04:49:59 【问题描述】:

我无法在通过 Rails 6 应用启用访问控制的 MongoDB 上运行命令。

这是我得到的错误:

Mongo::Error::OperationFailure (command find requires authentication (13) (on localhost:27017, modern retry, attempt 1))

这是我的mongoid.yml

staging:
  clients:
    default:
      database: mydb_staging
      hosts:
        - localhost:27017
      user: 'julien'
      password: 'mypass'
      auth_source: admin
      auth_mech: :scram
      options:

我可以从命令行在 mongo 中连接和验证,我可以连接到它并通过 Robomongo 远程验证。

这是我创建的用户:

> use admin
switched to db admin
> db.auth('julien', 'mypass')
1
> db.getUsers()
[
    
        "_id" : "admin.julien",
        "userId" : UUID("3444564f-7dcd-4283-8fb1-ce4f122ed9b8"),
        "user" : "julien",
        "db" : "admin",
        "roles" : [
            
                "role" : "userAdminAnyDatabase",
                "db" : "admin"
            ,
            
                "role" : "root",
                "db" : "admin"
            ,
            
                "role" : "readWriteAnyDatabase",
                "db" : "admin"
            
        ],
        "mechanisms" : [
            "SCRAM-SHA-1",
            "SCRAM-SHA-256"
        ]
    
]

我什至在 Rails 应用程序的特定数据库中创建了一个具有相同凭据的用户:

> use mydb_staging
switched to db mydb_staging
> db.getUsers()
[
    
        "_id" : "mydb_staging.julien",
        "userId" : UUID("dc2d8ab6-4f01-4568-be9d-6486283aea14"),
        "user" : "julien",
        "db" : "mydb_staging",
        "roles" : [
            
                "role" : "readWrite",
                "db" : "mydb_staging"
            
        ],
        "mechanisms" : [
            "SCRAM-SHA-1",
            "SCRAM-SHA-256"
        ]
    
]

更多细节:

$ mongo --version
MongoDB shell version v4.2.8
git version: 43d25964249164d76d5e04dd6cf38f6111e21f5f
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

宝石版本:

导轨 (6.0.3.2) mongo (2.12.1) mongoid (7.1.2)

为什么我不能通过 Rails 运行命令,我该如何解决这个问题?

编辑

以下是 rails 控制台的示例:

$ RAILS_ENV=staging bundle exec rails console
Loading staging environment (Rails 6.0.3.2)
irb(main):001:0> User.count
Traceback (most recent call last):
        1: from (irb):1
Mongo::Error::OperationFailure (command count requires authentication (13) (on localhost:27017, modern retry, attempt 1))

【问题讨论】:

我们能看到产生错误的代码吗? @Joe 任何使用数据库的代码都会导致错误,即使是简单的User.count。所有代码在我的本地环境中运行良好,其中数据库的访问控制被禁用。 您可以尝试将 'auth_source' 移动到选项下吗? holy molly,你是对的,该配置文件中有两个 options 密钥,而我的所有身份验证都在错误的那个中! 【参考方案1】:

Nek 的评论为我指明了正确的方向,如果您查看 docs 的 mongoid.yml 文件,您会注意到有 两个 options 部分,我不得不将身份验证部分移到其中一个中,正确的配置如下所示:

staging:
  clients:
    default:
      database: mydb_staging
      hosts:
        - localhost:27017
      options:
        user: 'julien'
        password: 'mypass'
        auth_source: admin
        auth_mech: :scram

【讨论】:

以上是关于MongoDB + Rails 错误:命令查找需要身份验证的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu20.04 通过 apt 方式安装 mongoDB

使用 pymongo 在 mongoDB 中查找

Ruby on Rails 是 mongodb - mongoid

MongoDb:管道内部查找错误:错误:参数必须是聚合管道运算符

如何使用 Rails 在 MongoDB 中验证跨模型的唯一性?

MongoDB索引