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 on 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 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 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

 

Stop and Delete (optional)

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

Leave a Reply

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