Install Mongodb in AWS Ubuntu Server 22.04
Published: 2022-11-26 09:35:19

_Login to your AWS Ubuntu Server
Install the dependencies_
$ sudo apt update
$ sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

_Download and add the MongoDB GPG key with the following command_
$ sudo wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

_Create a list for MongoDB_
$ 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

_Update the local package database_
$ sudo apt-get update

$ echo "deb http://security.ubuntu.com/ubuntu focal-security main" | sudo tee /etc/apt/sources.list.d/focal-security.list
$ sudo apt-get update 
$ sudo apt-get install libssl1.1

_Now Install the MongoDB with the following command_
$ sudo apt-get install -y mongodb-org

_Start the MongoDB service and enable it to start automatically after rebooting the system._
$ sudo systemctl start mongod
$ sudo systemctl enable mongod

_Now, check the status of the MongoDB service_
$ sudo systemctl status mongod

_To verify whether the installation has completed successfully by running the following command._
$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'

_Output:_
  ubuntu@ip-172-31-7-45:~$ mongo --eval 'db.runCommand({ connectionStatus: 1 })'
MongoDB shell version v5.0.13
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("66cf060a-dbd1-419f-9317-980163b0b34f") }
MongoDB server version: 5.0.13
{
        "authInfo" : {
                "authenticatedUsers" : [ ],
                "authenticatedUserRoles" : [ ]
        },
        "ok" : 1
}


_Use this command, to access the MongoDB shell._
$ mongo

_Output:_
ubuntu@ip-172-31-7-45:~$ mongo
MongoDB shell version v5.0.13
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("61a1f8c5-2e86-449a-a7ec-792bcd1d5b93") }
MongoDB server version: 5.0.13
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
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
---
The server generated these startup warnings when booting:
        2022-11-15T08:01:08.164+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2022-11-15T08:01:08.921+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

_Connect to the admin database._
$ use admin

_Output:_
    switched to db admin

_Run the following command to create a new user and set the password for the user._
$ db.createUser(
   {
     user: "mongoAdmin",
     pwd: "KAb3747d",
     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
   }
  )

_Output:_
   Successfully added user: {
        "user" : "mongoAdmin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ]
}

Exit the mongo shell.
$ quit()

To test the changes, access the mongo shell using the created administrative user.
$ mongo -u mongoAdmin -p --authenticationDatabase admin

_Output: _  
    ubuntu@ip-172-31-7-45:~$ mongo -u mongoAdmin -p --authenticationDatabase admin
    MongoDB shell version v5.0.13
    Enter password:

_Switch to the admin database._
$ use admin

_List the users and see if you can list the created user._
$ show users

_Output:_
    {
        "_id" : "admin.mongoAdmin",
        "userId" : UUID("ef767177-be14-488f-8eb9-947941a16d0f"),
        "user" : "mongoAdmin",
        "db" : "admin",
        "roles" : [
                {
                        "role" : "userAdminAnyDatabase",
                        "db" : "admin"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}