Debian/Ubuntu手动编译安装MongoDB C++11驱动及驱动测试
系统环境:
最小化、无桌面环境 新安装的Debian 8 Server 版本操作系统虚拟机一台
手动编译安装MongoDB C++驱动过程:
在官方网站的这里(https://docs.mongodb.com/ecosystem/drivers/)有各种语言相关驱动信息列表。
本文记录了C++驱动安装过程,驱动安装的大体流程参照官方github的WiKi : https://github.com/mongodb/mongo-cxx-driver/wiki/Quickstart-Guide-(New-Driver)
安装先决条件:
1 Windows or any standard 'nix platform. 2 A modern compiler. We require Clang 3.5+, Apple Clang 5.1+, GCC 4.8.2+, or VC2015 Update 1+. 3 CMake 3.2+. 4 The MongoDB C driver version 1.3.1+.
另外还需要 libbson 和 MongoDB C driver预先安装配置妥当,而MongoDB C driver又要求automake
, autoconf
and libtool,MongoDB C++ driver要求git和pkg-config
sudo apt-get install build-essential automake autoconf libtool git pkg-config -y
安装好以上的基本组件后,还需要安装cmake,由于Debian仓库安装的cmake版本低于官方要求的3.2+,而Ubuntu仓库中的cmake版本则高于官方的要求,
Ubuntu可以直接使用apt-get安装:
sudo apt-get install cmake -y
Debian则使用下面的源码编译安装
进入cmake官网www.cmake.org,在Download页面下载Unix/Linux Source的tar.gz文件,或者在命令行终端下载
wget "https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz"
下载后进行解压、编译、安装
1 tar -zxvf cmake-3.5.2.tar.gz 2 cd cmake-3.5.2/ 3 ./bootstrap 4 make 5 sudo make install
6 cd
安装完毕后查看cmake版本
cmake --version
可以看到编译安装的版本为3.5.2
接下是MongoDB C driver的安装,但MongoDB C driver依赖Libbson,所以得先安装Libbson,官方教程中安装MongoDB C driver时,如果未检测到已安装Libbson,会自动安装Libbson,教程是使用git clone源码,但是git clone下来的源码一方面包含了不稳定版本的Libbson库,另一方面该版本的Libbson存在问题,无法正常编译(Mongodb官方已核实,现已修复,不过我没有测试,但应该没有问题了)因此这里Libbson和MongoDB C driver的源码是通过github项目主页下载release版本得到,具体地址如下:
1 https://github.com/mongodb/mongo-c-driver/releases 2 https://github.com/mongodb/libbson/releases
这里我使用了Libbson-1.3.5,下载地址:
https://github.com/mongodb/libbson/archive/1.3.5.tar.gz
mongo-c-driver 1.3.5版本驱动,下载地址:
https://github.com/mongodb/mongo-c-driver/archive/1.3.5.tar.gz
在终端中下载:
1 wget "https://github.com/mongodb/libbson/archive/1.3.5.tar.gz" -O libbson-1.3.5.tar.gz 2 wget "https://github.com/mongodb/mongo-c-driver/archive/1.3.5.tar.gz" -O mongo-c-driver.tar.gz
下载完毕后,解压安装:
1 tar -zxvf libbson-1.3.5.tar.gz
2 tar -zxvf mongo-c-driver.tar.gz
首先是libbson库的安装
1 cd libbson-1.3.5/ 2 ./autogen.sh
3 make && sudo make install
然后是MongoDB C driver的安装
1 cd 2 cd mongo-c-driver-1.3.5/ 3 ./autogen.sh
4 make && sudo make install 5 cd
在以上各自 install 的过程中可以看到libbson 和 MongoDB C driver的库文件被安装在 /usr/local/lib 目录下
更新说明:
稍新版本的 MongoDB C driver在运行 autogen.sh 脚本时会报错,会提示:
"Not a release archive or a git clone"
"Please download mongoc from https://github.com/mongodb/mongo-c-driver/releases"
打开这个脚本可以看到脚本中做了一个判断,elif [ ! -f src/libbson/autogen.sh ],也即判断当前目录下src/libbson/autogen.sh文件存在不存在,不存在就报错退出。
因此这里应该将libbson源码解压到MongoDB C driver路径下的src/libbson中。
接下来就是MongoDB C++ driver的安装了,回到个人家目录,官方的教程同样是使用git clone下来MongoDB C++ driver的源码进行编译安装,这里我使用和上面安装libbson 和 MongoDB C driver一样的方式来安装MongoDB C++ driver,通过github项目主页下载release版本得到,具体地址如下:
https://github.com/mongodb/mongo-cxx-driver/releases
这里我使用了MongoDB C++ driver 3.0.1,下载地址:
https://github.com/mongodb/mongo-cxx-driver/archive/r3.0.1.tar.gz
在终端中下载:
wget "https://github.com/mongodb/mongo-cxx-driver/archive/r3.0.1.tar.gz" -O mongo-cxx-driver-r3.0.1.tar.gz
下载完毕后,解压安装:
1 tar -zxvf mongo-cxx-driver-r3.0.1.tar.gz 2 cd mongo-cxx-driver-r3.0.1/ 3 cd build/ 4 cmake .. -DCMAKE_INSTALL_PREFIX=/usr/local
注意第四步中使用选项 -DCMAKE_INSTALL_PREFIX=/usr/local 来指定将要安装C++驱动的路径
如果不指定的话,将默认安装在 mongo-cxx-driver-r3.0.1/build/install 路径下
将会导致找不到相应的头文件和库而无法编译
最后两步就是编译与安装了,使用下面的命令执行编译与安装
1 sudo make 2 sudo make install
注意第二步一定要使用sudo来做,因为在上面生成makefile文件时指定了安装到 /usr/local/ 这个路径下
该路径需要root权限才能写入
安装成功后,可以看到 /usr/local/lib 目录下已经成功的生成了相关的库文件,如下图所示
最后测试一下驱动,使用Mongodb官方给的测试代码,代码如下:
#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{}; mongocxx::client conn{mongocxx::uri{}}; bsoncxx::builder::stream::document document{}; auto collection = conn["testdb"]["testcollection"]; document << "hello" << "world"; collection.insert_one(document.view()); auto cursor = collection.find({}); for (auto && doc : cursor) { std::cout << bsoncxx::to_json(doc) << std::endl; } }View Code