How to install MongoDB Server on Alpine Linux

Alpine Linux is a Linux distribution optimized for resource efficiency and security. It can be installed in different versions and can therefore run completely from the RAM, for example. In addition to the latest x86 hardware, images are also available for the various Raspberry Pi models. This Linux distribution is based on musl and BusyBox and uses OpenRC for its init system and compiles all user-space binaries as position. Hence, that’s the reason users prefer it for running containers, especially Dockers.

It is an extremely lightweight  Linux operating system that aims at minimalism and high security. The standard Alpine ISO size is around 138 MB only, hence quite easy to move, if required. This makes Alpine fits comfortably on a CD-R with enough space. Alpine Linux uses a technique called location-independent executables this prevents attackers to exploit any flaw in the memory because this feature randomly locates programs in memory.

Steps to install MongoDB Server on Alpine Linux

1. Add Alpine 3.9 repository

Here we are using Alpine 3.14 Linux, however, the mongo-db package is not available in the default repository. Instead, it is in the Alpine v3.9 version repo, that we can easily add to our current Alpine to run the MongoDB server and client.

So, go to your Linux terminal of this OS and use the below-given commands:

Add repo

echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/main' >> /etc/apk/repositories
echo 'http://dl-cdn.alpinelinux.org/alpine/v3.6/community' >> /etc/apk/repositories

Update system

After adding the repositories, run the system update command to refresh the repo cache.

apk update

2. Install MongoDB & Tools on Alpine 3.14 Linux

Once the Repos are added, we can easily download and install MongoDB and tools. So, just follow the below-given commands:

apk add mongodb
apk add mongodb-tools

3. Create a Data folder for MongoDB

We need to create a data folder in the root directory that will use by our MongoDB Instance to store data.

mkdir -p /data/db/

Also, provide the ownership of the folder to the user. Here we have used the root user to create the folder, however, if you have used a particular user to install and use MongoDB then replace the ownership of the folder using the below command accordingly.

chown `root` /data/db

4. Enable and start MongoDB service on Alpine

The service file to start the MongoDB Database server in the background is already there on Alpine, after installing it, under /etc/init.d/. However, we have to start and mark it to run automatically with system boot using the commands given below.

rc-update add mongodb default
rc-service mongodb start
Enable and start MongoDB service on Alpine Linux

Check the Mongo version:

mongo --version
Check Version

5. Access MongoDB Command Line

Once the service is started, we can access the mongo database command line to start creating and managing databases.  To get it simply type:

mongo
MongoDB command line Alpine

6. Stop and Delete MongoDB in Alpine Linux

If you don’t want the database server on your Alpine system anymore then simply remove it using the given commands:

rc-service mongodb stop
apk del mongodb
apk del mongodb-tools
Stop service and delete mongoDB server

Other Articles:

How to install XFCE GUI on Alpine Linux
How to install Docker Engine on Alpine Linux

4 thoughts on “How to install MongoDB Server on Alpine Linux”

  1. not working , will be thankful for any help

    v3.16.1-5-ge692d8f074 [https://dl-cdn.alpinelinux.org/alpine/v3.16/main]
    v3.16.1-13-g9ffada43f7 [https://dl-cdn.alpinelinux.org/alpine/v3.16/community]
    OK: 16880 distinct packages available
    bash-5.1# echo ‘http://dl-cdn.alpinelinux.org/alpine/v3.6/main’ >> /etc/apk/repositories
    echo ‘http://dl-cdn.alpinelinux.org/alpine/v3.6/community’ >> /etc/apk/repositories
    bash-5.1# cat /etc/apk/repositories
    https://dl-cdn.alpinelinux.org/alpine/v3.16/main
    https://dl-cdn.alpinelinux.org/alpine/v3.16/community
    http://dl-cdn.alpinelinux.org/alpine/v3.6/main
    http://dl-cdn.alpinelinux.org/alpine/v3.6/community
    bash-5.1# cat /etc/os-release
    NAME=”Alpine Linux”
    ID=alpine
    VERSION_ID=3.16.1
    PRETTY_NAME=”Alpine Linux v3.16″
    HOME_URL=”https://alpinelinux.org/”
    BUG_REPORT_URL=”https://gitlab.alpinelinux.org/alpine/aports/-/issues”
    bash-5.1# vi /etc/apk/repositories
    bash-5.1# apk update
    fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/main/aarch64/APKINDEX.tar.gz
    fetch https://dl-cdn.alpinelinux.org/alpine/v3.16/community/aarch64/APKINDEX.tar.gz
    v3.16.1-5-ge692d8f074 [https://dl-cdn.alpinelinux.org/alpine/v3.16/main]
    v3.16.1-13-g9ffada43f7 [https://dl-cdn.alpinelinux.org/alpine/v3.16/community]
    OK: 16880 distinct packages available
    bash-5.1# apk add mongodb
    ERROR: unable to select packages:
    mongodb (no such package):
    required by: world[mongodb]
    bash-5.1#

    Reply
  2. in the text it says to add the alpine 3.9 repos, but the code actually adds 3.6.

    mongodb-tools is available in 3.6 but mongodb itself is not. so, the first code box “Add the repos” should read:

    “`
    echo ‘http://dl-cdn.alpinelinux.org/alpine/v3.9/main’ >> /etc/apk/repositories
    echo ‘http://dl-cdn.alpinelinux.org/alpine/v3.9/community’ >> /etc/apk/repositories
    “`

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.