Running MySQL on Amazon EC2

In a previous article, I talked about setting up JBoss AS 7 in Amazon EC2. Today, I will talk about MySQL installation.

The SUSE Linux Enterprise Server 11 AMI has MySQL pre-installed. But, I had all kinds of problems trying to start the server. So, I decided to uninstall all the pre-installed packages and start from scratch. In real life though, you will probably go with RDS. But, if you want to build a simple system and want to install MySQL on your own, these steps may help.

Make sure that you are logged in as root. Installation of MySQL from RPM requires root privilege.

First of all, remove all pre-installed packages. You can get a list:

rpm -qa | grep -i mysql

Then uninstall all of them. For example:

rpm -e mysql libmysqlclient15-5.0.94- <and so on>

Completely remove the /var/lib/mysql folder. This is where the actual database data files are stored. Without this step, a future installation will run into problem.

cd /var/lib
rm -rf mysql

Download the MySQL server RPM:

curl -o mysql.rpm \

Then, install the RPM.

rpm -Uvh mysql.rpm

This will copy the files and create the mysql user, which we will use later.

Download the MySQL Client package.

curl -o mysql-client.rpm \

Install the package.

rpm -Uvh mysql-client.rpm

In SUSE, all MySQL binary executables are directly stored under /usr/bin. Since, this folder is already in your PATH, you can run various MySQL commands from anywhere.

First, you must initialize the database. It is essential that you use the mysql user here:

mysql_install_db --user=mysql

This will make mysql user the owner of various files.

Finally, run the server. Once again, make sure that you use the mysql user for this:

mysqld_safe --user=mysql &

Verify that server has started by running a few commands:

mysqladmin version

Shutdown the server.

mysqladmin -u root shutdown

Here, “root” is the MySQL user and not the UNIX user.

Only thing left to do now is have MySQL start and stop automatically as the machine boots up and shuts down. The RPM installer has already created the /etc/init.d/mysql file. We will make  small change.

Open /etc/init.d/mysql in an editor.

Find these two lines:


Change them as follows:


To be extra sure, you can run the “mysqladmin variables” command and look up the correct values of the basedir and datadir variables.

Reboot the EC2 instance and verify that MySQL has started automatically.

You should now have a fully functional MySQL installation. The biggest gotcha in the process is the file system security. You run all the steps by logging in as root. But, actual work done by MySQL has to be done by the mysql user. This is why commands like mysql_install_db and mysqld_safe are run with the mysql user setting.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s