mongocxx-driver編譯安裝


1. 確保安裝epel

yum install -y epel-release

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

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

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

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

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

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. 編譯安裝

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

make -j4

make install

ldconfig

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

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 應用環境變量

10. 測試libmongocxx庫

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

也可以使用此方法編譯安裝


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM