MySQL在线添加字段的几种方案介绍

方案1:业务低峰期进行alter table操作:
例如,凌晨1点或2点进行:
mysql>ALTER TABLE `test` ADD `new_column` int not null default 0 comment '文本长度';


方案2:创建临时表、加字段、插入旧数据,增量数据插入和重命名表名

1). 创建临时表
mysql>create table test_tmp like test;

2). 在临时表上添加字段
mysql>ALTER TABLE `test_tmp` ADD `new_column` int not null default 0 comment '文本长度';


3). 向临时表里插入旧数据
mysql>INSERT INTO test_tmp(id,name) (SELECT id,name from test);

4). 增量数据插入:
mysql>INSERT INTO test_tmp(id,name) (SELECT id,name from test WHERE `id` > (SELECT MAX(`id`) from `test_tmp`));


5). 重命名表名:
mysql>rename table test TO test_2018;
mysql>rename table test_tmp TO test;


方案3:用第三方开源工具实现,具体实现步骤看链接:
oak-online-alter-table:
https://wing324.github.io/2015/10/13/oak-online-alter-table%E5%B7%A5%E5%85%B7/

pt-osc:
https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html

gh-ost:
http://www.cnblogs.com/jiangwenju/p/5846743.html

facebook osc(Online Schema Change for MySQL):
https://github.com/facebookincubator/OnlineSchemaChange
http://cenalulu.github.io/mysql/open_source_osc/

方案4:在从库进行添加字段操作,然后进行mysql主从切换
注意事项:主从复制日志格式如果为row格式,添加的新列必须是在表的最后。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2153012/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2153012/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值