Amazon Linux: Migrate MySQL from EBS to iSCSI

This week, I was working with a 100% cloud business, who have been using  AWS from their incorporation over 5 years ago.

The business wanted to take advantage of the instant cloning capabilities offered by running ONTAP in AWS and migrate the existing MySQL databases to the new cloud storage system.

These steps will get you up and running quickly, including some handy gotchas that you may run into.

First, I assume that you already have your Amazon Linux (or other instance) up and running and that you have your iSCSI devices mapped and mounted in your instance. Don’t worry if you haven’t – I created a handy post here that covers it.

Let’s check the current location of your MySQL datadir, there are two methods for checking this:

Method #1: Check within MySQL

mysql -u root -p

select @@datadir;

Method #2: Check from the console and look at the current value of datadir (for example datadir=/var/lib/mysql)

sudo cat /etc/my.cnf

Now that we know the current data directory we can move the database to the new location with these steps. In this example, my iSCSI device is mounted to /mnt/ontap

Shutdown the MySQL service (this ensures data and application consistency):

sudo service mysqld stop

Next, copy the database to the new location:

sudo rsync -av /var/lib/mysql /mnt/ontap

Once complete it’s best to rename the old directory to .bak to prevent confusion:

sudo mv /var/lib/mysql /var/lib/mysql.bak

Now it’s time to change the working directory path to the new one:

sudo nano /etc/my.cnf


It would seem natural at this point to start the service, however, there is at least one more step:

(Optional: If you are using AppArmour):

sudo nano /etc/apparmor.d/tunables/alias

At the bottom of the file, add the following alias rule:

. . .
alias /var/lib/mysql/ -> /mnt/netapp1/mysql/,
. . .

Restart the AppArmour servicesudo

sudo service restart apparmour

Now for the final step, MySQL runs an environment check script upon service start. The script simply checks for the existence of /var/lib/mysql and /var/lib/mysql/mysql . We need to create a minimal directory structure to pass the environment check:

sudo mkdir /var/lib/mysql/mysql -p

That’s it! Now we can start the MySQL service:

sudo service mysqld start

Finally, let’s check that the new data directory is indeed in use:

mysql -u root -p

select @@datadir

Thanks for reading, I hope you found this useful!