Skip to content

Install MongoDB on AWS

Tong Wang edited this page Sep 8, 2017 · 2 revisions

Manual Deployment on a single EC2 Instance

The instruction is based on Amazon EC2.

Create an instance of Amazon Linux of type m4.xlarge (or m4.4xlarge), which by default is EBS-optimized, with individual PIOPS EBS volumes: 160GiB 5000 IOPS /dev/xvdb for data, 10 GiB 100 IOPS /dev/xvdc for log. We put data and journal in the same volume for ease of backup.

Now SSH into the instance, install MongoDB 2.6:

echo "[mongodb-org-2.6]
name=MongoDB 2.6 Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-2.6.asc" |
  sudo tee -a /etc/yum.repos.d/mongodb-org-2.6.repo
sudo yum -y update && sudo yum install -y mongodb-org-server \
    mongodb-org-shell mongodb-org-tools

Next, create/configure the mount points, mount each volume, set ownership (MongoDB runs under the mongod user/group):

sudo mkdir /data /log

sudo mkfs.ext4 /dev/xvdb
sudo mkfs.ext4 /dev/xvdc

echo '/dev/xvdb /data ext4 defaults,auto,noatime,noexec 0 0
/dev/xvdc /log ext4 defaults,auto,noatime,noexec 0 0' | sudo tee -a /etc/fstab

sudo mount /data
sudo mount /log

sudo chown mongod:mongod /data /log

Now configure the following MongoDB parameters by editing the configuration file /etc/mongod.conf so that it contains the following:

dbpath = /data
logpath = /log/mongod.log

In addition, remove or comment out the following lines in order to enable authentication and listen on all network interfaces:

auth=true

# Listen to local interface only. Comment out to listen on all interfaces.
#bind_ip=127.0.0.1

By default Amazon Linux uses ulimit settings that are not appropriate for MongoDB. To setup proper ulimit, run:

echo '* soft nofile 64000
* hard nofile 64000
* soft nproc 64000
* hard nproc 64000' | sudo tee /etc/security/limits.d/90-mongodb.conf

Additionally, default read ahead settings on EC2 are not optimized for MongoDB. As noted in the read-ahead settings from Production Notes, you should adjust the settings to read approximately 32 blocks (or 16 KB) of data. The following command will set the readahead appropriately (repeat for additional volumes):

sudo blockdev --setra 32 /dev/xvdb

To make this change persistent across system boot, issue the following command:

echo 'ACTION=="add|change", KERNEL=="xvdb", ATTR{bdi/read_ahead_kb}="16"' | sudo tee -a /etc/udev/rules.d/85-ebs.rules

To start mongod, issue the following command:

sudo service mongod start

To have MongoDB startup automatically at boot issue the following command:

sudo chkconfig mongod on

Backup

This section is based on Tutorial: Schedule Automated EBS Snapshots Using CloudWatch Events

Create a new Event Rule in CloudWatch:

  1. For Event Source, select "Schedule", then select "Cron expression", enter "0 8 * * ? *" (8am UTC)
  2. For Target, choose Add target and then select EC2 Create Snapshot API call. Enter EDB Volume Id for the data volume.

Click "Configure details", enter name "mongodb_data_backup", and description. For AWS permissions, choose the option to create a new role.

Replica Set Deployment on AWS

This section is based on Deploy a Highly-Available MongoDB Replica Set on AWS.

TODO