1. 程式人生 > >Linux下Mongodb的C++操作配置

Linux下Mongodb的C++操作配置

首先安裝MongoDB,最簡單的安裝方法在終端輸入:

sudo apt-get install mongodb

如果出錯就:sudo apt-get update 更新下

然後啟動資料庫MongoDB:mongo。預設連線到test。然後就可以在Shell下簡單操作Mongodb了。

============================安裝pcre,scons(Mongodb輔助工具),boost庫==================================

    #tar pcre-8.00.tar.gz     #cd pcre-8.00     #./configure        #make install                             //pcre完成 然後終端下輸入:sudo apt-get install scons   //scons安裝完成 安裝boost庫 下載:wget -O boost_1_54_0.tar.bz2 http://downloads.sourceforge.net/project/boost/boost/1.54.0/boost_1_54_0.tar.bz2?r=http%3A%2F%2Fwww.boost.org%2Fusers%2Fhistory%2Fversion_1_54_0.html&ts=1375162670&use_mirror=jaist //下載boost包,我用的是1.54
解壓:tar --bzip2 -xf ./boost_1_54_0.tar.bz2 開始編譯,全部編譯耗時太多,所以我僅選擇我需要的庫:
先用下面的命令檢視有多少庫可以編譯:./bootstrap.sh --show-libraries
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2


The following Boost libraries have portions that require a separate build
and installation step. Any library not listed here can be used by including
the headers only.


The Boost libraries requiring separate building and installation are:
    - atomic
    - chrono
    - context
    - coroutine
    - date_time
    - exception
    - filesystem
    - graph
    - graph_parallel
    - iostreams
    - locale
    - log
    - math
    - mpi
    - program_options
    - python
    - random
    - regex
    - serialization
    - signals
    - system
    - test
    - thread
    - timer
    - wave
然後就編譯我要的庫:./bootstrap.sh --with-libraries=system,filesystem,log,thread
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2
Unicode/ICU support for Boost.Regex?... not found.
Generating Boost.Build configuration in project-config.jam...


Bootstrapping is done. To build, run:


    ./b2
    
To adjust configuration, edit 'project-config.jam'.
Further information:


   - Command line help:
     ./b2 --help
     
   - Getting started guide: 
     http://www.boost.org/more/getting_started/unix-variants.html
     
   - Boost.Build documentation:
     http://www.boost.org/boost-build2/doc/html/index.html
然後執行下面的命令完成編譯: ./b2 編譯需要一段時間,完成後:The Boost C++ Libraries were successfully built!
    The following directory should be added to compiler include paths:
   /usr/src/boost_1_54_0
  The following directory should be added to linker library paths:
  /usr/src/boost_1_54_0/stage/lib

再執行./b2 install 命令,預設安裝在

/usr/local/lib目錄下

標頭檔案在

/usr/local/include/boost目錄下

install 後面可以加引數--prefix=/usr                                                                          //boost庫安裝完成

===============================MongoDB c++ driver======================

首先獲取原始碼:git clone https://github.com/mongodb/mongo-cxx-driver.git

然後切換分支:git checkout 26compat

然後用下面的命令編譯:scons --prefix=/usr --use-system-boost --sharedclient --full install-mongoclient

   這個命令會編譯出.so和.a庫,位於build目錄: # ls build/linux2/use-system-boost/libmongoclient.a -alh
  -rw-r--r-- 1 root root 57M  5月 10 23:35 build/linux2/use-system-boost/libmongoclient.a
  # ls build/linux2/use-system-boost/libmongoclient.so -alh
  -rwxr-xr-x 1 root root 20M  5月 10 23:37 build/linux2/use-system-boost/libmongoclient.so

並且安裝到了/usr/lib/目錄下,標頭檔案在/usr/include目錄下:
# ls /usr/lib/libmongoclient.* -alh
-rw-r--r-- 1 root root 57M  5月 10 23:35 /usr/lib/libmongoclient.a
-rwxr-xr-x 1 root root 20M  5月 10 23:37 /usr/lib/libmongoclient.so