千家信息网

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

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章将为大家详细讲解有关MySQL 5.7如何使用pt-online-schema-change对大表加字段,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。业务需
千家信息网最后更新 2026年11月02日MySQL 5.7如何使用pt-online-schema-change对大表加字段

这篇文章将为大家详细讲解有关MySQL 5.7如何使用pt-online-schema-change对大表加字段,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

业务需求:

对一张线上的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:socket

Will check slave lag on:

localhost.localdomain -> 192.168.1.19:socket

Operation, 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, 1

Altering `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 remain

Copying `ora`.`idempotent`: 8% 10:40 remain

Copying `ora`.`idempotent`: 11% 11:49 remain

Copying `ora`.`idempotent`: 14% 12:08 remain

Copying `ora`.`idempotent`: 17% 12:09 remain

Copying `ora`.`idempotent`: 19% 12:15 remain

Copying `ora`.`idempotent`: 22% 12:15 remain

Copying `ora`.`idempotent`: 24% 12:11 remain

Copying `ora`.`idempotent`: 27% 12:09 remain

Copying `ora`.`idempotent`: 29% 12:03 remain

Copying `ora`.`idempotent`: 31% 11:45 remain

Copying `ora`.`idempotent`: 34% 11:23 remain

Copying `ora`.`idempotent`: 37% 11:01 remain

Copying `ora`.`idempotent`: 39% 10:39 remain

Copying `ora`.`idempotent`: 42% 10:11 remain

Copying `ora`.`idempotent`: 44% 09:50 remain

Copying `ora`.`idempotent`: 47% 09:35 remain

Copying `ora`.`idempotent`: 49% 09:14 remain

Copying `ora`.`idempotent`: 52% 08:44 remain

Copying `ora`.`idempotent`: 55% 08:10 remain

Copying `ora`.`idempotent`: 59% 07:17 remain

Copying `ora`.`idempotent`: 63% 06:21 remain

Copying `ora`.`idempotent`: 67% 05:32 remain

Copying `ora`.`idempotent`: 71% 04:44 remain

Copying `ora`.`idempotent`: 75% 03:59 remain

Copying `ora`.`idempotent`: 80% 03:11 remain

Copying `ora`.`idempotent`: 84% 02:28 remain

Copying `ora`.`idempotent`: 88% 01:49 remain

Copying `ora`.`idempotent`: 92% 01:08 remain

Copying `ora`.`idempotent`: 96% 00:28 remain

2019-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 '编号';

关于"MySQL 5.7如何使用pt-online-schema-change对大表加字段"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

字段 业务 对大 篇文章 单据 建议 更多 注释 不错 实用 普通 乱码 内容 名称 命令 字符 字符集 工具 打印机 文章 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网与科技 深圳九邦互联网科技有限公司 A股互联网金融科技公司 互联网科技会 互联网科技集团 礼当家互联网科技 艾特科技互联网 齐家互联网科技有限公司 互联网科技网站 云创客互联网科技公司 北京三清互联网科技 特约风豹互联网科技为什么扣费 工业互联网展湾科技股份有限公司 滨湖区互联网智能科技产品有哪些 山东子淼互联网科技有限公司 深圳互联网科技的发展 有人说阿里系是互联网科技企业 卡尔迅互联网科技有限公司 互联网科技收购价值 长沙易易互联网科技有限公司 上海挚品互联网科技有限公司 广东猪兼强互联网科技有限公司 科技互联网企业 科技互联网政务服务 互联网科技ppt模板 银川新华互联网科技职业技术培训学校图片 宝灵互联网科技有限公司 四川慕妮娅互联网科技有限公司 中科明德北京互联网科技 云南省自由互联网科技有限公司
0