MongoDB的源代码依赖了一些基础组件,如js引擎Spider Monkey,正则表达式引擎PCRE,安装构建工具Scons,以及C++的boost库等

1、准备

wget http://downloads.mongodb.org/src/mongodb-src-r1.8.2.tar.gz
wget http://sourceforge.net/projects/scons/files/scons/2.1.0.alpha.20101125/scons-2.1.0.alpha.20101125.tar.gz/download
wget http://ftp.mozilla.org/pub/mozilla.org/js/js-1.7.0.tar.gz
wget http://sourceforge.net/projects/pcre/files/pcre/7.4/pcre-7.4.tar.gz/download
wget http://downloads.sourceforge.net/project/boost/boost/1.42.0/boost_1_42_0.tar.gz?use_mirror=cdnetworks-kr-1

2、scons编译安装

tar zxvf scons-2.1.0.alpha.20101125.tar.gz
cd scons-2.1.0.alpha.20101125
python setup.py install

3、pcre编译安装

tar zxvf pcre-7.4.tar.gz
cd pcre-7.4
configure --enable-unicode-properties
make -j 2 && make install

configure中要加入--enable-unicode-properties支持,要不运行mongod会出现下面错误

Fri Dec 30 23:53:45 Assertion: 10342:pcre not compiled with utf8 support
0x53f16a 0x5cfda1 0x8945f3 0x338101d994 0x4c0e49
/usr/local/mongodb/bin/mongod(_ZN5mongo11msgassertedEiPKc+0x12a) [0x53f16a]
/usr/local/mongodb/bin/mongod(_ZN5mongo6RXTest3runEv+0x3b1) [0x5cfda1]
/usr/local/mongodb/bin/mongod(main+0x5c73) [0x8945f3]
/lib64/libc.so.6(__libc_start_main+0xf4) [0x338101d994]
/usr/local/mongodb/bin/mongod(__gxx_personality_v0+0x451) [0x4c0e49]

4、js编译安装

tar zxvf js-1.7.0.tar.gz
cd js/src
export CFLAGS="-DJS_C_STRINGS_ARE_UTF8"
make -f Makefile.ref
JS_DIST=/usr make -f Makefile.ref export

5、boost编译安装

tar -zxvf boost_1_42_0.tar.gz
cd boost_1_42_0/
#使用下面的命令创建boost自己的编译工具bjam(目的是保证Boost在任何平台上都能用bjam编译),类似于GNU Make。
./bootstrap.sh
#会安装头文件和库文件到相应指定目录,头文件安装到/usr/local/include下,库文件安装到/usr/local/lib下
install
#最后执行的创建命令:
./bjam –without-python –layout=tagged variant=debug,release link=static,shared runtime-link=shared threading=multi stage

6、mongodb编译安装

tar -xzvf mongodb-src-r1.8.2.tar.tar
cd mongodb-src-r1.8.2/
scons all
scons --prefix=/usr/local/mongodb --full install

安装成功后 安装到/usr/local/mongodb下

文件结果

bin
GNU-AGPL-3.0
include
lib64
README
THIRD-PARTY-NOTICES

7、加入环境变量

/etc/profile 加入

CLASSPATH=.:/usr/local/lib
export CLASSPATH

然后source /etc/profile

完成后就可以运行mongodb。

[root@test_192.168.4.95 mongodb]# bin/mongo
MongoDB shell version: 1.8.2
connecting to: test

运行可能会出现下面错误

/usr/local/mongodb/bin/mongod: error while loading shared libraries: libboost_system.so.1.42.0: cannot open shared object file: No such file or directory

解决方法

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig