how to install mongodb in ubuntu

How to Install MongoDB in Ubuntu 18.04/20.04

MongoDB is a popular open-source, NoSQL database used by many websites and applications all over the world. In this article, we will look at how to install MongoDB in Ubuntu. You can use these steps for other Debian systems also.


How to Install MongoDB in Ubuntu 18.04/20.04

Here are the steps to install MongoDB in Ubuntu.


1. Add MongoDB repository

Open terminal and run the following command to add the GPG key for MongoDB.

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

Next, add MongoDB repository to source list

$ sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

If you want an older MongoDB repository replace 4.0 with a lower number.

Also read : How to Download Files using cURL


2. Install MongoDB

Update package list and install MongoDB

$ sudo apt update
$ sudo apt install mongodb-org

This will install the following on your system

  • mongodb-org-server – MongoDB server
  • mongodb-org-mongos – MongoDB daemon
  • mongodb-org-shell – MongoDB shell to run commands and run administrative tasks.
  • mongodb-org-tools – MongoDB tools to perform additional tasks such as import/export of data

Also read : How to Set or Change Time Zone in Ubuntu


3. Start & Enable MongoDB

Run the following commands to enable and start MongoDB.

$ sudo systemctl start mongodb
$ sudo systemctl enable mongodb

You can run the following command to verify if MongoDB is running. It basically connects to your database server and prints connection status.

$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'

Here is a sample output

MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.10
{
  "authInfo" : {
    "authenticatedUsers" : [ ],
    "authenticatedUserRoles" : [ ]
  },
  "ok" : 1
}

The line “ok”:1 indicates that MongoDB server is running properly.

Also read : How to Implement SSL/TLS in Apache Tomcat


4. Manage MongoDB

Here are the common commands to manage your MongoDB service.

To start MongoDB,

$ sudo systemctl start mongodb

To stop MongoDB,

$ sudo systemctl stop mongodb

To restart MongoDB,

$ sudo systemctl restart mongodb

To enable MongoDB,

$ sudo systemctl enable mongodb

To disable MongoDB,

$ sudo systemctl disable mongodb

Also read : How to Extract Data from JSON in Python


5. Create Root User

By default, user authorization is disabled in MongoDB. So we need to enable it first before we create an Admin user.

Open MongoDB configuration file in a text editor.

$ sudo vi /etc/mongo.conf

Uncomment the following lines to enable user authentication.

security:
   authorization: enabled

Save and exit the file.

Restart MongoDB to apply changes.

$ sudo systemctl restart mongodb

Next, open MongoDB shell

$ sudo mongodb

Connect to admin database

> use admin

Also read : How to Install Laravel with NGINX

Run the following command to create a root user (admin) for your MongoDB database. Replace root and strong_password@123 below with your database user’s username and password.

> db.createUser({user:"root", pwd:"strong_password@123", roles:[{role:"userAdminAnyDatabase", db:"admin"}]})

You will see the following output

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

Quit the shell

> quit()

You can test the newly created user with the following lines

$ mongo -u root -p --authenticationDatabase admin

After you enter the password, you can proceed to working with MongoDB.

That’s it. In this article, we have seen how to install and configure MongoDB in Ubuntu.

Also read : How to Install CouchDB in Ubuntu


Leave a Reply

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