DEV Community

Amruta Pardeshi
Amruta Pardeshi

Posted on

Install Mongodb in AWS Ubuntu Server 22.04

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()
Enter fullscreen mode Exit fullscreen mode

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"
]
}

Top comments (0)