[MySQL] 表在线重定义 - pt-online-schema-change

MySQL不像Oracle本身就支持表的在线重定义,但我们可以通过开源软件percona-toolkit中的工具pt-online-schema-change进行在线重定义。

官方文档:http://www.percona.com/doc/percona-toolkit/2.2/pt-online-schema-change.html#pt-online-schema-change

pt-online-schema-change包含在percona-toolkit中,所以我们得先下载安装:

wget percona.com/get/percona-toolkit.tar.gz
tar -zxvf percona-toolkit-2.2.6.tar.gz

   perl Makefile.PL
   make
   make test
   make install

通过上诉步骤之后,把./percona-toolkit-2.2.6/bin下的可执行文件加入到$PATH中即可。


好了,安装完成之后,就可以利用该工具进行在线重定义了。主要用到两个参数:

--dry-run
Create and alter the new table, but do not create triggers, copy data, or replace the original table.

--execute
Indicate that you have read the documentation and want to alter the table. You must specify this option to alter the table. If you do not, then the tool will only perform some safety checks and exit. This helps ensure that you have read the documentation and understand how to use this tool. If you have not read the documentation, then do not specify this option.

首先,我们用--dry-run验证是否可以执行修改:

pt-online-schema-change --alter "modify treatment_afterday INT(4) NULL" D=portal,t=comment_expert -uroot -p*** --dry-run
Operation, tries, wait:
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Starting a dry run.  `portal`.`comment_expert` will not be altered.  Specify --execute instead of --dry-run to alter the table.
Creating new table...
Created new table portal._comment_expert_new OK.
Altering new table...
Altered `portal`.`_comment_expert_new` OK.
Not creating triggers because this is a dry run.
Not copying rows because this is a dry run.
Not swapping tables because this is a dry run.
Not dropping old table because this is a dry run.
Not dropping triggers because this is a dry run.
2014-01-14T13:59:08 Dropping new table...
2014-01-14T13:59:08 Dropped new table OK.

确认无误之后,再用--execute真正执行:

pt-online-schema-change --alter "modify treatment_afterday INT(4) NULL" D=portal,t=comment_expert -uroot -pzhujie1986 --execute
Found 1 slaves:
  lx203
Will check slave lag on:
  lx203
Operation, tries, wait:
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `portal`.`comment_expert`...
Creating new table...
Created new table portal._comment_expert_new OK.
Altering new table...
Altered `portal`.`_comment_expert_new` OK.
2014-01-14T13:59:20 Creating triggers...
2014-01-14T13:59:20 Created triggers OK.
2014-01-14T13:59:20 Copying approximately 1165430 rows...
Copying `portal`.`comment_expert`:  19% 02:03 remain
Copying `portal`.`comment_expert`:  38% 01:37 remain
Copying `portal`.`comment_expert`:  55% 01:12 remain
Copying `portal`.`comment_expert`:  73% 00:44 remain
Copying `portal`.`comment_expert`:  90% 00:15 remain
2014-01-14T14:02:08 Copied rows OK.
2014-01-14T14:02:08 Swapping tables...
2014-01-14T14:02:08 Swapped original and new tables OK.
2014-01-14T14:02:08 Dropping old table...
2014-01-14T14:02:10 Dropped old table `portal`.`_comment_expert_old` OK.
2014-01-14T14:02:10 Dropping triggers...
2014-01-14T14:02:10 Dropped triggers OK.
Successfully altered `portal`.`comment_expert`.

除了修改表结构之外,在大表上建索引也可以利用这种方法防止锁表。


评论 3 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

Zhu_Julian

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值