MySQL 5.7使用pt-online-schema-change对大表加字段

来源:这里教程网 时间:2026-03-01 12:43:22 作者:

业务需求:对一张线上的3000W行的表加字段:用普通方式,建议小表使用:ALTER TABLE `idempotent`ADD COLUMN `orderid` VARCHAR(20) NULL DEFAULT NULL COMMENT '业务单据号' AFTER `isdelete`; ALTER TABLE ship_type ADD printer_name VARCHAR(50)  COMMENT '打印机名称';大表会锁表。 对大表加字段,建议用pt-online-schema-change工具,可以不锁表。对表 idempotent加字段 orderid: 命令: [root@DB001 ~]#pt-online-schema-change --user=root --password=123456 --host=192.168.1.18  --alter "ADD COLUMN orderid VARCHAR(20) NULL DEFAULT NULL COMMENT '业务单据号'" D=ora,t=idempotent --no-check-replication-filters  --execute Found 1 slaves:localhost.localdomain -> 192.168.1.19:socketWill check slave lag on:localhost.localdomain -> 192.168.1.19:socketOperation, tries, wait:  analyze_table, 10, 1  copy_rows, 10, 0.25  create_triggers, 10, 1  drop_triggers, 10, 1  swap_tables, 10, 1  update_foreign_keys, 10, 1Altering `ora`.`idempotent`...Creating new table...Created new table ora._idempotent_new OK.Altering new table...Altered `ora`.`_idempotent_new` OK.2019-10-23T19:01:00 Creating triggers...2019-10-23T19:01:00 Created triggers OK.2019-10-23T19:01:00 Copying approximately 24968401 rows...Copying `ora`.`idempotent`:   4% 10:53 remainCopying `ora`.`idempotent`:   8% 10:40 remainCopying `ora`.`idempotent`:  11% 11:49 remainCopying `ora`.`idempotent`:  14% 12:08 remainCopying `ora`.`idempotent`:  17% 12:09 remainCopying `ora`.`idempotent`:  19% 12:15 remainCopying `ora`.`idempotent`:  22% 12:15 remainCopying `ora`.`idempotent`:  24% 12:11 remainCopying `ora`.`idempotent`:  27% 12:09 remainCopying `ora`.`idempotent`:  29% 12:03 remainCopying `ora`.`idempotent`:  31% 11:45 remainCopying `ora`.`idempotent`:  34% 11:23 remainCopying `ora`.`idempotent`:  37% 11:01 remainCopying `ora`.`idempotent`:  39% 10:39 remainCopying `ora`.`idempotent`:  42% 10:11 remainCopying `ora`.`idempotent`:  44% 09:50 remainCopying `ora`.`idempotent`:  47% 09:35 remainCopying `ora`.`idempotent`:  49% 09:14 remainCopying `ora`.`idempotent`:  52% 08:44 remainCopying `ora`.`idempotent`:  55% 08:10 remainCopying `ora`.`idempotent`:  59% 07:17 remainCopying `ora`.`idempotent`:  63% 06:21 remainCopying `ora`.`idempotent`:  67% 05:32 remainCopying `ora`.`idempotent`:  71% 04:44 remainCopying `ora`.`idempotent`:  75% 03:59 remainCopying `ora`.`idempotent`:  80% 03:11 remainCopying `ora`.`idempotent`:  84% 02:28 remainCopying `ora`.`idempotent`:  88% 01:49 remainCopying `ora`.`idempotent`:  92% 01:08 remainCopying `ora`.`idempotent`:  96% 00:28 remain2019-10-23T19:18:24 Copied rows OK.2019-10-23T19:18:24 Analyzing new table...2019-10-23T19:18:24 Swapping tables...2019-10-23T19:18:24 Swapped original and new tables OK.2019-10-23T19:18:24 Dropping old table...2019-10-23T19:18:26 Dropped old table `ora`.`_idempotent_old` OK.2019-10-23T19:18:26 Dropping triggers...2019-10-23T19:18:26 Dropped triggers OK.Successfully altered `ora`.`idempotent`.You have mail in /var/spool/mail/root 整个过程持续10几分钟,不锁表。字段添加完成。 PS:切记在业务低峰运行。

pt-online-schema-change --user=user --password=xxx --host=ip --port=3306 --alter “add column col1 VARCHAR(64) NULL COMMENT ‘订单号’” D=sy,t=t1 --execute --charset=utf8  --nocheck-replication-filters --max-load=“Threads_running=20”

可以指定字符集,防止加好字段的表的注释出现乱码。修改注释:ALTER TABLE idempotent MODIFY COLUMN `sysno` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '编号';

相关推荐