mongocxx-driver編譯安裝

1. 確保安裝epelhtml

yum install -y epel-release

2. 按照《CentOS7.2部署node-mapnik》一文中的步驟,手動安裝 gcc-6.2.0 和 boost-1.65.1 node

3. 按照官方文檔安裝MongoDB,請注意開啓防火牆端口和設置SELinux相關選項,(本示例中建立了數據庫 gis , dbOwner 用戶名爲 root ,密碼爲 111111 )ios

4.  安裝編譯工具及依賴項c++

yum install -y automake autoconf libtool cmake3 openssl-devel unzip

5. 下載解壓 mongo-c-driver 源代碼git

wget https://github.com/mongodb/mongo-c-driver/releases/download/1.10.0/mongo-c-driver-1.10.0.tar.gz

tar -xzvf mongo-c-driver-1.10.0.tar.gz

cd mongo-c-driver-1.10.0

mkdir cmake-build && cd cmake-build

6. 編譯安裝github

#注意後面是兩個..(點號)
cmake3 -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..

make -j4

make install

ldconfig

7. 下載並解壓 mongo-cxx-driver mongodb

wget https://github.com/mongodb/mongo-cxx-driver/archive/r3.2.0.zip

unzip r3.2.0.zip

cd mongo-cxx-driver-r3.2.0

8. 編譯安裝數據庫

cd build

cmake3 -DCMAKE_BUILD_TYPE=Release -DBSONCXX_POLY_USE_BOOST=1 -DCMAKE_INSTALL_PREFIX=/usr/local ..

make -j8

make install

9. 添加 PKG_CONFIG_PATH 環境變量,使用 vim /etc/profile 打開文件,在最下面輸入 export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig ,而後 source /etc/profile 應用環境變量json

10. 測試libmongocxx庫vim

10.1 登陸mongodb mongo -u <user> -p <password> --authenticationDatabase <dbname> 

10.2 插入測試數據 db.points.insert({"hello": "world!"}) 

10.3 輸入c++測試代碼

#include <iostream>

#include <bsoncxx/json.hpp>

#include <mongocxx/instance.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/cursor.hpp>

int main(int, char**)
{
  mongocxx::instance instance{};
  mongocxx::client client{ mongocxx::uri {"mongodb://root:111111@192.168.1.67:27017/?authSource=gis"}};

  auto collection = client["gis"]["points"];

  auto cursor = collection.find({});

  for(auto &&doc : cursor)
  {
    std::cout << bsoncxx::to_json(doc) << std::endl;
  }
}

10.4 編譯c++代碼,生成測試程序

g++ -std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)

10.5 運行測試程序 ./test 

運行結果以下:

 

----------------------2019.02.21----------------------

經驗證:

MongoDB 4.0.6

mongo-c-driver-1.13.1

mongo-cxx-driver-r3.4.0

也可使用此方法編譯安裝

相關文章
相關標籤/搜索