This is an English translation of a Japanese blog. Some content may not be fully translated.
AWS

Installing MongoDB (5.x) on Amazon Linux

Checking the Linux Distribution

grep ^NAME  /etc/*release
[ec2-user@bastin ~]$ grep ^NAME  /etc/*release
/etc/os-release:NAME="Amazon Linux"

Installation Preparation

Create mongodb-org-5.0.repo

sudo vi /etc/yum.repos.d/mongodb-org-5.0.repo
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc

Install via yum

The latest version of MongoDB will be installed.

sudo yum install -y mongodb-org
mongo -version

Starting MongoDB

sudo systemctl start mongod

Checking MongoDB Status

sudo systemctl status mongod

Restarting MongoDB

sudo systemctl restart mongod

Starting the Process Automatically on OS Boot

sudo systemctl enable mongod

Version Check

[ec2-user@bastin ~]$ mongo
MongoDB shell version v3.6.23
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c2030c87-8f92-4fdd-8b93-6265be9fdc28") }
MongoDB server version: 5.0.5
WARNING: shell and server versions do not match
Server has startup warnings:
{"t":{"$date":"2021-12-30T21:38:38.651+09:00"},"s":"W",  "c":"CONTROL",  "id":22120,   "ctx":"initandlisten","msg":"Access control is not enabled for the database. Read and write access to data and configuration is unrestricted","tags":["startupWarnings"]}
> db.version()
5.0.5

Inserting and Verifying Data

> db.stats()
{
	"db" : "test",
	"collections" : 0,
	"views" : 0,
	"objects" : 0,
	"avgObjSize" : 0,
	"dataSize" : 0,
	"storageSize" : 0,
	"totalSize" : 0,
	"indexes" : 0,
	"indexSize" : 0,
	"scaleFactor" : 1,
	"fileSize" : 0,
	"fsUsedSize" : 0,
	"fsTotalSize" : 0,
	"ok" : 1
}
>
> db.user.insert({name:'mr.a', age:10, gender:'m', hobbies:['programming']});
WriteResult({ "nInserted" : 1 })
> db.user.insert({name:'mr.b', age:20, gender:'m', hobbies:['vi']});
WriteResult({ "nInserted" : 1 })
> db.user.insert({name:'ms.c', age:30, gender:'f', hobbies:['programming', 'vi']});
WriteResult({ "nInserted" : 1 })
> db.user.insert({name:'ms.d', age:40, gender:'f', hobbies:['cooking']});
WriteResult({ "nInserted" : 1 })
>
> db.user.find()
{ "_id" : ObjectId("61cda8e0f9bdc7753af20459"), "name" : "mr.a", "age" : 10, "gender" : "m", "hobbies" : [ "programming" ] }
{ "_id" : ObjectId("61cda8e0f9bdc7753af2045a"), "name" : "mr.b", "age" : 20, "gender" : "m", "hobbies" : [ "vi" ] }
{ "_id" : ObjectId("61cda8e0f9bdc7753af2045b"), "name" : "ms.c", "age" : 30, "gender" : "f", "hobbies" : [ "programming", "vi" ] }
{ "_id" : ObjectId("61cda8e0f9bdc7753af2045c"), "name" : "ms.d", "age" : 40, "gender" : "f", "hobbies" : [ "cooking" ] }
> db.stats()
{
	"db" : "test",
	"collections" : 1,
	"views" : 0,
	"objects" : 4,
	"avgObjSize" : 96.25,
	"dataSize" : 385,
	"storageSize" : 20480,
	"freeStorageSize" : 0,
	"indexes" : 1,
	"indexSize" : 20480,
	"indexFreeStorageSize" : 0,
	"totalSize" : 40960,
	"totalFreeStorageSize" : 0,
	"scaleFactor" : 1,
	"fsUsedSize" : 206759358464,
	"fsTotalSize" : 214735761408,
	"ok" : 1
}

Reference

Install MongoDB Community Edition on Amazon Linux — MongoDB Manual https://docs.mongodb.com/manual/tutorial/install-mongodb-on-amazon/

Suggest an edit on GitHub