How to Install & configure MongoDB on Ubuntu 20.04.

MongoDB is a free & open-source document-oriented database program. MongoDB belongs to NoSQL database. It stores data in JSON format. We can easily add or change fields.

Install MongoDB on Ubuntu

Update the system.

apt-get update

Install the required packages.

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

Import & Add the Repo & key.

wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
&
add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu focal/
mongodb-org/4.4 multiverse'

Install the mongodb.

apt-get install mongodb-org

Start & Enable MongoDB service.

systemctl start mongod
systemctl enable mongod

Check the Mongodb.

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

Here is the command output.

MongoDB shell version v4.4.7
connecting to:mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("23a9d45d-8083-41d8-ba73-2952e0b26658") }
MongoDB server version: 4.4.7
{
	"authInfo" : {
		"authenticatedUsers" : [ ],
		"authenticatedUserRoles" : [ ]
	},
	"ok" : 1
}

Configure the MongoDB.

vim /etc/mongod.conf

Add the following lines:

security:
  authorization: enabled

Restart the MongoDB Service.

systemctl restart mongod

Login to MongoDB.

mongo

Here is the command output.

MongoDB shell version v4.4.7
connecting to:mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("05004366-fdd7-4e59-a0ea-e529326c5843") }
MongoDB server version: 4.4.7
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
	https://community.mongodb.com
> 

Connect with the admin database:

use admin

Here is the command output.

switched to db admin

Create MongoDB User.

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

Here is the command output.

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

Exit the mongo shell.

quit()

Login to new created mongo user.

mongo -u user-name -p --authenticationDatabase admin

Here is the command output.

  • Provide the password.
MongoDB shell version v4.4.7
Enter password:
connecting to:mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapi
ServiceName=mongodb
Implicit session: session { "id" : UUID("90158e8d-1dac-497d-9839-458b83b6cdb3") }
MongoDB server version: 4.4.7
> 

Connect with the admin database:

use admin

Here is the command output.

switched to db admin

Check the information about the new created user:

show users

Here is the command output.

{
	"_id" : "admin.user-name",
	"userId" : UUID("d5c71a08-626c-4fa0-9c77-7d33b8791e41"),
	"user" : "user-name",
	"db" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	],
	"mechanisms" : [
		"SCRAM-SHA-1",
		"SCRAM-SHA-256"
	]
}

 

Leave a Reply