MongoDB article thumbnail image.

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 19.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 19.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 19.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:

● 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:

● 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:


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:

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:

admin    0.078GB
local    0.078GB

To create a database, use this command (make sure you use your database name):

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:{name: "toaster"})

We can also retrieve all the items in the products collection using this command:


The output will look similar to this:

{ "_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:

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.

How To Uninstall MongoDB

In the future, you may need to uninstall MongoDB from your Ubuntu 19.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.


In this article, we covered how to install MongoDB on a Ubuntu 19.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!

About the author

Hi, I'm Nick Major. I'm a software developer from Wisconsin, United States.

I create free and premium courses, have published hundreds of informational coding articles, and am currently a Frontend Software Engineer at RoundingWell.

You can read more about me here.

Picture of Nick Major.