Monday, June 04, 2007

Amazon EC2 Ephemeral Storage (/mnt) and MySQL

Amazon Elastic Compute Cloud (EC2) includes 160 GB of local storage. This storage is split into two partitions:

  1. ~10 GB for your VM/AMI image instance which includes the OS and software.
  2. ~147 GB for your "ephemeral storage" which is where you should put your data or anything that is going to need a lot of space. This storage is in /mnt.
So in order to let your MySQL database grow, you'll need to put the data files in /mnt and the easiest way to do this is to make a symbolic link from the default MySQL data storage location (usually /var/lib/mysql) to a directory on /mnt. Be sure to do this immediately after installing MySQL so you won't run into any issues.

Here is a step by step how to do it:
  1. mkdir /mnt/mysql
    1. Creates your new data directory
  2. chown mysql:mysql /mnt/mysql
    1. Gives mysql ownership to the directory
  3. /etc/init.d/mysql stop
    1. Stop MySQL before moving data files
  4. mv /var/lib/mysql/* /mnt/mysql
    1. Move your data files
  5. rmdir /var/lib/mysql
    1. Delete the old directory
  6. ln -s /mnt/mysql mysql
    1. Create a symlink to your new directory on /mnt
  7. /etc/init.d/mysql start
    1. Restart MySQL
That's all she wrote. One last very important thing to remember: This storage is NOT reliable. If your instance is terminated or crashes, your data is lost forever! So always be sure to do regular backups to S3.