Introduction

MongoDB is a free and open-source NoSQL database. It is one of the most popular databases used in web applications today because it offers high performance, scalability, and lots of flexibility in database schema design. In this tutorial, you will learn how to install and run MongoDB on FreeBSD 10.1.

Prerequisites

To follow this tutorial, you need to have:

A FreeBSD 10.1 server which is accessible over SSH

A user with root privileges; the default freebsd user on DigitalOcean is fine

Step 1 — Installing the Package Management Tool

FreeBSD uses a tool called pkg to manage binary packages. Update the repository catalogue by typing:

sudo pkg update -f

Step 2 — Installing MongoDB

Now that pkg is ready to be used, install MongoDB and all its dependencies by running the following command:

sudo pkg install mongodb

You might be prompted to update pkg first before installing mongodb. If prompted, press Y. The installation of MongoDB will automatically start after pkg is updated.

You will be shown a list of packages that are going to be installed and asked to confirm if you want to proceed. Press Y to begin the installation.

Step 3 — Allowing MongoDB to Start Automatically At Boot Time

To start MongoDB automatically at boot time, you need to edit the /etc/rc.conf file. You will need to use sudo because root privileges are required. If you want to use nano, you will need to install it with the following command:

sudo pkg install nano

You might have to log out and log back in to get nano added to your default path.

Otherwise, you can use vi:

sudo vi /etc/rc.conf

Add the following line at the end of the file to allow MongoDB’s primary daemon to start automatically when your FreeBSD server is booting up:

mongod_enable="YES"

Step 4 — Starting MongoDB

You can now reboot your server to start MongoDB automatically. If you don’t want to do that, you can start MongoDB manually using the service command.

sudo service mongod start

MongoDB is up and running.

Step 5 — Configuring MongoDB

Optionally, you can add configuration details to /usr/local/etc/mongodb.conf to customize MongoDB.

For example, to run on port 9000 instead of port 27017 (the default port), add the following to mongodb.conf:

/usr/local/etc/mongodb.conf

net:
port: 9000

Every time you modify mongodb.conf, you must restart MongoDB to enable the changes:

Step 6 — Verifying the Installation

If you changed the configuration to run MongoDB on a different port, run the following instead:

sudo mongo –port <your-port-number>

If everything went well, you will see the following output:

MongoDB shell version: 2.6.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
>

On a 32-bit FreeBSD server, you will also see the following warnings:

Server has startup warnings:
2015-05-13T19:01:49.548+0100 [initandlisten]
2015-05-13T19:01:49.548+0100 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2015-05-13T19:01:49.548+0100 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2015-05-13T19:01:49.548+0100 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
2015-05-13T19:01:49.548+0100 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2015-05-13T19:01:49.548+0100 [initandlisten]

Though these warnings can be ignored in a development or test environment, it is recommended that you run production instances of MongoDB only on 64-bit servers

Conclusion

In this short tutorial, you learned how to use the package management tool to install MongoDB on your FreeBSD 10.1 server. To know more about what you can do with your instance of MongoDB, refer to the MongoDB 2.6 Manual.