How to install and configure MongoDB 5 on FreeBSD 13

In this guide we are going to learn how to install MongoDB 5.0 Community Edition on a FreeBSD 13 server.

MongoDB is a cross-platform document-oriented NoSQL database program that uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

MongoDB was built for people building internet and business applications who need to evolve quickly and scale elegantly. Companies and development teams of all sizes use MongoDB for a wide variety of reasons.

Instead of storing data in tables of rows or columns like SQL databases, each record in a MongoDB database is a document described in BSON, a binary representation of the data. Applications can then retrieve this information in a JSON format.

Here’s a simple JSON document describing a historical figure.

{
  "_id": 1,
  "name": {
    "first": "Ada",
    "last": "Lovelace"
  },
  "title": "The First Programmer",
  "interests": ["mathematics", "programming"]
}

# Prerequisites

To follow along, ensure you have the following:

  • An up to date FreeBSD 11 server
  • Knowledge of Linux Terminal
  • Access to the internet
  • Root access to the server or User with Sudo access

# Table of Content

  1. Ensuring the server is up to date
  2. Instaling MongoDB
  3. Allowing MongoDB port on firewall
  4. Starting and enabling MongoDB service
  5. Connecting to Mongodb and Executing some test commands
  6. Uninstall MongoDB Community Edition

# 1. Ensuring the server is up to date

You need to become root to proceed.

$ su -
root@freebsd:~ #

Before proceeding, let us make sure that our server repos and packages are in the latest versions by updating using this command:

pkg update
pkg upgrade -y

# 2. Installing MongoDB Community Edition

Search for mongodb

# pkg search mongodb | grep 5.0
mongodb-tools-100.5.0          Tools for MongoDB 4.4.x and up
mongodb50-5.0.2                Distributed document-oriented "NoSQL" database (5.0.x Branch)

The mongodb package is available as

mongodb50-5.0.2 from the above search. Install it using this command:

pkg install <meta charset="utf-8">mongodb50-5.0.2

Confirm the installed package using this command

# pkg info mongodb50-5.0.2
mongodb50-5.0.2
Name           : mongodb50
Version        : 5.0.2
Installed on   : Mon Nov 22 19:11:17 2021 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.2:::::freebsd13:x64
	repo_type      : binary
	repository     : FreeBSD
Flat size      : 164MiB
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/

You can check the version of MongoDB installed using the following command:

# mongo -version
MongoDB shell version v5.0.2
Build Info: {
    "version": "5.0.2",
    "gitVersion": "6d9ec525e78465dcecadcff99cce953d380fedc8",
    "openSSLVersion": "OpenSSL 1.1.1k-freebsd  24 Aug 2021",
    "modules": [],
    "allocator": "system",
    "environment": {
        "distarch": "x86_64",
        "target_arch": "x86_64"
    }
}

# 4. Starting and Enabling MongoDB Service

Once the service has been installed, it will not be started by default. Start the mongodb service using this command:

service mongod onestart

Confirm that the service is up and running by checking its status:

# service mongod onestatus
mongod is running as pid 28399.

Our mongodb service is up and running. Let us enable it to start on boot using this command:

sync mongod_enable="YES"

# 5. Connecting to Mongodb and Executing some test commands

Use the mongo command to connect to the mongo shell.

# mongo --port 27017
MongoDB shell version v5.0.2
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b01407dd-7238-4655-8e8d-d5579599160e") }
MongoDB server version: 5.0.2
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
---
The server generated these startup warnings when booting:
        2021-11-22T19:14:56.724+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> db.version()
5.0.2
>

Let us execute some test commands to confirm its proper workings:

> use citizix_db;
switched to db citizix_db
>
> db.users.save({
...     name: "etowett",
...     location: "Arctic Vault"
... });
WriteResult({ "nInserted" : 1 })
> db.users.find();
{ "_id" : ObjectId("619bece64195da4aea51efa5"), "name" : "etowett", "location" : "Arctic Vault" }
>

# 6. Uninstall MongoDB Community Edition

To completely remove MongoDB from a system, you must remove the MongoDB applications themselves, the configuration files, and any directories containing data and logs.

This process will completely remove MongoDB, its configuration, and all databases. This process is not reversible, so ensure that all of your configuration and data is backed up before proceeding.

The following section guides you through the necessary steps.

# Stop MongoDB

Stop the mongod process by issuing the following command:

service mongod stop

# Remove Packages.

Remove any MongoDB packages that you had previously installed.

pkg delete <meta charset="utf-8">mongodb50-5.0.2

# Remove Data Directories.

Remove MongoDB databases and log files.

sudo rm -r /var/log/mongodbsudo rm -r /var/lib/mongo

# Conclusion

In this guide, we managed to Install and do some basic operations with MongoDB on our FreeBSD.

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy