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. ...
随机推荐
- 【转】Spark on Yarn遇到的几个问题
本文转自 http://www.cnblogs.com/Scott007/p/3889959.html 1 概述 Spark的on Yarn模式,其资源分配是交给Yarn的ResourceManage ...
- mysql 修改数据库密码
MYSQL5.7以下版本的数据库密码使用的是 mysql这个数据库里的user表的password这个字段, 修改密码只需: 1.update MySQL.user set password=pass ...
- STL算法分类记忆
STL算法主要是我们强大的标准库中以迭代器或数值或函数对象为参数预先定义好的一系列算法操作. 在STL算法分类中首先要提的就是两个普遍存在的后缀: _if _copy 其中这两个后缀的作用分别是:一. ...
- NSJSONSerialization能够处理的JSONData
NSJSONSerialization能够处理的JSONData You use the NSJSONSerialization class to convert JSON to Foundation ...
- windows生成硬链接
因工作电脑需要同时使用pl/sql和toad工具需要同时配置32位和64位oracle client如此增加了维护tnsnames.ora的复杂程度使用windows硬链接可以减少工作量,每次只修改源 ...
- CentOS 7 Nginx+PHP环境搭建!
1.Nginx 安装 rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx. ...
- JavaScript的DOM_处理空白节点
一.空白节点产生的原因 在非 IE6,7,8 中,标准的 DOM 具有识别空白文本节点的功能. 在火狐浏览器等其他浏览器中是 7个,而 IE6,7,8 自动忽略了,如果要保持一致的子元素节点,需要手 ...
- Android进阶笔记17:Android手机屏幕坐标系
1. 手机屏幕坐标系: 整个坐标系是以手机屏幕左上角为原点(0,0),如下:
- 阅读MySQL文档第20章:存储程序和函数
本文把阅读到的重点摘抄下来. 一.一个子程序要么是一个程序要么是一个函数.使用CALL语句来调用程序,程序只能用输出变量传回值.就像别其它函数调用一样,函数可以被从语句外调用(即通过引用函数名),函数 ...
- mysql配置远程登录
1.vim /etc/my.cnf注释这一行:bind-address=127.0.0.1 ==> #bind-address=127.0.0.1 2.重启服务:sudo service mys ...