This tutorial will show you how to install and secure MongoDB on an Ubuntu or Debian. This guide will also explain on some basic features and functions of the MongoDB database.
Prerequisites
To follow this guide, you will need one (physical or virtual) machine installed with Ubuntu or Debian having sudo non-root user privileges.
Set Timezone
You can set correct timezone on your Ubuntu or Debian using the below command:
Set Hostname
You can set hostname on your Ubuntu or Debian using the below command:
Adding MongoDB Source
You should check the available MongoDB version on https://repo.mongodb.org/apt/ubuntu. For this guide, we will install and use MongoDB version 4.2.
Install MongoDB
Type below command to install MongoDB on your Ubuntu or Debian:
Secure MongoDB
Edit mongod.conf file with any of your prefered editor:
Locate the security section, comment out by removing #, then add the authorization: enabled string like below:
Make sure you add double blank space in the beginning of authorization string.
Save and close the editor.
The authorization option enables role-based access control for your databases. If no value is specified, any user will have the ability to modify any database. We'll explain it to you how to create database users and set their permissions later in this guide.
Start or restart MongoDB to apply changes:
Confirm that the MongoDB is up and running:
You will see output like below:
Create an Administrative user
We will create a administrative privileges user to be used on the database as we have already enabled role-based access control in the (Secure MongoDB) section.
Type below command to open the mongo shell:
You will see MongoDB prompt like below:
Type below to switch to admin database:
Type below command to create an administrative privileged user with the ability to create other users on any database.
For this guide, we will create a user called administrator:
You will see output similar to the following:
Now exit from the mongo shell with below command:
Next, connect to your MongoDB with the newly created user like below:
The -u, -p, and --authenticationDatabase options in the above command are required in order to authenticate connections to the shell. Without authentication, the MongoDB shell can be accessed but will not allow connections to databases.
You will login to MongoDB shell like below:
As the administrator user, create a new database to store regular user data for authentication. For this guide, we will create a database called testDB:
You will see output similar to the following:
Now exit from the mongo shell with below command:
Managing the Data and Collections
This section will explain a few basic features, but we encourage you to do further research based on your specific use case.
Access the MongoDB shell using the testuser1 we created in earlier step:
You will login to MongoDB shell:
Type below to switch to testDB:
Type below to create a new collection, for example testDBCollection:
Next, create sample data entry into the testDB database. MongoDB accepts input as documents in the form of JSON objects such as the below. The a and b variables are used to simplify entry; objects can be inserted directly via functions as well.
Note that documents inserted into a collection need not have the same schema, which is one of many benefits of using a NoSQL database. Insert the data into testDBCollection, using the insert method:
The output will show the number of objects successfully written to the current working database:
Confirm that the testDBCollection was properly created:
The output will list collections within the current working database like below:
View unfiltered data in the testDBCollection using the find method like below:
If the query passed, you will see the output similar to the following:
You may notice the objects we entered are preceded by _id keys and ObjectId values. These are unique indexes generated by MongoDB when an _id value is not explicitly defined. ObjectId values can be used as primary keys when entering queries, although for ease of use, you may wish to create your own index as you would with any other database system.
The find method can also be used to search for a specific document or field by entering a search term parameter (in the form of an object) rather than leaving it empty.
For example:
This returns a list of documents containing the {"name" : "Muhammad Anwar"} object.
Wrapping up
We hope this guide was helpful to install and secure MongoDB on your Ubuntu or Debian server. If you wish to learn how to set up a highly available fault-tolerant MongoDB Sharded Cluster for your production use, follow this guide.
this is not working
ReplyDeleteHi,
ReplyDeleteGood tutorial. After "Add the MongoDB repository to your sources.list.d directory" step you shroud run "sudo apt-get update".
Thanks for pointing out correction.
Delete