Mysql – Will an ALTER TABLE ADD COLUMN on a slave break replication

alter-tablelockingMySQLperformancereplication

I need to add a last_modified column to a MyISAM table tbl_items. The crux of this problem is that tbl_items houses several gigabytes of data. Also of note, I am using a master-slave deployment with one slave.

Obviously, running the necessary ALTER TABLE command on the master is unacceptable since it locks the table for well over a half-hour. I cannot afford this kind of downtime at ANY time of day or night, most unfortunately.

A few sources have suggested some form of the following:

  1. Disable replication to the slave
  2. Run the ALTER TABLE command on the slave
  3. Switch replication back on and wait for slave to catch up
  4. Promote slave to master

Is this a viable solution? It would seem to me that all new tbl_items inserts would be lost between the beginning of step 2 and step 4; that is, is it possible for tbl_items data to replicate throughout the interval that it's missing the new column?

If that's the case, is there an alternate solution that eliminates the possibility of both downtime and data-loss?

Best Answer

I think that, if your purpose is to avoid locking, you should use pt-online-schema-change (a tool from Percona Toolkit) or Gh-ost (from GitHub).

Here is how pt-osc works:

  • Creates a ghost table identical to the original table, but empty
  • Runs ALTER TABLE on the ghost table
  • Creates triggers on the original table, which insert new rows in the new table, and updates and deletes them if they are already there.
  • It copies the rows in chunks, monitoring the server performance (it can slow down or pause its work if necessary)
  • Switch the table names

gh-ost is very similar, the main difference is that it creates no triggers, and uses the binary log instead to detect the changes to the original table.

To answer your question more directly - yes, it is possible to add a column on the slave. And most probably it will simply work, as long as it has a default value. But it is very likely that in the future you will regret this choice, because you'll need to do something that will break replication, like adding a column in the master. I recommend to sure to check the documentation and fully understand the dangers, before doing such a thing.