将mongocxx连接到mongodb服务器时出错:SSL支持不可用

Error connecting mongocxx to mongodb server: SSL support not available

本文关键字:SSL 支持 出错 连接 mongocxx mongodb 服务器      更新时间:2023-10-16

使用 mongocxx 3.3 或 mongo cxx 3.4 稳定版本,我正在尝试连接到 mongo atlas 实例。这是我的基本代码:

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
mongocxx::instance inst{};    
mongocxx::uri uri = mongocxx::uri("mongodb+srv://...");
mongocxx::client conn(uri);
mongocxx::database db = conn["test"];

我已经测试了几种替代方案,例如使用客户端选项(如此处所述:http://mongocxx.org/mongocxx-v3/configuration/(,以及设置pem文件路径,如下所示:Mongocxx无法使用SSL连接到mongoDB。

我总是有以下错误:

terminate called after throwing an instance of 'mongocxx::v_noabi::exception'
what():  SSL support not available
Aborted (core dumped)

您收到此错误是因为一个或两个 C 和 C++ 驱动程序是在没有 SSL 支持的情况下配置的。C++驱动程序版本默认为 SSL 支持(查找 MONGOCXX_ENABLE_SSL CMake 选项(。因此,最可能的解释是底层 C 驱动程序是在没有 SSL 的情况下构建的,第二个最可能的解释是 C 驱动程序确实内置了 SSL 支持,但在构建 C++ 驱动程序时显式设置为关闭。可以通过在 C 驱动程序标头中查找 MONGOC_ENABLE_SSL 的值来验证 C 驱动程序的状态。如果已启用,它应如下所示:

$ find /usr/local/Cellar/mongo-c-driver/1.14.0/include -type f -name "*.h" | xargs grep 'MONGOC_ENABLE_SSL '
/usr/local/Cellar/mongo-c-driver/1.14.0/include/libmongoc-1.0/mongoc/mongoc-config.h: * MONGOC_ENABLE_SSL is set from configure to determine if we are
/usr/local/Cellar/mongo-c-driver/1.14.0/include/libmongoc-1.0/mongoc/mongoc-config.h:#define MONGOC_ENABLE_SSL 1
/usr/local/Cellar/mongo-c-driver/1.14.0/include/libmongoc-1.0/mongoc/mongoc-config.h:#if MONGOC_ENABLE_SSL != 1

当然,您应该将上面包含目录的路径替换为安装 C 驱动程序的实际位置。

如果您看到除#define MONGOC_ENABLE_SSL 1以外的任何内容,则您的 C 驱动程序未启用 SSL 支持,您需要重新生成它才能获得它。