How to install, secure and import data into MongoDB.

MongoDB is a document-oriented database. In this we install v4.2.6, configure two users and import data in a bson format.

MongoDB is a document-oriented NoSQL database. It has become popular in web-apps. It provides a JSON-like format with optional schemas.

For this we will use the Ubuntu operating system. The ubuntu version is 18.04 (Bionic).

This how to uses command for the Ubuntu terminal.

In this how to we explain how to:

  • Install MongoDB Community Edition and Git.
  • Add two users.
  • Update the config file to enable authorisation.
  • Restore existing data in a BSON format.

Step 1 - Installing MongoDB.

For this we will not use the default MongoDB install in the Ubuntu respository. Instead we will install the latest stable version managed by Mongo Inc. In this case that is version v4.2.6.

First we need to import the public GPG Key for MongoDB. After you run the below command you should see a response "ok".

wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

You may receive an error showing gnupg isn't installed. To overcome this run the following commands.

sudo apt-get install gnupg
sudo apt-get update
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

Next we need to add the MongoDB to our repository source list. To do this run the following command in your terminal.

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

Next update the local package database.

sudo apt-get update

Now we can install the Mongo Inc managed versions of MongoDB using apt.

sudo apt-get install -y mongodb-org

We can now start the MongoDB daemon with the following command.

sudo systemctl start mongod

If you receive an error, you will need to restart the daemon. To do that use the following command in your terminal.

sudo systemctl daemon-reload

Your MongoDB installation is now complete. You can test the status of your MongoDB install by using:

sudo systemctl status mongod

Step 2 - Installing Git

You may already have git installed. In which case you can skip this step.

As we have already updated are Ubuntu operating systems. We can use the default package for git. In your terminal type:

sudo apt install git

To install git.

Step 3 - Adding two users to the MongoDB install.

Now we can add two users to our MongoDB install. To do this type "mongo" into the terminal. This will open a command line for MongoDB.

We will need to add our users to the "admin" table. To do this type the following command into the MongoDB command line.

use admin

Then press enter. MongoDB installs three databases when installed. To change database in the MongoDB command line you type "use [database]". You display databases with the command "show dbs".

First we will add an Admin user. To do this use the following command.

db.createUser({user:'admin', pwd:'AdminSecurePwd', roles:['userAdminAnyDatabase']});

The output from this command will be.

Successfully added user: { "user" : "admin", "roles" : [ "userAdminAnyDatabase" ] }

Now we can use the authorisation command to authenticate our admin user. To do that type:

db.auth('admin', 'AdminSecurePwd');

This will have a response of "1".

Now we can add a user that will authenticate as a Client user. This user will act as a connector user for G8CC in the next tutorial. To add this user type:

db.createUser({user:'g8cc',pwd: 'G8CCSecurePWD', roles:[{db:'customers', role:'readWrite'}]});

This gives the G8CC user read and write access to the customers database. You can now exit the MongoDB command line tool by typing "exit".

Step 3 - Enabling authorisation in MongoDB.

We will use nano to update the MongoDB configuration. To do that type:

sudo nano /etc/mongod.conf

In the conf file you will have a security tag. Following this tag you may have [authorization: "disabled"]. If you have the authorization line replace disabled with enabled. If you don't have the authorization line add [authorization: "enabled"] below the security tag. Don't include the [ ] (Square brackets).

Click ctrl-y to exit nano and type "y" to save. We must now restart the mongodb daemon for the changes to take place. To do that use the following command in your terminal.

sudo systemctl restart mongod

Step 4 - Download and import bson demo data.

To download the data type the following in your terminal.

git clone https://github.com/Ciges/MongoDB_Sample_Databases.git

This will create a new directory. Use the following command to change to the directory where the BSON documents are stored.

cd MongoDB_Sample_Databases/mongodb

You can type ls to view a list of files. To import the employees data type the following command in your terminal.

mongorestore --username g8cc --password G8CCSecurePWD -d customers employees

This restores the employees BSON data dump. You've now installed and configured a secure MongoDB database to test.

Next, you can now connect G8CC as a data interface and sharing layer. G8CC provides a simple low code framework. It allows you to securely access, share and extend data. It achieves this within and across traditional borders.

You can see how simple it is to connect G8CC to MongoDB here.

Securely share and extend data from MongoDB with G8CC.

Contact us here today to reduce IT delivery costs.

With G8CC you can:

Deploy anywhere.

Deploy our Application Platform on AWS, Microsoft Azure, Google Cloud Platform (GCP), Kubernetes, private cloud.

Access Management.

G8CC's flexibility allows you to manage your data building trust. That empowers you to add value to your data across traditional borders.

Save Money.

Remove duplicated effort and wastage. Harness and build on the value of digital and data, reducing delivery and ongoing costs.

Get started by: