环境:Ubantu

mongoc driver和BSON库的安装

1.简介
  1. mongocxx driver在mongoc driver基础上构建,因此下载安装mongocxx driver之前需要先安装mongoc driver。
For mongocxx-3.8.x, libmongoc 1.24.0 or later is required.
For mongocxx-3.7.x, libmongoc 1.22.1 or later is required.
For mongocxx-3.6.x, libmongoc 1.17.0 or later is required.
For mongocxx-3.5.x, libmongoc 1.15.0 or later is required.
For mongocxx-3.4.x, libmongoc 1.13.0 or later is required.
For mongocxx-3.3.x, libmongoc 1.10.1 or later is required.
For mongocxx-3.2.x, libmongoc 1.9.2 or later is required.
For mongocxx-3.1.4+, libmongoc 1.7.0 or later is required.
For mongocxx-3.1.[0-3], libmongoc 1.5.0 or later is required.
For mongocxx-3.0.x, we recommend the last 1.4.x version of libmongoc
2.mongoc driver和BSON库的安装

驱动版本:1.24.4

  1. 前提:安装OpenSSL相关的包
 sudo apt-get install cmake libssl-dev libsasl2-dev
  1. 下载mongoc driver驱动,解压
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.24.4/mongo-c-driver-1.24.4.tar.gz
tar xzf mongo-c-driver-1.24.4.tar.gz
cd mongo-c-driver-1.24.4
  1. 配置
    1. 选项-DBSON_OUTPUT_BASENAME:指定生成的库的名称
    mkdir cmake-build && cd cmake-build
    cmake   ..
    
  2. build:make && make install

mongocxx driver的配置和安装

version:3.8.1,这是最新的稳定版本。

  1. 选择一个C++17 polyfill
    1. 非Windows平台:执行cmake命令时传递参数-DBSONCXX_POLY_USE_MNMLSTC=1
    2. windows平台:执行cmake命令时传递参数-DBSONCXX_POLY_USE_BOOST=1
  2. 下载mongocxx driver驱动,解压
wget https://github.com/mongodb/mongo-cxx-driver/releases/download/r3.8.1/mongo-cxx-driver-r3.8.1.tar.gz

tar -zxvf mongo-cxx-driver-r3.8.1.tar.gz
  1. 配置
    1. 选项-DCMAKE_INSTALL_PREFIX表示将mongocxx驱动安装到指定位置
    2. 选项-DCMAKE_INSTALL_RPATH表示将生成的lib安装到指定的目录
    3. 选项-DCMAKE_PREFIX_PATH:指定搜索依赖包的路径
    4. 选项-DCMAKE_BUILD_TYPE表示构建项目的类型,其值有Release、Debug等
    5. 选项DBUILD_SHARED_LIBS:值为OFF,表示构建目标作为静态库
    cd mongo-cxx-driver-r3.8.1/build
    
    cmake ..                                \
    -DCMAKE_BUILD_TYPE=Release          \
    -DBSONCXX_POLY_USE_MNMLSTC=1        \
    -DCMAKE_PREFIX_PATH=/root/cxx17/External/mongoc\lib64 \
    -DCMAKE_INSTALL_PREFIX=/root/cxx17/External/mongocxx \
    -DCMAKE_INSTALL_RPATH=/root/cxx17/External/mongocxx/lib64 \
    -DBUILD_SHARED_LIBS=OFF
    
  2. build:make -j 4 && make install

MongoDB的安装

版本:4.4,社区版

  1. install gnupg and curl
sudo apt-get install gnupg curl
  1. import the MongoDB public GPG Key from https://pgp.mongodb.com/server-4.4.asc
    :
curl -fsSL https://pgp.mongodb.com/server-4.4.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-4.4.gpg \
   --dearmor
  1. Create the list file /etc/apt/sources.list.d/mongodb-org-4.4.listfor your version of Ubuntu.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-4.4.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
  1. Reload local package database
sudo apt-get update
  1. Install the MongoDB packages
sudo apt-get install -y mongodb-org=4.4.25 mongodb-org-server=4.4.25 mongodb-org-shell=4.4.25 mongodb-org-mongos=4.4.25 mongodb-org-tools=4.4.25
  1. 验证MongoDB是否安装成功
// 启动MongoDB server
sudo service mongod start
sudo service mongod status

问题: 执行第五步骤报错,则需要安装libssl1,再继续第五步骤。

下列软件包有未满足的依赖关系:
mongodb-org-mongos : 依赖: libssl1.1 (>= 1.1.0) 但无法安装它
mongodb-org-server : 依赖: libssl1.1 (>= 1.1.0) 但无法安装它
mongodb-org-shell : 依赖: libssl1.1 (>= 1.1.0) 但无法安装它
E: 无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系。
  1. Download libssl1.1_1.1.1f-1ubuntu2_amd64.deb from official repository:
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
  1. Install it
sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

boost

版本:boost-1.80.0.beta1

  1. 源码下载
git clone https://github.com/boostorg/boost.git
cd boost
git checkout boost-1.80.0.beta1

protobuf

  1. 版本:v3.20.3
  2. 参考:Protocol Buffers的基本使用

rediscxx

版本:1.3.10

  1. 源码下载
git clone https://github.com/sewenew/redis-plus-plus.git
cd redis-plus-plus/
  1. 编译
mkdir build && cd build
cmake ..
make -j 4

hiredis

版本:v0.13.1

  1. 源码下载
git clone https://github.com/redis/hiredis.git
cd hiredis/
git checkout v0.13.1
  1. 编译生成libhiredis.a、libhiredis.so库
make

libevent

版本:release-2.1.12-stable

  1. 源码下载
git clone https://github.com/libevent/libevent.git
cd libevent/
git checkout release-2.1.12-stable
  1. 编译生成库,主要有libevent.a、libevent_openssl.a
 $ mkdir build && cd build
 $ cmake ..     # Default to Unix Makefiles.
 $ make -j 4

openssl

项目地址:https://github.com/openssl/openssl.git,版本:openssl-3.1.0-beta1

  1. 源码下载
git clone https://github.com/openssl/openssl.git
cd openssl
git checkout openssl-3.1.0-beta1
  1. 生成Makefile
./Configure
  1. 编译生成libcrypto.a、libcrypto.so、libssl.a、libssl.so等库文件
make -j 4

tinyxml2

项目地址:https://github.com/leethomason/tinyxml2,版本:9.0.0

  1. 源码下载:
git clone  https://github.com/leethomason/tinyxml2
cd tinyxml2/
git checkout 9.0.0
  1. 生成Makefile
cmake .
  1. 编译生成静态库libtinyxml2.a
make -j 4

jsoncpp

版本:v3.11.1

  1. 源码下载
git clone https://github.com/nlohmann/json.git
cd json
git checkout v3.11.1
  1. 编译
mkdir build && cd build
cmake ..
make -j 4