Mongo 带有 CA 证书的 SSL 配置仅适用于 rails 控制台,不适用于 rails 服务器

Mongo SSL config with CA cert only works for rails console, not rails server

我正在尝试配置 Rails 4.2.6 应用程序以通过 SSL 连接到 compose.io 上的 MongoDB 集群。该应用程序使用 mongo 2.4.1 和 mongoid 5.1.5 gem。 Compose.io MongoDB 集群使用自签名 SSL 证书,所以我从 compose.io 下载了 CA 证书并将其放在 lib/certs/mongo-ca-bundle.pem.

我的 mongoid.yml 文件看起来像这样(已编辑):

development:
  clients:
    default:
      uri: mongodb://myuser:mypassword@some-host.dblayer.com:1111,some-other-host.dblayer.com:2222/mydb?ssl=true
      options:
        ssl_ca_cert: /path/to/myapp/lib/certs/mongo-ca-bundle.pem

当我 运行 rails console 并且我可以成功地 运行 查询 compose.io 数据库中的集合时,上述配置完美运行。

但是,当我尝试 运行 rails server 时,它会在 30 秒后崩溃并显示 "No server is available" 错误消息:

[..]/mongo/server_selector/selectable.rb:115:in `select_server': No server is available matching preference: #<Mongo::ServerSelector::Primary:0x47167439625640 tag_sets=[] max_staleness=nil> using server_selection_timeout=30 and local_threshold=0.015 (Mongo::Error::NoServerAvailable)
[..]/mongo/cluster.rb:226:in `next_primary'

当我为 mongoid 启用调试日志记录时,它表明问题出在 SSL 握手上:

DEBUG -- : MONGODB | Server some-host.dblayer.com:1111 initializing.
DEBUG -- : MONGODB | SSL handshake failed. MongoDB may not be configured with SSL support.

我曾经在 rails 控制台中遇到同样的错误,直到我添加了 ssl_ca_cert 配置。我确实通过指定 ssl_verify: false 设法让服务器连接,但我认为这不是有效的解决方法。我怎样才能解决这个问题?怎么控制台能用,服务器不行?

看来我在 CA 捆绑包中缺少证书导致连接失败。在捆绑包中使用适当的 CA 证书,rails server 可以正常连接。

rails console 工作的原因似乎是它连接到本地 mongodb 实例,即使我在 mongoid.yml 中指定了不同的 URL。这对我来说似乎有问题,但我还没有进一步调查。