Console Login

Zero-Downtime Database Migration: A Battle Plan for 2013

Surviving the Shift: Zero-Downtime MySQL Migration Strategies for High-Traffic Nodes

It is 3:00 AM on a Tuesday. I am staring at a terminal window, watching a cursor blink. The client is a large e-commerce retailer based here in Oslo, and their current hosting provider—let's call them "BudgetBox"—is choking under the load of their spring sale. The database lock wait timeouts are spiking. We need to move. Now.

Migration is the most stressful task for any Systems Administrator. Get it right, and nobody notices. Get it wrong, and you corrupt the ibdata1 file, lose customer orders, and spend the next week explaining to the CEO why the site is 404ing. I have performed hundreds of migrations, from simple mysqldump transfers to complex multi-master replication setups across the NIX (Norwegian Internet Exchange). The physics of spinning rust (HDDs) versus the new wave of SSD storage changes the game, but the logic remains the same: Don't trust the network, and verify every byte.

The "Dump and Pray" Method (and Why You Should Avoid It)

For small sites, you might get away with a simple dump and restore. But if your database exceeds 5GB, or if you cannot afford more than 15 minutes of downtime, this method is suicide.

The standard command looks like this:

mysqldump -u root -p --opt --single-transaction --master-data=2 my_database | gzip > /tmp/db_dump.sql.gz

While --single-transaction helps with InnoDB consistency without locking the whole table, the restoration process on the destination server is bound by disk I/O. On a standard 7.2k RPM drive, you are looking at hours of restoration time. This is where hardware selection becomes critical.

The Professional Approach: Master-Slave Replication

For mission-critical applications, we don't move the database. We clone it, sync it, and then flip the switch. This relies on MySQL's native replication features. By setting up your new CoolVDS instance as a slave to your current sluggish master, you can replicate data in real-time until the lag is zero.

Step 1: Prepare the Master

First, ensure your my.cnf (usually in /etc/mysql/ or /etc/) is configured for binary logging. If you haven't enabled this yet, a restart is required.

[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
binlog_do_db = targeted_db_name

Step 2: The Snapshot

Instead of blocking the database for hours with a lock, we use Percona XtraBackup. It is the gold standard in 2013 for hot backups. It copies InnoDB data files without locking the database.

innobackupex --user=root --password=YOURPASS /data/backups/

Once the backup is ready, we use rsync to push it to the new server. Note the flags; we want to preserve permissions and show progress.

rsync -avz --progress /data/backups/2013-04-25_03-00-00/ root@new-server-ip:/data/backups/

Step 3: Configure the Slave (The New Server)

On your fresh CoolVDS instance (running CentOS 6 or Ubuntu 12.04), restore the backup and configure MySQL to read from the master.

service mysql stop
rm -rf /var/lib/mysql/*
innobackupex --copy-back /data/backups/2013-04-25_03-00-00/
chown -R mysql:mysql /var/lib/mysql
service mysql start

Now, grab the binary log coordinates from the xtrabackup_binlog_info file and set up the replication chain.

CHANGE MASTER TO
MASTER_HOST='10.0.0.5',
MASTER_USER='replicator',
MASTER_PASSWORD='secure_password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=107;
START SLAVE;

Hardware Matters: The KVM Advantage

I cannot stress this enough: Virtualization architecture dictates database performance. Many providers in Europe are still pushing OpenVZ containers. In an OpenVZ environment, the kernel is shared. If your neighbor decides to run a heavy tar command or compile a kernel, your MySQL I/O wait shoots up. The buffer cache is contested territory.

This is why serious deployments use KVM (Kernel-based Virtual Machine). It provides true hardware virtualization. At CoolVDS, we use KVM exclusively. When you allocate 4GB of RAM to your database, that RAM is yours. The Linux kernel inside your VM manages its own memory pages. For databases, this isolation is non-negotiable.

Pro Tip: Check your I/O scheduler. On a virtualized environment, the default `cfq` scheduler can be inefficient. Switch to `deadline` or `noop` for better throughput on SSD-backed storage.

echo noop > /sys/block/vda/queue/scheduler

Tuning for the New Environment

Simply moving the data isn't enough. You have likely moved to CoolVDS because you have access to better hardware—specifically, our high-performance SSD tiers. You must update your configuration to utilize this.

On spinning disks, we had to be conservative with innodb_io_capacity. On SSDs, you can crank this up. Update your my.cnf:

[mysqld]
# Increase for SSD performance
innodb_io_capacity = 2000
innodb_read_io_threads = 8
innodb_write_io_threads = 8

# RAM allocation (Assumes 8GB System)
innodb_buffer_pool_size = 6G
innodb_flush_method = O_DIRECT

Local Sovereignty and Latency

Beyond the technical bits, there is the legal reality. With the Norwegian Personal Data Act (Personopplysningsloven) and the vigilant eye of Datatilsynet, keeping your customer data within national borders is often a compliance requirement, not just a preference. Hosting outside of Norway introduces latency and legal complexity.

By hosting on CoolVDS, you are pinging 2ms to the NIX in Oslo. For a database-heavy application, every millisecond of network latency compounds. If your web server does 50 queries to generate a page, a 10ms latency difference between app and DB servers results in a half-second delay for the user. That is an eternity in e-commerce.

Final Cutover

Once your slave shows Seconds_Behind_Master: 0, you are ready.

  1. Put your web application in maintenance mode.
  2. Stop writes to the old master.
  3. Verify the slave has processed the last transaction.
  4. Promote the slave to master (STOP SLAVE; RESET SLAVE ALL;).
  5. Update your application config to point to the new IP.

Total downtime? Often less than 60 seconds.

Don't let slow I/O or noisy neighbors kill your application's performance. If you are ready to test a true KVM environment with pure SSD storage, spin up a test instance on CoolVDS today.