1 Star 0 Fork 0

苏落凡 / 拉勾私教作业

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pt-online-schema-change.md 8.03 KB
一键复制 编辑 原始数据 按行查看 历史
帅到你自杀 提交于 2021-08-29 15:14 . 滑动窗口

InnoDB引擎是通过以下步骤来进行DDL的:

1、按照原始表(original_table)的表结构和DDL语句,新建一个不可见的临时表(tmp_table)
2、在原表上加write lock,阻塞所有更新操作(insert、delete、update等)
3、执行insert into tmp_table select * from original_table
4、rename original_table和tmp_table,最后drop original_table
5、释放 write lock。
可以看见在InnoDB执行DDL的时候,原表是只能读不能写的。为此 perconal 推出一个工具 pt-online-schema-change ,其特点是修改过程中不会造成读写阻塞。

pt-online-schema-change

名字:pt-online-schema-change - ALTER tables without locking them. 在线改表

下载地址: https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#downloading 官方文档:https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html

安装

下载最新的pt-online-schema-change
[root@18c data]# wget https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm
安装
[root@18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm 
warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
error: Failed dependencies:
        perl(DBI) >= 1.13 is needed by percona-toolkit-3.0.12-1.el6.x86_64
        perl(DBD::mysql) >= 1.0 is needed by percona-toolkit-3.0.12-1.el6.x86_64
        perl(Time::HiRes) is needed by percona-toolkit-3.0.12-1.el6.x86_64
        perl(IO::Socket::SSL) is needed by percona-toolkit-3.0.12-1.el6.x86_64
        perl(Term::ReadKey) is needed by percona-toolkit-3.0.12-1.el6.x86_64
		
安装相关的依赖包:
 [root@18c data]#  yum install perl-TermReadKey.x86_64 
[root@18c data]#   yum install perl-DBI
[root@18c data]#   yum install perl-DBD-MySQL
[root@18c data]#   yum install perl-Time-HiRes
[root@18c data]#   yum install perl-IO-Socket-SSL
然后再次安装
[root@18c data]# rpm -ivh percona-toolkit-3.0.12-1.el6.x86_64.rpm 
warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
Preparing...                ########################################### [100%]
   1:percona-toolkit        ########################################### [100%]

pt-online-schema-change工作原理:

1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。没有使用 --alter-foreign-keys-method 指定特定的值,该工具不予执行
2、创建一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。
3、创建触发器,用于记录从拷贝数据开始之后,对源数据表继续进行数据修改的操作记录下来,用于数据拷贝结束后,执行这些操作,保证数据不丢失。如果表中已经定义了触发器这个工具就不能工作了。
4、拷贝数据,从源数据表中拷贝数据到新表中。
5、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。
6、rename源数据表为old表,把新表rename为源表名,并将old表删除。
7、删除触发器

添加字段

pt-online-schema-change --alter="add c2 varchar(10) NOT NUll DEFAULT '' ;"  --execute --print --max-lag=5 D=test,t=test,u=root,p=root,S=/var/lib/mysql/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100

常用参数


--nocheck-replication-filters :不检查复制过滤器,建议启用。
--no-check-binlog-format : 不检查复制的binlog模式,要是binlog模式是ROW,则会报错。
--replicate-check-only :只显示不同步的信息。
--replicate= :把checksum的信息写入到指定表中,建议直接写到被检查的数据库当中。
--chunk-size=50   Number of rows to select for each chunk copied. Allowable suffixes are k, M, G.默认1000
--critical-load="Threads_running=600"  表示线程到600自动退出,默认值50
--max-load="Threads_running=100"   表示线程到100暂停pt操作,默认25
--progress=time,2   两秒打印一次日志输出
[root@localhost ~]# pt-online-schema-change --alter="add c4 varchar(10) NOT NUll DEFAULT '4' ;"  --execute --print --max-lag=5 D=test,t=test,u=root,p=root,S=/var/lib/mysql/mysql.sock --no-check-replication-filters --max-load="Threads_running=100" --critical-load="Threads_running=120" --charset=utf8   --chunk-size=100
No slaves found.  See --recursion-method if host localhost.localdomain has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
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 `test`.`test`...
Creating new table...
CREATE TABLE `test`.`_test_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `c1` varchar(255) DEFAULT NULL,
  `c2` varchar(10) NOT NULL DEFAULT '',
  `c3` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10944331 DEFAULT CHARSET=utf8mb4
Created new table test._test_new OK.
Altering new table...
ALTER TABLE `test`.`_test_new` add c4 varchar(10) NOT NUll DEFAULT '4' ;
Altered `test`.`_test_new` OK.
2021-08-22T09:10:24 Creating triggers...
2021-08-22T09:10:24 Created triggers OK.
2021-08-22T09:10:24 Copying approximately 9946924 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_test_new` (`id`, `c1`, `c2`, `c3`) SELECT `id`, `c1`, `c2`, `c3` FROM `test`.`test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 1932 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
Copying `test`.`test`:   8% 05:22 remain
Copying `test`.`test`:  16% 04:58 remain
Copying `test`.`test`:  25% 04:29 remain
Copying `test`.`test`:  32% 04:04 remain
Copying `test`.`test`:  41% 03:34 remain
Copying `test`.`test`:  49% 03:05 remain
Copying `test`.`test`:  57% 02:35 remain
Copying `test`.`test`:  65% 02:05 remain
Copying `test`.`test`:  73% 01:38 remain
Copying `test`.`test`:  81% 01:08 remain
Copying `test`.`test`:  89% 00:38 remain
Copying `test`.`test`:  97% 00:09 remain
2021-08-22T09:16:54 Copied rows OK.
2021-08-22T09:16:54 Analyzing new table...
2021-08-22T09:16:54 Swapping tables...
RENAME TABLE `test`.`test` TO `test`.`_test_old`, `test`.`_test_new` TO `test`.`test`
2021-08-22T09:16:54 Swapped original and new tables OK.
2021-08-22T09:16:54 Dropping old table...
DROP TABLE IF EXISTS `test`.`_test_old`
2021-08-22T09:16:54 Dropped old table `test`.`_test_old` OK.
2021-08-22T09:16:54 Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_del`
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_upd`
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_test_ins`
2021-08-22T09:16:54 Dropped triggers OK.
Successfully altered `test`.`test`.

--alter-foreign-keys-method 如何把外键引用到新表?需要特殊处理带有外键约束的表,以保证它们可以应用到新表.当重命名表的时候,外键关系会带到重命名后的表上。该工具有两种方法,可以自动找到子表,并修改约束关系。

auto: 在rebuild_constraints和drop_swap两种处理方式中选择一个。
rebuild_constraints:使用 ALTER TABLE语句先删除外键约束,然后再添加.如果子表很大的话,会导致长时间的阻塞。
drop_swap: 执行FOREIGN_KEY_CHECKS=0,禁止外键约束,删除原表,再重命名新表。这种方式很快,也不会产生阻塞,但是有风险:
1, 在删除原表和重命名新表的短时间内,表是不存在的,程序会返回错误。
2, 如果重命名表出现错误,也不能回滚了.因为原表已经被删除。
none: 类似"drop_swap"的处理方式,但是它不删除原表,并且外键关系会随着重命名转到老表上面
1
https://gitee.com/suluofan/homework.git
git@gitee.com:suluofan/homework.git
suluofan
homework
拉勾私教作业
master

搜索帮助