mongodb C++ Driver安装

前言

mongocxx官网地址 http://mongocxx.org/?jmp=docs

本文的安装版本是:mongocxx-r3.2.0.tar.gz

参考文档安装过程http://mongocxx.org/mongocxx-v3/installation/。根据文档过程安装,由于系统不一致,可能会遇到各种问题,笔者就一路坎坷。

Linux系统信息:Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt25-2 (2016-04-08) x86_64 GNU/Linux

1 开始安装

1.1 安装最新版的mongoDB C driver

mongocxx是基于mongo-c-driver的,所以需要先下载安装最新版本的 mongo-c-driver。此时最新版本是 1.13.0.tar.gz

参考链接:http://mongoc.org/libmongoc/current/installing.html。本文所有内容都是采用源码安装的方式。

根据文档要求,cmake需要是3.2或之后的版本。camke --version发现系统的cmake还停留在2.x版本。先升级一下cmake。

1.1.1 升级cmake到最近版本

wget https://cmake.org/files/v3.12/cmake-3.12.3.tar.gz
tar -xzf cmake-3.12.3.tar.gz
cd cmake-3.12.
./configure
sudo make && make install
cmake --version

其中,cmake --version检查一下最终的cmake版本。

mongo-c-driver的cmake过程

wget https://github.com/mongodb/mongo-c-driver/releases/download/1.13.0/mongo-c-driver-1.13.0.tar.gz
tar -xzf mongo-c-driver-1.13..tar.gz
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release ..

NOTE:这里要特别注意cmake的输出,会提示你系统还缺少什么库(NOT FOUND),请不要无视。笔者的mongo-c-driver就安装了两遍。

例如,openssl和snappy没有安装正确。虽然最终cmake和make不会报错,但是最后面用mongocxx的时候会提示ssl错误,不能连接mongodb。

附上cmake的部分输出:

-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
-- Searching for sasl/sasl.h
-- Found in /usr/include
-- Searching for libsasl2
-- Found /usr/lib/x86_64-linux-gnu/libsasl2.so
-- Detected parameters: accept (int, struct sockaddr *, socklen_t *)
-- Searching for compression library header snappy-c.h
-- Not found (specify -DCMAKE_INCLUDE_PATH=/path/to/snappy/include for Snappy compression)
-- No ICU library found, SASLPrep disabled for SCRAM-SHA- authentication.
-- If ICU is installed in a non-standard directory, define ICU_ROOT as the ICU installation path.
-- SSL disabled
-- Compiling against Cyrus SASL
-- Configuring done
-- Generating done
-- Build files have been written to: /home/yyln2745/studyplace/mongo-c-driver-1.13.0/cmake-build

如果SSL disabled的话,后面你写的代码应该会报错:The SCRAM_SHA_1 authentication mechanism requires libmongoc built with ENABLE_SSL: generic server error 。

1.1.2 openssl问题

先确定是否安装了openssl。直接在shell输入openssl命令,或者查看目录:/usr/local/ssl 是否有东西。确定没有则需要安装一下了。

按照cmake的提示,需要设置OPENSSL_ROOT_DIR、OPENSSL_CRYPTO_LIBRARY、OPENSSL_INCLUDE_DIR到系统路径才能找到,可能放在cmake文件也可以,但是对cmake不太熟。先按照上面的提示设置到system variable中。直接设置到profile文件中,编辑vim /etc/profile,添加以下三行。
export OPENSSL_ROOT_DIR=/usr/local/ssl
export OPENSSL_CRYPTO_LIBRARY=/usr/local/ssl/lib
export OPENSSL_INCLUDE_DIR=/usr/local/ssl/include/

1.1.4 安装snappy

snappy用来压缩的。虽然不知道不安装会有什么问题,但是不想像SSL一样,遇到问题了再回头来修,将上面cmake提到的not found的都解决。安装命令:

wget https://github.com/google/snappy/tarball/master
tar -xzf master
cd google-snappy-ea660b5/cmake
cmake ..
// 暂停一下
make
sudo make install

在make前,我们需要先编辑一下CMakeCache.txt,修改CMAKE_CXX_FLAGS:STRING=-fPIC为什么要加 -fPIC 呢?因为不这样的话,待会mongo-c-driver执行make时,应该会报以下错误:

