Install MongoDB

$ wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
$ sudo systemctl start mongod

Add admin user

$ mongo

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

Set enable authorization

$ sudo vi /etc/mongod.conf 


 security:
   authorization: enabled

Connect to MongoDB

$ mongo -u "admin" -p --authenticationDatabase "admin"
 
   or
 
$ mongo
> use admin
> db.auth("admin", "password" )