To install the MongoDB C++ driver (mongo-cxx-driver) on a CentOS 7 system, follow these step-by-step instructions.

Step 1: Install Prerequisites

Before installing the MongoDB C++ driver, you need to install several dependencies.

  1. Update your system:
sudo yum update -y

Install development tools:

sudo yum groupinstall "Development Tools" -y

Install CMake: CentOS 7 comes with an older version of CMake. You can install the latest version as follows:

sudo yum install -y epel-release
sudo yum install -y cmake3
sudo ln -s /usr/bin/cmake3 /usr/bin/cmake

Install dependencies:

sudo yum install -y openssl-devel boost-devel libcurl-devel

Step 2: Install the MongoDB C Driver (libmongoc)

The C++ driver depends on the MongoDB C driver, so you need to install it first.

  1. Download the MongoDB C driver source:
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.23.4/mongo-c-driver-1.23.4.tar.gz

  1. Extract the tarball:bashCopy codetar -xzvf mongo-c-driver-1.23.4.tar.gz cd mongo-c-driver-1.23.4
  2. Build and install:bashCopy codemkdir cmake-build cd cmake-build cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_INSTALL_PREFIX=/usr/local .. make sudo make install

Step 3: Install the MongoDB C++ Driver (mongo-cxx-driver)

Now that the MongoDB C driver is installed, you can install the MongoDB C++ driver.

  1. Download the MongoDB
C++ driver source:bashCopy codewget https://github.com/mongodb/mongo-cxx-driver/releases/download/r3.7.0/mongo-cxx-driver-r3.7.0.tar.gz

Extract the tarball:

tar -xzvf mongo-cxx-driver-r3.7.0.tar.gz cd mongo-cxx-driver-r3.7.0

Build and install:

mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local .. make sudo make install

Step 4: Verify the Installation

To verify that the MongoDB C++ driver has been installed correctly, you can write a simple test program.

  1. Create a test file:bashCopy codenano test_mongo.cpp
  2. Paste the following code into the file
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <bsoncxx/json.hpp>

int main() {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};

    bsoncxx::document::value doc = bsoncxx::builder::stream::document{} << "hello" << "world" << bsoncxx::builder::stream::finalize;

    auto collection = conn["testdb"]["testcollection"];
    collection.insert_one(doc.view());

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

    return 0;
}

  1. Compile the test program:
g++ test_mongo.cpp -o test_mongo \ -std=c++11 -I/usr/local/include/mongocxx/v_noabi \ -I/usr/local/include/bsoncxx/v_noabi -L/usr/local/lib \ -lmongocxx -lbsoncxx
  1. Run the test program:bashCopy code./test_mongo

If everything was installed correctly, the program should run without errors and output the inserted document.

Step 5: Set Up Environment Variables (Optional)

You can add the MongoDB C++ driver paths to your environment variables to simplify future compilations.

Add the following to your .bashrc or .bash_profile:

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

Reload the environment variables:

source ~/.bashrc

That’s it! The MongoDB C++ driver should now be successfully installed and ready for use on your CentOS 7 system.

Leave a Reply

Discover more from Geeky Codes

Subscribe now to keep reading and get access to the full archive.

Continue reading