如果您有点冒险,可以通过在可见的阶段执行ALTER TABLE来解决问题。假设您要更改的表称为WorkingTable。您可以按以下步骤进行更改:
#
# Script 1
# Alter table structure of a single column of a large table
#
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
您可以在所有从站上执行此操作。那主人呢?如何防止复制到从站。简单:不要将SQL发送到主数据库的二进制日志中。在执行ALTER TABLE之前,只需在会话中关闭二进制日志记录即可:
#
# Script 2
# Alter table structure of a single column of a large table
# while preventing it from replicating to slaves
#
SET SQL_LOG_BIN = 0;
CREATE TABLE WorkingTableNew LIKE WorkingTable;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTable;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
可是等等 !!!处理这些命令时出现的任何新数据如何处理?在操作开始时重命名表应该可以解决问题。让我们稍微修改一下此代码,以防止在这方面输入新数据:
#
# Script 3
# Alter table structure of a single column of a large table
# while preventing it from replicating to slaves
# and preventing new data from entering into the old table
#
SET SQL_LOG_BIN = 0;
ALTER TABLE WorkingTable RENAME WorkingTableOld;
CREATE TABLE WorkingTableNew LIKE WorkingTableOld;
ALTER TABLE WorkingTableNew MODIFY BigColumn VARCHAR(50);
INSERT INTO WorkingTableNew SELECT SQL_NO_CACHE * FROM WorkingTableOld;
ALTER TABLE WorkingTableNew RENAME WorkingTable;
DROP TABLE WorkingTableOld;
- 可以在任何未启用二进制日志的从站上执行脚本1
- 脚本2可以在任何启用了二进制日志的从站上执行
- 脚本3可以在主服务器或其他任何地方执行
试试看 !!!