Contents
I had to search a lot before figuring out a proper way to install and configure a production ready standalone MongoDB installation in AWS EC2 instance. I am sharing my learnings and hope it is useful for others.
Install MongoDB Community Edition
Prior to installation; please refer deploy ec2 in a private subnet & securely communicate with internet to properply setup your ec2 instance.
Create a /etc/yum.repos.d/mongodb-org-4.4.repo
file so that you can install MongoDB directly using yum:
|
|
To install the latest stable version of MongoDB, issue the following command:
$ sudo yum install -y mongodb-org
Issue below command to verify whether MongoDB is installed properly
|
|
Configure EC2 Instance
In my opinion, it will be good to add 2 additional EBS volumes apart from default root volume for MongoDB
- Data volume: Size: X GB (according to your size estimation)
- Log volume: Recommended size is 1/10th of data volume
Note:- A separate volume for journal
can also be attached by creating a symbolic link to point the journal directory to the attached volume as
MongoDB creates a subdirectory named journal under the dbPath directory
Size of EBS volumes can be changed later also.
Read detailed steps for Make an Amazon EBS volume available for use.. Mount each volume as below:
|
|
Add below content using command $ sudo vi /etc/fstab
|
|
I would also recommend to add swap space of atleast 4GB. Run below commands to create the swap space.
|
|
Add below contents:
|
|
Run command $ sudo swapon --show
to verify swap is active.
|
|
Configure ulimit
settings
Update the limits.conf
file with recommended values:
$ sudo vi /etc/security/limits.conf
Add below content:
|
|
This step might require to reboot the instance.
Verify the values by issuing the command $ ulimit -a
Disable Transparent Huge Pages (THP)
I would recommend to follow steps mentioned at MongoDB official documentation.
Configure Log Rotation
Linux logrotate utility allows automatic rotation, compression and removal of log files. Create a file /etc/logrotate.d/mongodb
and add below contents:
|
|
Then create a cron job to run hourly (run as per your need)
$ crontab -e
Add below content:
0 * * * * sudo /usr/sbin/logrotate /etc/logrotate.d/mongodb
MongoDB Configuration
Update MongoDB configuration:
$ sudo vi /etc/mongod.conf
Change value of port other than the default 27017
and also update the security group settings in aws accordingly.
|
|
Update path of pid
file.
|
|
Also, update data and log paths:
|
|
MongoDB User
Start mongodb user by using command $ mongod --config /etc/mongod.conf
Connect to mongodb by using command $ mongo --port <configured port value>
Create a root user:
|
|
I would strongly recommend to go through the section Built-In Roles
Update /etc/mongod.conf
file with
|
|
Restart the mongod process and try to connect using command:
$ mongo --port <port value> -u adminUser --authenticationDatabase admin
Conclusion
I have tried to document all the steps based on my learnings during a standalone installation. I will write another blog for MongoDB Installation with Replication setup. I hope you enjoyed reading this and find it helpful. I sincerely request for your feedback. You can follow me on twitter @lifeClicks25.