Rails Mongoid 无法验证 - 失败,错误 13:"not authorized for query on my_db.my_collection"

Rails Mongoid fails to authenticate - failed with error 13: "not authorized for query on my_db.my_collection"

This issue 据说在最新版本的 Moped 上已解决,但仍然发生在我身上。 我有一个带有 Mongoid 的 rails 4.2 应用程序,为带有 readWritedbOwner[= 的 MongoDB 数据库创建了一个用户31=]角色,并在mong.conf文件中设置auth=true
我可以使用 Mongo shell 或使用其 Mongo 驱动程序的简单 Java 应用程序使用该用户凭据在数据库上执行任何操作。
但是,当尝试使用 Mongoid 进行身份验证时,我 总是 收到此错误:

failed with error 13: "not authorized for query on my_db.my_collection"

这是我的 mongoid.yml 文件的相关部分:

production:
  # Configure available database sessions. (required)
  sessions:
    # Defines the default session. (required)
    default:
      # Defines the name of the default database that Mongoid can connect to.
      # (required).
      database: my_db
      hosts:
              - localhost:27017
      username: my_username
      password: my_password

我也试过用服务器的远程地址替换主机并远程访问它(在禁用身份验证选项的情况下工作)但没有成功。 对于它的价值,我可以通过调试 mpped/node.rb 文件来查看凭据,在 ensure_connected 方法中我看到 @ credentials 变量包含我的用户名和密码 我在这里错过了什么? 谢谢!

运行也成这个问题了。我愿意猜你是 运行 MongoDB 3+?

坏消息: Moped 2(今天被 mongoid 使用)不支持 MongoDB 3+ 的身份验证。

Moped also falls short on a lot of essential functionality for supporting server version 3.0. For example, it simply doesn’t support authentication with MongoDB 3.0 or recent versions of the wire protocol (which includes commands such as listCollections, listIndexes, and the ability to do bulk writes). Integrating Mongoid with Ruby driver 2.0 will bring a lot of this functionality and forward-compatibility to the ODM. Source

好消息: 目前正在进行重大重写 (Mongoid 5),MongoDB 团队将提供支持。你今天可以从 master 中提取并测试它......但我不建议用于生产,因为它非常不稳定。

解决方案选项

  1. 从 MongoDB 和数据库的任何 public DNS 中删除 user/pass 身份验证。使用带有私钥的 SSH 并连接到私有地址。
  2. 降级到 MongoDB 2.x
  3. 使用 Mongoid 5