How to Install MongoDB on FreeBSD 13

Updated on March 1, 2022
How to Install MongoDB on FreeBSD 13 header image

Introduction

MongoDB is a document-oriented NoSQL database used for managing, storing, and retrieving document-oriented information structured in a JSON-like format of key and value pairs. MongoDB is mostly used in big data applications and working with ever-growing data sets that don't fit into a relational data model.

MongoDB performs well when deployed in the cloud. It provides excellent operational flexibility, cost-effectiveness, high redundancy, and exceptional data security. In this guide, you will install MongoDB on a FreeBSD 13.0 server.

Prerequisites

To follow along with this guide effectively, make sure to have the following requirements:

Update FreeBSD 13 Base System

An up-to-date FreeBSD system and package repository are essential to rid your system of critical and non-critical bugs, improve functionality, and prevent security vulnerabilities.

SSH into your server and perform the following steps.

  1. To update your FreeBSD system with new and available security patches, version upgrades, and other system files and dependencies, perform the following command:

     $ sudo freebsd-update fetch install
  2. FreeBSD uses a tool called pkg to install, upgrade, and manage binary packages. Update the FreeBSD repository with the most recent database package information.

     $ sudo pkg update -f

The -f flag in the command above forces updates if the default pkg update command is unable to pull the most recent updates.

Install MongoDB

Now with your server up-to-date and pkg ready and running, install MongoDB and its dependencies.

To install the most recent version of MongoDB from the FreeBSD repository, you must first look for the MongoDB binary in the repository. Unfortunately, MongoDB cannot be installed directly with the pkg install mongodb command due to the expired and deprecated MongoDB port.

  1. Search for the MongoDB package with this command:

     $ sudo pkg search mongodb

    As shown below, you should see a list of available MongoDB packages ranging from older versions to the most recent version and other related resources.

     mongodb-tools-100.5.1          Tools for MongoDB 4.4.x and up
     mongodb36-3.6.23               Distributed document-oriented "NoSQL" database
     mongodb40-4.0.27               Distributed document-oriented "NoSQL" database (4.0.x Branch)
     mongodb40-tools-4.0.27         Tools for MongoDB
     mongodb42-4.2.17               Distributed document-oriented "NoSQL" database (4.2.x Branch)
     mongodb42-tools-4.2.17         Tools for MongoDB 4.2.x
     mongodb44-4.4.11               Distributed document-oriented "NoSQL" database (4.4.x Branch)
     mongodb50-5.0.5                Distributed document-oriented "NoSQL" database (5.0.x Branch)
     p5-Mojolicious-Plugin-Mongodb-1.16_1 Use MongoDB in Mojolicious
     p5-MongoDB-2.2.2               Mongo Driver for Perl
     php73-pecl-mongodb-1.9.1       PECL classes for MongoDB
     php74-pecl-mongodb-1.9.1       PECL classes for MongoDB
     php80-pecl-mongodb-1.9.1       PECL classes for MongoDB
     py38-nagios-check_mongodb-0.1  Nagios plugin to check mongodb server

    You will install the most recent version of MongoDB, version 5.0.5, as of the writing of this guide.

  2. To install MongoDB version 5.0.5 run the following command:

     $ sudo pkg install mongodb50-5.0.5

    You will be prompted to proceed with the installation. Press Y and hit Enter to begin.

  3. Confirm that MongoDB has been successfully installed with the following command:

     $ sudo pkg info mongodb50-5.0.5

    The output of the command above would look like this:

     mongodb50-5.0.5
     Name           : mongodb50
     Version        : 5.0.5
     Installed on   : Mon Feb 21 07:48:46 2022 UTC
     Origin         : databases/mongodb50
     Architecture   : FreeBSD:13:amd64
     Prefix         : /usr/local
     Categories     : databases net
     Licenses       : APACHE20, SSPLv1
     Maintainer     : ronald-lists@klop.ws
     WWW            : https://docs.mongodb.com/v5.0/
     Comment        : Distributed document-oriented "NoSQL" database (5.0.x Branch)
     Options        :
                     LTO            : on
                     SASL           : on
                     SSL            : on
     Shared Libs required:
                     libsasl2.so.3
                     libpcre.so.1
                     libpcrecpp.so.0
                     libsnappy.so.1
                     libcurl.so.4
     Annotations    :
                     FreeBSD_version: 1300139
                     cpe            : cpe:2.3:a:mongodb:mongodb:5.0.5:::::freebsd13:x64
                     repo_type      : binary
                     repository     : FreeBSD
     Flat size      : 165MiB
     Description    :
     Mongo (from "humongous") is a high-performance, open source,
     schema-free, document-oriented database. A common name in the
     "NOSQL" community.
    
     WWW: https://docs.mongodb.com/v5.0/

Start MongoDB During System Boot up

To avoid manually starting up MongoDB after system boot, you can set up MongoDB to start automatically on system boot-up. To do that, you need to edit the rc.conf file in the /etc directory.

Before you can edit the rc.conf file, you need a command-line text editor. For this guide, you will use GNU nano.

  1. To install nano perform the following command:

     $ sudo pkg install nano
  2. Edit the rc.conf file in the /etc directory:

     $ sudo nano /etc/rc.conf
  3. Add the following line in the rc.conf file to configure the MongoDB daemon to start automatically when the system boots up:

     mongod_enable="YES"

Press Control+S to save, and Control+X to exit nano.

Start MongoDB Service & Test Database

MongoDB has been successfully installed and set to start automatically when the system is booted. However, the configuration you created has not yet been applied.

  1. For the configuration to take effect, start MongoDB manually:

     $ sudo service mongod start 
  2. Confirm that MongoDB has been successfully started:

     $ sudo service mongod status

    You should get the following output:

     mongod is running as pid 16173
  3. To test the database and further confirm that MongoDB is properly started and fully operational, run the following command to check the connection status of MongoDB:

     $ mongo --eval 'db.runCommand({ connectionStatus: 1 })'

    From the output shown below, MongoDB is up and running on localhost (127.0.0.1) via the MongoDB default port, 27017. The key ok as shown in the JSON-like response, has a value of 1, indicating that the server is running as expected.

     MongoDB shell version v5.0.5
     connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
     Implicit session: session { "id" : UUID("0dc008c4-dab1-4d0f-9745-04d29aade856") }
     MongoDB server version: 5.0.5
     {
                     "authInfo" : {
                                     "authenticatedUsers" : [ ],
                                     "authenticatedUserRoles" : [ ]
                     },
                     "ok" : 1
     }
  4. To stop the MongoDB service (optional), run the following command:

     $ sudo service mongodb stop

Secure MongoDB

MongoDB is not secure by default after installation. As a result, users can read, write, delete, and modify data without requiring authentication from the server. For more information on how to secure MongoDB, refer to the Securing MongoDB article here.

Once you are able to secure MongoDB, restart the MongoDB with the following command:

$ sudo service mongod restart

Conclusion

With this guide, you have successfully updated your FreeBSD system and repository, installed MongoDB, configured it to start on system boot-up, and finally secured the server to prevent unauthorized access. For more information and further guides, see the official documentation: