正常情况下线上大表新增字段是需要停机的,因为这时候mysql会锁表,任何写操作都会被阻塞。不过如果能保证数据库只读就没问题。
下面是一次线上操作记录,没有对网站造成任何影响。
mysql> alter table album_album add `cover` varchar(255) DEFAULT NULL;
Query OK, 3839848 rows affected (4 min 18.95 sec)
Records: 3839848 Duplicates: 0 Warnings: 0
mysql> show processlist;
+----------+-------------+----------------------+-------+-------------+---------+----------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----------+-------------+----------------------+-------+-------------+---------+----------------------------------------------------------------+------------------------------------------------------------------------------------------------------+
| 70003726 | root | localhost | zcwdb | Query | 161 | copy to tmp table | alter table album_album add `cover` varchar(255) DEFAULT NULL