User data to install and run MongoDB in Amazon Web Services (AWS) instance

by Marc Cortada 26/11/2015

This script installs MongoDB in an AWS EC2 Ubuntu instance, reboots the newly created server and when is up and running an empty MongoDB server is available (to know why the machine must be rebooted go to the end of this post).

Just add the following lines into the text box located at the section "Advanced Details > user data" when you are configuring a new EC2 instance. Remember that the selected AMI type in the previous steps must be an Ubuntu Server.

#!/bin/bash
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.0.list
apt-get update
apt-get install -y mongodb-org
apt-get install -y mongodb-org=3.0.5 mongodb-org-server=3.0.5 mongodb-org-shell=3.0.5 mongodb-org-mongos=3.0.5 mongodb-org-tools=3.0.5
echo "mongodb-org hold" | dpkg --set-selections
echo "mongodb-org-server hold" | dpkg --set-selections
echo "mongodb-org-shell hold" | dpkg --set-selections
echo "mongodb-org-mongos hold" | dpkg --set-selections
echo "mongodb-org-tools hold" | dpkg --set-selections

echo "#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
   echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi

exit 0

" > /etc/rc.local

reboot 0

A restart is needed at the end of the script execution because it disables Transparent Huge Pages (THP) following the MongoDB recommendations: "You should disable THP on Linux machines to ensure best performance with MongoDB". All changes made to the configuration file take effect when the machine is rebooted.

Tags:

Categorías: Amazon Web Services (AWS) | AWS User Data | BigData | MongoDB

Entradas por mes