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 是否有东西。确定没有则需要安装一下了。
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
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://stackoverflow.com/questions/6562403/i-dont-understand-wl-rpath-wl
总结
本文介绍了mongocxx的安装过程,主要记录其中可能遇到的问题和解决过程。
mongodb C++ Driver安装的更多相关文章
- mongodb c++ driver安装踩坑记
安装教程:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/ (1) “initializer_list” fil ...
- windows平台下安装、编译、使用mongodb C++ driver
本博客将记录在Win8.1 ,VS2013环境下编译.配置mongodb C++ driver的流程. 1.下载预备 下载Boost:http://sourceforge.net/projects/b ...
- MongoDB C Driver and APIinstances linux MongoDB安装配置
<一,linux平台MongoDB安装配置>在这我们使用的Centos6 yum部署的,你想搞编译,自个干!
- 编译安装MongoDB C++ Driver (win8.1 vs2013)
在C++中调用mongodb的库函数需要安装mongodb的c++driver,需要自己编译,(自己搞了一天半 =_=''' ) 官网Build MongoDB From Source 说To bui ...
- RedHat6/Centos6.5安装mongodb php driver
条件: 安装apache 安装php 1.下载mongodb phh driver 下载地址:pecl wget http://pecl.php.net/get/mongo-1.5.8.tgz 2.解 ...
- MongoDB Java Driver操作指南
MongoDB为Java提供了非常丰富的API操作,相比关系型数据库,这种NoSQL本身的数据也有点面向对象的意思,所以对于Java来说,Mongo的数据结构更加友好. MongoDB在今年做了一次重 ...
- MongoDB C Driver使用教程
MongoDB C Driver使用教程 转载请注明出处http://www.cnblogs.com/oloroso/ 本指南提供简介 MongoDB C 驱动程序. 在 C API 的详细信息,请参 ...
- mongodb c++ driver(2.53)windows编译
编译环境: (1) 下载python2.7, 使用x86_32位,因为scons只有32位安装包可用: (2) 下载scons2.3.0,The current production release ...
- mongodb c++ driver 2.0编译使用
安装boost1.48.0 在boost的官网下载boost1.48.0,链接例如以下: http://sourceforge.net/projects/boost/files/boost/1.48. ...
随机推荐
- idea整个项目乱码解决办法
idea前几天还好好的,这两天一直出现乱码.不止一个文件,是整个项目都有这个问题.想想这几天什么也没做啊... 上网搜了下 解决. 经过多次排查寻找,终于,在项目的目录下有个.idea的文件夹,这个文 ...
- git 命令备忘
git 常用命令备忘 仅作为日常使用备忘,并非常用命名整理 删除源端分支 git push orgin --delete branch_a a分支某次提交应用到b分支 切换到branch_b 分支: ...
- 使用原生的javascript来实现轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- spring----面试题
1.什么是Spring beans? Spring beans 是那些形成Spring应用的主干的java对象.它们被Spring IOC容器初始化,装配,和管理.这些beans通过容器中配置的元数据 ...
- ubuntu16.04下无法连接网络的bug
首先介绍下Bug的情况,这个bug纠缠我整整一天,在命令行下ifconfig能够看到ip地址,不过我的不是eth0,而是enps03,然后Ping 本机和ping 网关都能够 ping 通,但是sud ...
- Eclipse 无法编译,提示“错误: 找不到或无法加载主类”
jar包问题: 1.项目的Java Build Path中的Libraries中有个jar包的Source attachment指为了一个不可用的jar包, 解决办法是:将这个不可用的jar包remo ...
- pc端配置详细 2017级机械设计新生 史浩然
品牌名称:SAMSUNG/三星 证书状态:有效 申请人名称:苏州三星电子电脑有限公司 型号:940X3K-K01 操作系统:window8.1 产品名 ...
- 删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误,即DeletedRowInaccessibleException
删除datatable的行后,出现“不能通过已删除的行访问该行的信息”的错误 =========================================================== 采 ...
- 转 C++11 并发指南std::condition_variable详解
之前看过,但是一直没有怎么用就忘了,转一篇别人的文字记录下来 本文将介绍 C++11 标准中 <condition_variable> 头文件里面的类和相关函数. <conditio ...
- bootstrap Switch 的一个坑点
在bootstrap的modal点开的时候改变bootstrapSwitch的状态的时候,会出现第一次打开modal,switch没有变化,第二次以后打开modal才会改变,这个问题找了好久没有找到答 ...