使用 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 fails to connect to mongoDB with SSL .
我总是有以下错误:
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 驱动程序 header 中查找 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
当然,您应该将上面的 include 目录的路径替换为您安装 C 驱动程序的实际位置。
如果您在其中看到 #define MONGOC_ENABLE_SSL 1
以外的任何内容,那么您的 C 驱动程序没有启用 SSL 支持,您需要重建它才能拥有它。
关于c++ - 将 mongocxx 连接到 mongodb 服务器 : SSL support not available 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56977425/