Mongocxx无法使用SSL连接到mongoDB

Mongocxx fails to connect to mongoDB with SSL

本文关键字:连接 mongoDB SSL Mongocxx      更新时间:2023-10-16

我完成了以下教程(https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89(来设置自签名SSL证书,以保护设备和托管mongoDB数据库的服务器之间的通信。

我可以使用以下命令从服务器和设备访问数据库:

mongo --ssl --sslCAFile /path/to/CA.pem --sslPEMKeyFile /path/to/mongodb.pem --host IP:port

错误

当我尝试使用C++程序连接到数据库时,出现 seg 错误:

Segmentation fault (core dumped)

GDB 的输出为

Program received signal SIGSEGV, Segmentation fault. 0x0000007fb7f6d6a8 in mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&) () from /usr/local/lib/libmongocxx.so._noabi

C++代码

我实例化了与以下方面的联系:

mongocxx::instance instance{};
mongocxx::options::ssl ssl_opts;
ssl_opts.pem_file("/path/to/mongodb.pem");
// ssl_opts.allow_invalid_certificates(false); // I have tried this
mongocxx::options::client client_opts;
client_opts.ssl_opts(ssl_opts);
auto client = mongocxx::client{mongocxx::uri{"mongodb://user:pwd@IP:port/?authMechanism=MONGODB-X509&ssl=true"}, client_opts};

并使用以下命令进行编译:

c++ --std=c++11 main.cpp $(pkg-config --cflags --libs libmongocxx) -Wl,-rpath,/usr/local/lib

我无法修复此错误或在线找到解决方案,任何帮助将不胜感激。

附加信息

版本:

蒙戈 c - 1.10.1

蒙戈 cxx - 3.3.0

回溯:

(gdb) thread apply all bt
Thread 1 (Thread 0x7fb7ff4000 (LWP 17800)):
#0  0x0000007fb7f6d6a8 in 
mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, 
mongocxx::v_noabi::options::client const&) () from 
/usr/local/lib/libmongocxx.so._noabi
#1  0x00000000004027d0 in main ()

在C++驱动程序版本 3.3.0 中将options::ssl_opts传递给客户端构造函数会导致段错误,这是一个已知的严重错误。此问题已在 3.3.1 中修复。强烈建议您升级。

作为 3.3.0 的解决方法,您可以通过 URI 字符串传递 pem_file 选项。URI 选项"sslclientcertificatekeyfile"对应于options::ssl::pem_file选项。例如:

auto uri = mongocxx::uri{"mongodb://localhost/?ssl=true&sslclientcertificatekeyfile=/path/to/mongodb.pem"};

但如果可能,请升级到 3.3.1。