Running MySQL on Amazon EC2 with Elastic Block Store (another take)

Summary:

I have been following ‘Eric Hammod’ article on how to setup MySQL so that, it survives EC2 instance termination or restarts.  That article is one of best article but it’s too much for my requirements. So I came up with some more simple steps to host MySQL files on EBS volume. Many parts of Eric’s article still applies like how to create EBS volume and how to create snapshot of EBS.

How it is different from Eric’s version.

  1. Eric’s article is solving this problem by mounting original locations as mirror of EBS locations. In case of restart, you will have to fix fstab file. In my case, you will have to fix my.cnf file. I rather not touch fstab file.
  2. My purpose, just to do enough, to save my MySQL data files in case of my instance crashes/restarts. Hence I don’t care of configuring log files location.
  3. I am using elastic fox plugin for creating EBS volume etc. Able to avoid ec2 tools. (if you planning to do lots of work, better spend some time setting up tools and get familiarized.)

Pre-requisites for readers

  • You are familiar with Amazon EC2 images and how to play with them.
  • You already have installed MySQL and it is successfully running.

How to configure MySQL to use data files on EBS.

I have created 10G EBS volume and mounted that volume to /mnt/jframeworks. Attach volume to instance (as /dev/sdf). I used Elasticfox firefox plugin. Using amazon ec2 tools was getting too much time consuming.

Let’s create a new user also, make his home on EBS only, to start with. Adding new user is not necessary for this purpose. But I think, it is good idea to create new user and use that user for all new changes starting from fresh EC2 image.

sudo useradd -d /mnt/jframeworks/home/<newuser> -m <newuser>
sudo passwd <newuser>
sudo usermod -a -G admin <newuser> -s /bin/bash
sudo usermod -s /bin/bash <newuser> (may not be needed)
sudo mkfs -t ext3 /dev/sdf

(Formatting as ext3. Being linux noob, not sure, why xfs filesystem as mentioned in Eric’s article.)

sudo mkdir /mnt/jframeworks
sudo mount /dev/sdf /mnt/jframeworks

First create required data directory on mounted EBS volume

mkdir /mnt/jframeworks/var
mkdir /mnt/jframeworks/var/lib

Copy MySQL libs to EBS directory

cp -a /var/lib/mysql /mnt/jframeworks/var/lib/

Note Remember to use “-a” flag while copying.

MySQL settings are stored in my.cnf file, available at /etc/mysql.

Hence change /etc/mysql/my.cnf file to change data directory to new location

In this example, here it looks like ->

datadir = /mnt/jframeworks/var/lib/mysql

AppArmor is new tool, which is being used in ubuntu by default. Hence lets fix that too.

sudo vi /etc/apparmor.d/*mysqld, replicate lines /var/lib/mysql** with new data dir location

Now finally, lets restart MySQL and AppArmor, to make changes in effect.

sudo /etc/init.d/apparmor restart
sudo /etc/init.d/mysqld restart

Why I decided to host website using Amazon EC2

Summary

Cloud computing is something, we been listening and reading about from past couple of years.  From my perspective, it is virtual machines running on virtualized hardware. Hence giving us dual benefit of running any image on any kind of hardware. We can dynamically launch any image and also dynamically expand/reduce available hardware capacity. Hence the word ‘Elastic’ & ‘Cloud’. In cloud computing world, amazon is more established compare to others.  So rest of article will use Amazon EC2. Amazon elastic computing allows user to launch any image (from their growing library of public images). They are called ‘AMI’s. You pay per use of images. (hourly usage rate plans). Hence it makes it easy to choose any image w/o binding to any contract.

Why cloud computing is better than tradition web hosting?

Answer is, it is not. If you looking to run simple apache web server and few php based applications then web hosting companies give best pricing and pre-configured applications.  Decision comes, if you looking for better hardware and more RAM for your applications.

Pros, Comparing amazon EC2 smallest image & virtual private server (VPS) plans.

  1. Amazon EC2 guarantees one CPU core whereas 4-5 VPS images run per CPU core.
  2. RAM available is 1.7GB for EC2 instance and VPS gives maximum of 512MB to 756MB (for plans >$40/mo) (for simultaneous multiple J2EE web applications, you wish for more RAM.)
  3. Available capacity is elastic. You can expand it as required, compare to fixed capacity available in VPS/Dedicated plans.
  4. Once you finished configuring your images, you can make current installation as another image incase you want multiple instances running.
  5. Amazon EC2 instance are running with better reliable infrastructure comparing.
  6. If you pre-pay for EC2 account, your average cost of running website 24×7, for whole month comes to $42 something, which is comparable to good VPS plans with lots of memory.

Hence comparing to dedicated hosting, amazon ec2 seems to be better option, if we start comparing relability factor, ability to instantly increase computing capacity, price and many other options.

I decided to give it try to move my website.

Other factors to consider

  • 1.7 GB of available RAM and 160 GB of drive capacity, gives me chance to run java apps w/o worrying about available memory.
  • It was learning experience for me, (as average user of linux OS).
  • It’s easy to install applications on ubuntu image. (same goes for other OS like fedora/centOS).
  • I can run MySQL & PostGres DB server together, as required.
  • Java tomcat web server seems to be good enough, but I can try some other servers like glassfish or jboss for better experience.
  • Using it as subversion server. Ping time & bandwidth is better than other servers.
  • Will be using as build server & agent both. (Most probably open source project ‘Hudson’).