一、RedHat/CentOS下的安装步骤
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-red-hat/
下载mongodb压缩包
http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.10.tgz
也可以使用下载好的安装包: mongodb-linux-x86_64-2.6.10.gz
上传到/home/itcast/install
目录
# root用户登录
tar -zxvf mongodb-linux-x86_64-2.6.10.gz
mv mongodb-linux-x86_64-2.6.10 /usr/local/mongodb
cd /usr/local/mongodb
mkdir db
mkdir logs
cd bin
增加配置文件
vi mongodb.conf
dbpath=/usr/local/mongodb/db
logpath=/usr/local/mongodb/logs/mongodb.log
port=27017
fork=true
nohttpinterface=true
开机自动启动mongodb
vi /etc/rc.d/rc.local
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/bin/mongodb.conf
建议mongo快捷启动
ln -s /usr/local/mongodb/bin/mongo /usr/local/bin/mongo
二、C++驱动的下载源
1.26compat下载网址
https://github.com/mongodb/mongo-cxx-driver/tree/26compat
2.legacy 下载网址
https://github.com/mongodb/mongo-cxx-driver/tree/legacy
3.master 下载网址
https://github.com/mongodb/mongo-cxx-driver/tree/master
三、Redhat/centOS下安装MongoDB的C++驱动
1.安装boost库
http://jaist.dl.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.zip
下载后上传到/home/itcast/install
$unzip boost_1_55_0.zip
$cd boost_1_55_0
修改boosttrap.sh 文件(为以后移植做准备),
PREFIX=/usr/local/boost
$./boosttrap.sh
…
$./b2
…
$sudo ./b2 install
…
时间有点长,可以歇会冲杯咖啡.
在/etc/ld.so.conf
文件中,添加下行
/usr/local/boost/lib
刷新库目录
$sudo ldconfig –v
2.安装PCRE
下载pcre-8.00.tar.gz (http://jaist.dl.sourceforge.net/project/pcre/pcre/8.00/pcre-8.00.tar.gz)
#tar -zxvf pcre-8.00.tar.gz
#cd pcre-8.00
#./configure
#make
#make install
3.安装scons
因为mongoDB驱动编译不是用CMake
,需要下载scons
下载scons-2.5.0-1.noarch.rpm
( http://jaist.dl.sourceforge.net/project/scons/scons/2.5.0/scons-2.5.0-1.noarch.rpm )
#rpm -ihv scons-2.5.0-1.noarch.rpm
4.编译驱动程序
下载mongodb
的驱动程序http://downloads.mongodb.org/cxx-driver/mongodb-linux-x86_64-v2.2-latest.tgz
得到文件mongodb-linux-x86_64-v2.2-latest.tgz
解压文件
$tar –zxvf mongodb-linux-x86_64-v2.2-latest.tgz
进入目录
$cd mongo-cxx-driver-v2.2
执行编译
$sudo scons --extrapath=/usr/local install
等待成功!
如果这样简单就好了….
正常情况下会报错: # error Compiling Filesystem version 3 file with BOOST_FILESYSTEM_VERSION defined != 3
这是因为驱动V2.2
与boost1.55
版本不够兼容,可以考虑用boost1.49版本,下面的介绍的是另外一个解决办法.
修改 src/mongo/pch.h
文件,将BOOST_FILESYSTEM_VERSION
的值定义为3
$vi src/mongo/pch.h
修改前
#define BOOST_FILESYSTEM_VERSION 2
修改后
#define BOOST_FILESYSTEM_VERSION 3
这样mongo的文件系统设置与boost库一致了,可以再次尝试编译.
重新编译,如果顺利应该会得到 libmongoclient.a
/usr/local/mongo
目录为我们得到的安装结果,/usr/local/boost
为我们安装的boost
库,下节介绍的内容是基于本节得到结果进行部署安装.
安装文件部署发布
mongo_CXX_driver.tar.gz
安装包部署,包含boost
库加MongoDB
驱动
用itcast
用户上传到$HOME/driver(/home/itcast/driver)
目录
$tar -zxvf mongo_CXX_driver.tar.gz
切换到root用户
$vi /etc/ld.so.conf
添加如下两行,使boost
库和mongoapi
可以运行时被找到(这里也可以将相应库文件拷贝到$PATH
对应的目录)
/home/itcast/driver/boost/lib
/home/itcast/driver/mongo/lib
$ldconfig
以上步骤结束后,可以启动mongo数据(mongod),编写测试程序进行连接.
//g++ -o testmongo testmongo.cpp -lmongoclient -lboost_thread -lboost_filesystem -lboost_program_options -L/home/itcast/driver/boost/lib -L/home/itcast/driver/mongo/lib -I/home/itcast/driver/mongo/include -I/home/itcast/driver/boost/include
#include <iostream>
#include "mongo/client/dbclient.h"
char dbhost[20]="localhost";
using namespace mongo;
using namespace std;
void printIfAge(DBClientConnection& c, int age) {
auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") );
while( cursor->more() ) {
BSONObj p = cursor->next();
cout << p.getStringField("name") << endl;
}
}
void run() {
DBClientConnection c;
c.connect(dbhost);
cout << "connected ok" << endl;
BSONObj p = BSON( "name" << "Joe" << "age" << 33 );
c.insert("tutorial.persons", p); /**< 向person表中插入数据 */
p = BSON( "name" << "Jane" << "age" << 40 );
c.insert("tutorial.persons", p);
p = BSON( "name" << "Abe" << "age" << 33 );
c.insert("tutorial.persons", p);
p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" );
c.insert("tutorial.persons", p);
c.ensureIndex("tutorial.persons", fromjson("{age:1}"));
cout << "count:" << c.count("tutorial.persons") << endl; /**< 显示person表中的数据数目 */
auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());
while( cursor->more() ) {
cout << cursor->next().toString() << endl;
}
cout << "\nprintifage:\n";
printIfAge(c, 33);
}
int main(int argc,char *argv[]) {
if(argc == 2)
{
memset(dbhost,0x00,sizeof(dbhost));
strcpy(dbhost,argv[1]);
printf("connect to dbhost:[%s]\n",dbhost);
}
else
{
printf("connect to dbhost:[%s]\n",dbhost);
printf("if you need to connet to remote service,please input ip!\n");
}
try {
run();
}
catch( DBException &e ) {
cout << "caught " << e.what() << endl;
}
return 0;
}
编译指令在文件头,注意-I 和 –L参数对应的路径.