/usr/bin/ld: /usr/local/lib/libsnappy.a(snappy.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/
libsnappy.a: error adding symbols: Bad value
collect2: error: ld returned exit status
src/libmongoc/CMakeFiles/mongoc_shared.dir/build.make:: recipe for target 'src/libmongoc/libmongoc-1.0.so.0.0.0' failed
make[]: *** [src/libmongoc/libmongoc-1.0.so.0.0.] Error
CMakeFiles/Makefile2:: recipe for target 'src/libmongoc/CMakeFiles/mongoc_shared.dir/all' failed
make[]: *** [src/libmongoc/CMakeFiles/mongoc_shared.dir/all] Error
Makefile:: recipe for target 'all' failed
make: *** [all] Error

关于更多 fPIC 的知识,可另行学习,建议了解一下。

1.1.5 安装ICU

官网:http://www.icu-project.org/download/4.2.html 安装步骤:

wget http://download.icu-project.org/files/icu4c/4.2.1/icu4c-4_2_1-src.tgz
tar -xzf icu4c-4_2_1-src.tgz
cd source
./configure
make
sudo make install

1.1.6 最后make

最后一步,接着make & make install mongo-c-driver。

make
sudo make install

如果还出现如下错误:

[66%]Linking C executable test-libmongoc
/usr/bin/ld: /usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned exit status
src/libmongoc/CMakeFiles/test-libmongoc.dir/build.make:: recipe for target 'src/libmongoc/test-libmongoc' failed
make[]: *** [src/libmongoc/test-libmongoc] Error
CMakeFiles/Makefile2:: recipe for target 'src/libmongoc/CMakeFiles/test-libmongoc.dir/all' failed
make[]: *** [src/libmongoc/CMakeFiles/test-libmongoc.dir/all] Error
Makefile:: recipe for target 'all' failed
make: *** [all] Error

这是mongo-c-driver测试样例编译错误,我们可以选择直接干掉,方法为编辑mongo-c-driver的CMakeCache.txt文件,修改ENABLE_TESTS:BOOL=OFF,或者在cmake的时候修改,命令如下,注意TEST后面有S:

cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release -DENABLE_TESTS=OFF ..

安装mongocxx

wget -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.3.1.tar.gz
tar -xzf r3.3.1.tar.gz
cd mongo-cxx-driver-r3.3.1/build
cmake -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_MNMLSTC= -DCMAKE_INSTALL_PREFIX=/usr/local ..
make EP_mnmlstc_core
make
sudo make install
其中,/usr/local是与前面的mongodb c driver同目录, DBSONCXX_POLY_USE_MNMLSTC=1 or DBSONCXX_POLY_USE_BOOST=

这里问题不大。

代码测试

测试代码如下:

#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp> #include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp> int main(int, char**)
{
mongocxx::instance inst{};
//下面uri 请替换成真实数值。
mongocxx::uri yace_uri{"mongodb://test_username:test_pwd@test_ip:test_port/?authSource=test_db"}; mongocxx::options::client client_options;
if (yace_uri.ssl()) {
std::cout << "ssl-----------\n";
}
else {
std::cout << "no-ssl\n";
} mongocxx::client conn{yace_uri}; mongocxx::database db = conn["test_db"];
mongocxx::collection gift_test = db["gift_test"]; bsoncxx::stdx::optional<bsoncxx::document::value> res = gift_test.find_one({});
if (res)
std::cout << bsoncxx::to_json(*res) << std::endl; return ;
}

编译命令:

c++ --std=c++ test.cpp \
-I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 \
-I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 \
-L/usr/local/lib -lmongocxx -lbsoncxx -Wl,-rpath,/usr/local/lib

添加“-Wl,-rpath,/usr/local/lib” 是因为运行时报错:error while loading shared libraries: libmongocxx.so._noabi。

参考链接:

https://jira.mongodb.org/browse/CXX-1425 for preior question.
http://blog.51cto.com/elephantliu/563298
https://blog.csdn.net/furzoom/article/details/70843664
https://*.com/questions/6562403/i-dont-understand-wl-rpath-wl

总结

本文介绍了mongocxx的安装过程,主要记录其中可能遇到的问题和解决过程。

上一篇:vue-cli: 渲染过程理解(vue create demo01方式创建)


下一篇:ES6中的迭代器(Iterator)和生成器(Generator)