ALTER TABLE without locking the table?

后端 未结 19 2000
梦毁少年i
梦毁少年i 2020-11-30 17:41

When doing an ALTER TABLE statement in MySQL, the whole table is read-locked (allowing concurrent reads, but prohibiting concurrent writes) for the duration of the statement

19条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 17:58

    If you cannot afford downtime for your database when doing application updates you should consider maintaining a two-node cluster for high availability. With a simple replication setup, you could do almost fully online structural changes like the one you suggest:

    • wait for all changes to be replicated on a passive slave
    • change the passive slave to be the active master
    • do the structural changes to the old master
    • replicate changes back from the new master to the old master
    • do the master swapping again and the new app deployment simultaneously

    It is not always easy but it works, usually with 0 downtime! The second node does not have to be only passive one, it can be used for testing, doing statistics or as a fallback node. If you do not have infrastructure replication can be set up within a single machine (with two instances of MySQL).

提交回复
热议问题