pt-online-schema-change --host=xxx -uxxx -pxxx \ --alter "add xxx int(1) NOT NULL DEFAULT '0' COMMENT
Perconal 推出一个工具 pt-online-schema-change,简称pt-osc,其特点是修改过程中不会造成读写阻塞。本文将简单介绍pt-osc的工作原理,安装方法及使用场景。 in @INC错误 则需执行yum -y install perl-Digest-MD5安装相关组件 提示缺少perl-DBI模块,那么直接 yum install perl-DBI 场景1:增加列 pt-online-schema-change age int(11) default null" D=test,t='test_tb' --execute --print --statistics --no-check-alter 场景2:删除列 pt-online-schema-change -alter "drop column age" D=test,t='test_tb' --execute --print --statistics --no-check-alter 场景3:更改列 pt-online-schema-change CHANGE id id_num int(20)" D=test,t='test_tb' --execute --print --statistics --no-check-alter 场景4:创建索引 pt-online-schema-change
概要 ---- 构建测试表 mysql> create table forpttest( id int(6), name char(10), comment char(10), abc char(10)); Query OK, 0 rows affected (0.10 sec) mysql> show create table forpttest\G *************************** 1. row *************************** Table:
非空报错 [root@h101 ~]# pt-online-schema-change -u root -h localhost -pmysql --alter='add column newid2 `forpttest` LOCK IN SHARE MODE /*pt-online-schema-change 6366 copy table*/ [root@h101 ~]# 如果新添列有非空约束
至少需要一个主键或唯一索引 Note: In almost all cases a PRIMARY KEY or UNIQUE INDEX needs to be present in the table. This is necessary because the tool creates a DELETE trigger to keep the new table updated while the process is running. 添加主键 mysql> show create table fo
当锁表时间过长时业务无法接受 于是有以下两种处理方式 1 用standby来操作,然后和master做切换 2 使用 pt-online-schema-change 工具直接在master上做修改 前一种方式比较折腾 ,特别是在有多个slave的情况下,由于表结构发生了变化,master发生了变化,重新构建slave很耗时 pt-online-schema-change 是一款开源的 percona-toolkit 下面分享一下 pt-online-schema-change 的基础操作,详细可以参阅 官方文档 Tip: 当前版本 pt-online-schema-change 2.2.14 ---- 概要 ---
. ---- 附:pt-online-schema-change 工作原理 pt-online-schema-change emulates the way that MySQL alters tables means that the original table is not locked, and clients may continue to read and change data in it. pt-online-schema-change
这里我们是MGR 单主模式,因此将 log_bin_trust_function_creators 设置为ON即可,注意要把其他节点的参数也改掉(另外配置文件中也改下使其持续化)。
添加新列 无主键报错 [root@h101 ~]# pt-online-schema-change -u root -h localhost -pmysql --alter='add column
mysql> select * from forpttest; +----+------+---------+------+-------+ | id | name | comment | abc | newid | +----+------+---------+------+-------+ | 1 | 1 | 1 | 1 | NULL | | 9 | 9 | 9 | 9 | NULL | | 10 | 10 | 10 | 10
添加主键成功 [root@h101 ~]# pt-online-schema-change -u root -h localhost -pmysql --alter='add column newid
Due to a limitation in MySQL, pt-online-schema-change adds a leading underscore to foreign key constraint Using a password on the command line interface can be insecure. 81 同时在另一个session里修改表结构 [root@h101 ~]# pt-online-schema-change
填入一些数据 [root@h101 ~]# i=1 [root@h101 ~]# while true; do echo $i; let "i=$i+1"; sleep 1 ; done 1 2 3 4 5 6 7 8 ^C [root@h101 ~]# while true; do echo $i; mysql -u root -pmysql -e "insert into pt.forpttest values($i,$i,$i,$i)" ;let "i=$i+1"; sleep 1 ; don
添加默认值成功 [root@h101 ~]# pt-online-schema-change -u root -h localhost -pmysql --alter='add column newid2
对于大表的 DDL操作,我们一般使用 pt-online-schema-change 来进行。 如下是一条DDL测试语句: ALTER TABLE tb_2 ADD COLUMN content text ; 对应的pt-osc写法如下: pt-online-schema-change --user INDEX(`PRIMARY`) WHERE ((`id` >= '20000000')) AND ((`id` <= '20000999')) LOCK IN SHARE MODE /*explain pt-online-schema-change FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '20000000')) AND ((`id` <= '20000999')) LOCK IN SHARE MODE /*pt-online-schema-change
二、pt-online-schema-change原理 1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。 三、测试用例 1、添加字段 pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute 2、删除字段 pt-online-schema-change no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute 3、修改字段 pt-online-schema-change no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute 4、字段改名 pt-online-schema-change
// pt-online-schema-change错误排查一例 // 今天在线上遇到一个PT工具相关的问题,问题是pt-osc这个在线改表工具报出来的。简单记录了一下排查的过程。 01 问题描述 线上的pt工具版本是3.1.0版本的,改表过程中的PT命令如下: pt-online-schema-change --alter " ADD COLUMN xxx " For more details, please use the --help option, or try 'pt-online-schema-change' for complete documentation --port=3366 D=sre_test,t=candles 正确写法: pt-online-schema-change D=sre_test,t=candles,P=3366 2、如果端口是3306 正确写法: pt-online-schema-change --port=3306 D=sre_test,t=candles 正确写法: pt-online-schema-change D=sre_test
MySQL 版本:8.0.22 pt-online-schema-change 版本:3.0.6 1. 使用 recursion-method 的 dsn 方法执行 pt-online-schema-change pt-online-schema-change \ --host="172.18.3.162 column type smallint default 1 not null comment '类型,1为小视频,2为频道视频';" \ --progress=time,30 \ --execute 参考: pt-online-schema-change
执行 pt-online-schema-change pt-online-schema-change \ --socket="/data/18253/mysqldata/mysql.sock" \ --
这些问题的解决方案正是pt-online-schema-change工具的设计精髓所在。 pt-online-schema-change工具概述与安装 什么是pt-online-schema-change? 实战演练:使用pt-online-schema-change修改表结构 准备工作与环境检查 在开始实际操作之前,我们需要确保环境满足pt-online-schema-change的基本要求。 深入原理:pt-online-schema-change如何实现不停机操作? 在深入探讨pt-online-schema-change的工作原理之前,我们需要理解传统DDL操作的根本痛点。 优势与局限:pt-online-schema-change的全面评估 工具的核心优势 pt-online-schema-change(简称pt-osc)作为Percona Toolkit的核心组件之一,