It is a common practice to architect systems with data layer (database servers) running on dedicated hardware. This post describes setting up and launching a MySQL server on Amazon EC2. A typical web application architecture would have two or more of such servers. While many server images (AMIs) are available on Amazon and many include MySQL, finding precisely what they contain and matching it to your needs is not always straightforward. Hence you may want to build your own image. Here are the steps to do it:
1. Launch an EC2 instance
The process of launching an instance is described at http://docs.amazonwebservices.com/AWSEC2/latest/GettingStartedGuide/.
The key question to answer, before going through the process, is what operating system to choose and which operating system AMI to select. CentOS and Ubuntu are frequent Linux choices. (You can find more about selecting a distribution of Linux at Which Linux?) We picked Ubuntu.
You can find Ubuntu AMIs at AMI Locator. If you go to that page, you will find a rather lengthy list. To narrow down the choices you need to specify:
- An Amazon region: we selected US-East as our customers are primarily in the USA and US-East is cheaper than US-West
- OS version: we chose 10.04 LTS (Lucid Lynx), a Long Term Support release
- Architecture: we selected 64-bit typically recommended for servers
Searching for US-East Lucid 64 will reveal two options – EBS and Instance Store AMI – as shown below:
You can find discussion on how to choose between the two at What is the difference between using the local instance store and Amazon Elastic Block storage (Amazon EBS) for the root device? We selected an EBS AMI.
Now you can copy an AMI-ID from the Locator, paste it into the Launch Instance Wizard and complete the Launch Instance process.
2. Install MySQL
Running MySQL on Amazon EC2 with EBS (Elastic Block Store) tutorial describes step by step process of setting up MySQL server on Ubuntu. It also describes how to automate snapshotting the databases for backup purposes.
3. Connect application servers to the MySQL server
Database server, as installed in Steps 1 and 2, accepts requests from the localhost only. To enable access from the application servers you need to do two things:
a. Make sure the Security Group the database server belongs to has port 3306 open for access from the application servers and application servers only.
b. Comment out the bind-server command in the /etc/mysql/my.cnf configuration file and restart the MySQL server. Default configuration is bind-address = 127.0.0.1 (localhost access only).
You should be able now to log in to any of your application servers and access mysql from it.