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:
- Disable replication to the slave
- Run the
ALTER TABLE
command on the slave - Switch replication back on and wait for slave to catch up
- 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:
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.