Install & Configure MongoDB on Ubuntu 16.04

Introduction
MongoDB is a general-purpose, document-based, and non-SQL database intended for building modern applications in the cloud era. It's completely free to use and built with open source software.
In this tutorial, we'll help you get MongoDB installed on a Ubuntu 16.04 machine, create a new database user, and add some data to it using the MongoDB shell. Also, we'll show you how to uninstall MongoDB if you ever need to.
Before you can work through this tutorial, make sure you have a Ubuntu 16.04 server or local machine to work with.
Let's get started!
Table of Contents
Install MongoDB
First things first, let's get MongoDB installed on your machinge. Since you're using Ubuntu 16.04, we can install the MongoDB software using the default repositories that are included with your operating system.
Open a terminal and execute this command (using apt) to make sure the package lists on your system are up-to-date:
$ sudo apt update
When that has completed, you can install MongoDB by executing this command:
$ sudo apt install -y mongodb
Awesome, MongoDB is now installed on your machine.
Verify the Database is Running
MongoDB is now installed on your machine and should be running automatically in the background using systemctl.
You can use this command to check on its status:
$ sudo systemctl status mongodb
If MongoDB is running and working correctly, you should see an output similar to this:
Output
 
mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-10-14 12:39:05 CDT; 3 days ago
     Docs: man:mongod(1)
 Main PID: 871 (mongod)
   CGroup: /system.slice/mongodb.service
           └─871 /usr/bin/mongod --config /etc/mongodb.conf
If you don't see that success output, you can start the systemctl service yourself:
$ sudo systemctl start mongodb
When you run the sudo systemctl status mongodb command again, you should see that MongoDB is running now.
The last step is to enable MongoDB to automatically start whenever your system starts. You can do that with this command:
$ sudo systemctl enable mongodb
The MongoDB service is now up and running using systemctl.
For your reference, you can stop the service with the sudo systemctl stop mongodb command.
How to Manage the MongoDB Service
In this section, we'll give you some commands that'll help you manage the systemctl MongoDB service.
This command will give you the current status of the systemctl service:
$ sudo systemctl status mongodb
And will output something similar to this:
Output
 
mongodb.service - An object/document-oriented database
   Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2019-10-14 12:39:05 CDT; 3 days ago
     Docs: man:mongod(1)
 Main PID: 871 (mongod)
   CGroup: /system.slice/mongodb.service
           └─871 /usr/bin/mongod --config /etc/mongodb.conf
This command will stop the service completely:
$ sudo systemctl stop mongodb
To start the service up again, use this command:
$ sudo systemctl start mongodb
The systemctl restart command will first stop the service and then start it again (it will also start MongoDB if it wasn't already running):
$ sudo systemctl restart mongodb
To stop the MongoDB service from running automatically on system start, execute this command:
$ sudo systemctl disable mongodb
And if you do want to have the MongoDB service start when your system does, use this command:
$ sudo systemctl enable mongodb
Those six systemctl commands will make the management of your MongoDB database an easier task.
Create a Root Database User
Since your MongoDB database is installed and ready to be used, we can create a database root account with a unique username and password you can use when working with your MongoDB database. This will give you root access to do whatever you need to in your MongoDB instance.
First, open the MongoDB shell:
$ mongo
And then switch over to the database admin with this command inside the MongoDB shell:
> use admin
Create a new root user with this command (replace the yellow text with the credentials you wish to use):
> db.createUser({user:"admin123", pwd:"password", roles:[{role:"root", db:"admin"}]})
For those changes to go into effect, you need to restart the MongoDB service.
First, exit the MongoDB shell. You can do this with the exit command in the MongoDB shell or by typing CTRL+C).
Then, restart the MongoDB service with this command:
$ sudo systemctl restart mongodb
Your new user is now created and the changes should be in effect. In the next section, we'll connect to the database using that user and go over how to create a new database.
Connect to MongoDB & Create a New Database
To use the new user we created in the previous step, execute this command in your terminal (not in the mongo shell):
$ mongo -u admin123 -p password --authenticationDatabase admin
That command will open a mongo shell as the authenticated user you specified.
Now that you are connected using your user, we can create a new database and add some data to it.
First, use the show dbs command to list out all the databases that have been created on your MongoDB instance:
> show dbs
Only the admin and local database will show up since we haven't created a database yet:
Output
 
> show dbs
admin    0.078GB
local    0.078GB
To create a database, use this command (replace the yellow text with whatever you want to name your database):
> use yourNewDbName
This command tells MongoDB to use a database with that name. The database won't be created until you add data to it. So, let's do that next.
The command below will create a collection named products and add a document to it with the name: "toaster" value:
> db.products.save({name: "toaster"})
We can also retrieve all the items in the products collection using this command:
> db.products.find()
The output will look similar to this:
Output
 
{ "_id" : ObjectId("5da949af258b2baeb57283c0"), "name" : "toaster" }
And since your new database has data in it now, it should now show up when you use the show dbs command:
Output
 
> show dbs
admin            0.078GB
local            0.078GB
yourNewDbName    0.078GB
There you have it! You've created a new database and stored some data in it.
Uninstall MongoDB
In the future, you may need to uninstall MongoDB from your Ubuntu 16.04 machine. You can do it with a couple of commands.
First, make sure you stop the systemctl service:
$ sudo service mongodb stop
With the MongoDB service stopped, you can use the apt purge command to completely remove MongoDB from your system:
$ sudo apt-get purge mongodb*
MongoDB also creates log files on your system and you'll need an additional command to remove those:
$ sudo rm -r /var/log/mongodb
$ sudo rm -r /var/lib/mongodb
After those commands, MongoDB will be completely removed from your system.
Conclusion
In this article, we covered how to install MongoDB on a Ubuntu 16.04 machine, created a root user with a username and password, created a new database, added some data to it, and showed you how to uninstall MongoDB if the need was to ever arise.
After working through this article, you should now have a great starting point for working with MongoDB.
Thanks for reading and happy coding!