How to Install MongoDB on Ubuntu Linux

computer 1459654 640

This post shows students and new users steps to install MongoDB database server on Ubuntu Linux. MongoDB, a free open source, NoSQL High-performance, schema-free document-oriented database can be used to 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, and data structure can be changed anytime during data modifications.

The steps below will 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. In order to install the latest, you will have to install MongoDB package repository on Ubuntu Linux, and this tutorial will show you how.

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

To get started with 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.

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

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

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

At time of this writing, the latest version of MongoDB is version 5.0. You can visit the link below to get details on 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, make sure to 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 enable, run the commands below to update 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 be used to stop, start and enable MongoDB to automatically startup when the systems boots 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 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

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, if want to enable authentication and other settings, you will have to modify its configuration file.

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 MondoDB.

How to add MongoDB administrator

As mentioned above, authentication is not enabled for MongoDB and the admin account isn’t activated by default. In production environment, it may be required to secure your server and enable user authentication.

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

Access MongoDB shell by typing the commands below:

mongo

From the shell, type the commands below 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 logon authentication.

Now to login 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. If you find any error above or have something to add, please use the comment form below.

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.