How to Install MongoDB on Ubuntu Linux

|

|

This post provides a detailed guide for students and new users on how to install and configure MongoDB, a NoSQL, high-performance, schema-less open-source database server on Ubuntu Linux. The tutorial covers adding the MongoDB package repository, installing MongoDB Community Edition, configuring the database for authentication, and creating a MongoDB administrator account.

This post shows students and new users how to install the MongoDB database server on Ubuntu Linux. MongoDB, a free open source, NoSQL High-performance, schema-free document-oriented database, can create powerful websites and applications.

MongoDB uses a flexible JSON-like documents data store where fields are not unique and can vary from one document to another. It also doesn’t require a predefined schema; the data structure can be changed anytime during data modifications.

The steps below show you how to install and configure MongoDB Community Edition on Ubuntu Linux. The installation is pretty straightforward.

MongoDB packages are included in Ubuntu default repositories. However, the versions in Ubuntu repositories aren’t the latest. To install the latest, you must install the MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.

Also, for students and new users learning Linux, Ubuntu Linux is the easiest place to start learning. Ubuntu is the modern, open-source Linux operating system for desktops, servers, and other devices.

To start installing MongoDB on Ubuntu Linux, follow the steps below.

How to add MongoDB repository on Ubuntu Linux

As mentioned above, one can simply run the apt-get install command on Ubuntu to download and install MongoDB. However, the versions in Ubuntu repositories are typically not the latest.

We’ll have to add the MongoDB repository to Ubuntu Linux to install the latest. But first, run the commands below to install the required packages and dependencies.

sudo apt update
sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Then import the MongoDB repository key and create a repository file using the commands below.

At the time of this writing, the latest version of MongoDB is version 5.0. You can visit the link below for details on the future version number, then replace the commands below.

MongoDB repositories

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

Next, run the commands below to create a repository file for MongoDB version 5.0.

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

Also, the repository above is for Ubuntu 20.04 (focal). If you’re using another version of Ubuntu Linux, replace focal with that version code name. Ubuntu 18.04 will be (bionic).

How to install MongoDB on Ubuntu Linux

Once the repository is created and enabled, run the commands below to update the Ubuntu package index and install MongoDB.

sudo apt update
sudo apt install mongodb-org

The commands above will install the following packages along with MongoDB core.

The following packages will be installed on your system:

  • mongodb-org-server – MongoDB server
  • mongodb-org-mongos – MongoDB daemon
  • mongodb-org-shell – The mongo shell, an interactive JavaScript interface to MongoDB.
  • mongodb-org-tools – MongoDB tools for importing and exporting data

After installing MongoDB, the commands below can stop, start and enable MongoDB to automatically startup when the systems boot up.

sudo systemctl stop mongod
sudo systemctl start mongod
sudo systemctl enable mongod

By default, MongoDB listens on port 27017. After installing, the local server should be able to communicate with MongoDB. To verify whether MongoDB is running and active, run the commands below:

sudo systemctl status mongod

You should see something like the lines below:

mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Thu 2020-05-21 12:37:32 CDT; 52s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 3409 (mongod)
     Memory: 72.7M
     CGroup: /system.slice/mongod.service
             └─3409 /usr/bin/mongod --config /etc/mongod.conf

May 21 12:37:32 ubuntu2004 systemd[1]: Started MongoDB Database Server.

To connect to the MongoDB shell, run the commands below:

mongo --host 127.0.0.1:27017

You should see something like the lines below:

ongoDB shell version v4.2.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1b88c27f-e477-4a29-b562-835ee56591b5") }
MongoDB server version: 4.2.6
Welcome to the MongoDB shell.
For interactive help, type "help".

Configuring MongoDB on Ubuntu Linux

The MongoDB configuration file is named mongod.conf and is located in the /etc directory. The file is in YAML format.

The default configurations are sufficient for most environments. However, you must modify its configuration file to enable authentication and other settings.

For example, you can enable authentication by changing the line in the configuration file to match the one below.

Open the configuration file as root:

sudo nano /etc/mongod.conf

Then change the line below to enable authentication.

security:
  authorization: enabled

Save and restart MongoDB.

How to add MongoDB administrator

As mentioned above, MongoDB cannot authenticate, and the admin account isn’t activated by default. It may be required to secure your server and enable user authentication in a production environment.

If you want to enable authentication, run the commands to create a new admin user after you’ve logged into the MongoDB server.

Access the MongoDB shell by typing the commands below:

mongo

Type the commands below from the shell to connect to the admin database.

> use admin

Then run the commands below to create a new admin user called admin and password. Replace strong_password_here with password you want to use.

db.createUser(
  {
    user: "admin",
    pwd: "strong_password_here",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

You should see a successful message that the admin user was created.

Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}

Exit and continue below to enable MongoDB login authentication.

Now to log in with the admin account, run the commands below.

mongo -u admin -p --authenticationDatabase admin

That should do it!

Conclusion:

This post showed you how to install and configure MongoDB on Ubuntu Linux. Please use the comment form below if you find any errors above or have something to add.

Like this:



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.