想在Linux下编译一下mongo和它的C++驱动,网上一堆教程,官方一堆文档,实现起来依旧困难重重。我就很纳闷了,本来mongodb就是C++写的,为什么不直接提供出驱动,而java,C#之类的,倒是直接提供了jar包和dll文件。今天终于编译成功,赶紧记录下来。
Linux版本:CentOS 5.4
C++驱动版本:mongo-cxx-driver-v2.2
Mongodb版本:mongodb-src-r2.2.6
Python版本:Python-2.7.5
scons版本:scons-src-2.3.0
boost版本:boost_1_49_0
步骤:
1. 升级boost
centos5.4自带了boost,但版本过老,需要进行升级:
rpm -qa boost* # 先查一下安装的版本
rpm -e xxxx # 卸载之
去boost官网下载boost库,推荐1.49版本,注意,作者第一次下载了1.54版本的boost,后来在编译C++驱动的时候,死活编译不过去,出现error: #error Compiling Filesystem version 3 file with BOOST_FILESYSTEM_VERSION defined != 3的错误,Boost官网http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/index.htm有这么一段话:This is Version 3 of the Filesystem library. Version 2 is not longer supported. 1.49.0 was the last release of Boost to supply Version 2。
最后又重新安装的1.49版本的。
下载boost_1_49_0.tar.bz2
解压后,执行
$ cd boost_1_49_0
$ ./bootstrap.sh
…
$ ./b2
…
$ sudo ./b2 install
…
时间有点长,可以先出去抽个烟,上个厕所。
2.升级python
参考:http://www.netingcn.com/centos-python-upgrade.html
http://running.iteye.com/blog/1122370
3.安装scons
下载scons-2.3.0-1.noarch.rpm ( http://www.scons.org/download.php )
安装:
#rpm -uvh scons-2.2.0-1.noarch.rpm
4. 编译mongodb
从官网上面下载mongo源码(http://www.mongodb.org/downloads),下载的版本是mongodb-src-r2.2.6.tar.gz
5.编译C++客户端V2.2驱动
下载:wget http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-v2.2-latest.tgz
tar zxf mongodb-linux-x86_64-v2.2-latest.tgz
cd mongo-cxx-driver-v2.2
scons
scons install
ldconfig /usr/local/lib
安装成功!
测试一下吧:
首先启动mongo数据库:
[root@localhost mongo]# cd /usr/local/mongo/bin
[root@localhost bin]# ./mongo
MongoDB shell version: 2.2.6
connecting to: test
Mon Oct 14 15:46:25 Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91
exception: connect failed
[root@localhost bin]# ./mongod -dbpath=/usr/local/mongo/data/db
出现couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91 这个错误,我把数据库文件路径定义在了
/usr/local/mongo/data/db下面,需要自己建data、db两个文件夹。
启动成功后,进入mongo-cxx-driver-v2.2目录下的src目录,建立一个测试文件test.cpp
执行:g++ test.cpp -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options#include<cstdlib> #include<iostream> #include"mongo/client/dbclient.h" void run(){ mongo::DBClientConnection c; c.connect("localhost");} int main() { try{ run(); std::cout <<"connected ok"<< std::endl; }catch(const mongo::DBException&e ){ std::cout <<"caught "<< e.what()<< std::endl;} return EXIT_SUCCESS; }
生成a.out文件
出现error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory这个错误,是因为我没有执行[root@localhost src]# ./a.out ./a.out: error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory [root@localhost src]# cd .. [root@localhost mongo-cxx-driver-v2.2]# ldconfig /usr/local/lib
ldconfig /usr/local/lib,执行完以后,再试一下:
再试试mongodb自带的例子,进入mongodb-src-r2.2.6/src/mongo/client/examples[root@localhost src]# ./a.out connected ok
成功了,现在你可以开始用c++编写操作mongoDB的程序[root@localhost examples]# g++ first.cpp -I /usr/local/mongo/include -L /usr/local/mongo/lib/libmongoclient.a -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -o first [root@localhost examples]# ./first using cursor { "_id" : { "$oid" : "525bb336ab8ef21b5b7886db" }, "name" : "eliot", "num" : 15 } { "_id" : { "$oid" : "525bb336ab8ef21b5b7886dc" }, "name" : "sara", "num" : 23 } 0 { "_id" : { "$oid" : "525bb336ab8ef21b5b7886db" }, "name" : "eliot", "num" : 15 } 1 {} [root@localhost examples]#
参考文档:
http://lingzhi007.iteye.com/blog/1844127
http://blog.sina.com.cn/s/blog_77c35cff01018qzt.html
http://www.cnblogs.com/logicbaby/archive/2011/07/04/2097697.html
http://www.cnblogs.com/mobileinternet/archive/2012/08/31/2665144.html