Compile MongoDB Embedded 4.2.1 on Raspberry Pi 4

Recently, I'm planning a new tiny project which can run on a Raspberry Pi 4. Well, actually, it can run anywhere, but given that I already bought a Raspberry Pi 4, why not have some fun on that♪(´ε` )

After considerations, a NoSQL database will be used for this project. And I opt for MongoDB. Although there is a MongoDB on Raspbian repo, the version of that is too old -- it's MongoDB 2.4, whereas the newest version is 4.2.1.

Therefore, to ensure that there won't be too many changes because of the updates of MongoDB in the future, let's use the latest version of MongoDB~ And of course, there will be a long time to compile MongoDB on Raspberry Pi. ( ;´Д`)

At the time of writing, the release version of Raspbian on my Raspberry Pi 4 is

$ cat /etc/os-release
PRETTY_NAME="Raspbian GNU/Linux 10 (buster)"
NAME="Raspbian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

To prepare the compile environment is not really time-consuming, but it will take several hours to do the actual compiling. Anyway, you can faff around while compiling. (*^3^)

  1. apt install dependencies
  2. pip3 install dependencies
  3. Compile Mongo C Driver
  4. Compile and Install MongoDB

1. apt install dependencies

sudo apt-get install -y scons libssl-dev python3 python3-pip libffi-dev libcurl4-openssl-dev cmake wget

scons is a software construction tool that MongoDB opts for. It can automatically analyse the dependency between codes, and support multiple languages. And it has many more features, you may visit its official website if you're interested in. https://scons.org

2. pip3 install dependencies

Here I refer to the compile-requirements.txt on MongoDB's GitHub repo. https://github.com/mongodb/mongo/blob/r4.3.0/etc/pip/compile-requirements.txt

However, this compile-requirements.txt uses some other requirements.txt, thus I gather them together and write a single one

Cheetah3 # src/mongo/base/generate_error_codes.py
psutil
pymongo >= 3.0, != 3.6.0  # See PYTHON-1434, SERVER-34820
PyYAML >= 3.0.0
regex
requests >= 2.0.0
typing >= 3.6.4

You may copy and paste the code above into a build-requirements.txt file, and then install all the libraies by pip3

sudo pip3 install -r ./build-requirements.txt

If you're in China, you may use TUNA mirror repo.

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r ./build-requirements.txt

3. Compile Mongo C Driver

One of the optional dependency of Mongo C Driver is snappy, a high-performance compression library developed by Google. Since that it won't take more than 5 minutes to compile and install snappy, let's do it.

3.1 Compile and Install snappy

The source code of snappy can be found under its GitHub Release page, https://github.com/google/snappy/releases

And the version of the latest snappy when I'm writing this post is 1.1.7, released on Aug 25th 2017. The link to that tarball is https://github.com/google/snappy/archive/1.1.7.tar.gz

You may connect to your Raspberry Pi via SSH, and then type commands below to download, compile and install snappy.

export SNAPPY_VER=1.1.7
wget https://github.com/google/snappy/archive/\${SNAPPY_VER}.tar.gz -O snappy-\${SNAPPY_VER}.tar.gz
tar xzf snappy-\${SNAPPY_VER}.tar.gz
cd snappy-\${SNAPPY_VER}
mkdir build && cd build
cmake -DBUILD_SHARED_LIBS=ON .. && make -j`nproc` && sudo make install

Here we switch on BUILD_SHARED_LIBS, because Mongo C Driver requires a dynamic link to snappy. If the static library of snappy presents only, the Mongo C Driver will complain about that and result in compile error.

3.2 Compile Mongo C Driver

The source code of Mongo C Driver can be obtained at its Github Release page. https://github.com/mongodb/mongo-c-driver/releases

At the time of writing, the newest version of Mongo C Driver is 1.15.2 released on Nov 7th 2019. The download link is https://github.com/mongodb/mongo-c-driver/releases/download/1.15.2/mongo-c-driver-1.15.2.tar.gz

To compile Mongo C Driver, you may following the commands below

export MONGO_C_VER=1.15.2
wget https://github.com/mongodb/mongo-c-driver/releases/download/\${MONGO_C_VER}/mongo-c-driver-\${MONGO_C_VER}.tar.gz -O mongo-c-driver-\${MONGO_C_VER}.tar.gz
tar xzf mongo-c-driver-\${MONGO_C_VER}.tar.gz
cd mongo-c-driver-\${MONGO_C_VER}
mkdir release && cd release
cmake .. && make -j`nproc` && sudo make install

4. Compile and Install MongoDB Embedded

MongoDB's source code can be downloaded from its official website, https://www.mongodb.com/download-center/community. And on the right half of that page, there is a link displayed as Download source (tgz).

Again, at the time of composing this post, the latest version of MongoDB is 4.2.1

export MONGODB_VER=4.2.1
wget https://fastdl.mongodb.org/src/mongodb-src-r\${MONGODB_VER}.tar.gz -O mongodb-src-r${MONGODB_VER}.tar.gz
tar xzf mongodb-src-r\${MONGODB_VER}.tar.gz
cd mongodb-src-r\${MONGODB_VER}

However, it seems that there is a type-casting issue in r4.2.1, because GCC 8.3.0 threw me a fatal error at my first shot.

So certain modification should be made before compiling, the corresponding file is src/mongo/util/net/ssl_manager.cpp

On line 941, a type-casting should be added (highlighted below)

if (mongoUnsignedAddOverflow64(tagAndLengthByteCount, derLength, (long unsigned int*)outLength)) ||
    *outLength > cdr.length()) {
    return Status(ErrorCodes::InvalidSSLConfiguration, "Invalid DER length");
}

Then we can compile MongoDB Embedded without any error. ♪(´ε` )

./buildscripts/scons.py --link-model=dynamic \
    --install-mode=hygienic \
    --disable-warnings-as-errors \
    --enable-free-mon=off \
    --js-engine=none \
    --dbg=off \
    --wiredtiger=off \
    --use-system-mongo-c=on \
    --allocator=system \
    --release=RELEASE \
    CPPPATH="/usr/local/include/libbson-1.0 /usr/local/include/libmongoc-1.0" \
    LIBPATH="/usr/local/lib" \
    CCFLAGS="-mabi=aapcs-linux -march=armv8-a+crc+simd" \
    install-embedded-{dev,test} -j`nproc`

After successfully compiled, we can install MongoDB Embedded

cd build/install
sudo cp -R * /usr/local

And use it!

mkdir -p ${HOME}/mongodb
mongoed --dbpath=${HOME}/mongodb

Leave a Reply

Your email address will not be published. Required fields are marked *

nineteen + eighteen =