背景:由于目前生产环境中大表比较多,这里选择了一个6000M的小表做一次在线测试。
环境:oot@storm-master-01:/root#uname -a
Linux storm-master-01 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
mysql版本 :
root@storm-master-01:/root#mysql -V
mysql Ver 14.14 Distrib 5.7.17-11, for Linux (x86_64) using 6.2
root@storm-master-01:/root#
表引擎:Innodb
一、pt-online-schema-change介绍
percona 公司提供的一款在线更新表的工具,更新过程不会锁表,也就是说操作alter的过程不会阻塞写和读取。即使如此,建议大家操作前还是先做好表备份。(备份工具推荐mydumper,这里不细说)
参数说明:
pt-online-schema-change [OPTIONS] DSN
options 可以自行查看 help,DNS 为你要操作的数据库和表。这里有两个参数需要介绍一下:
–dry-run
这个参数不建立触发器,不拷贝数据,也不会替换原表。只是创建和更改新表。
–execute
这个参数的作用和前面工作原理的介绍的一样,会建立触发器,来保证最新变更的数据会影响至新表。注意:如果不加这个参数,这个工具会在执行一些检查后退出。
注:操作的表必须有主键;否则报错:Cannot chunk the original table `houyi`.`ga`: There is no good index and the table is oversized. at ./pt-online-schema-change line 5353.
- wget percona.com/get/percona-toolkit.tar.gz
- https://www.percona.com/downloads/percona-toolkit/
- tar -zxvf percona-toolkit-2.2.6.tar.gz
- perl Makefile.PL
- make
- make test
- make install
No slaves found. See --recursion-method if host storm-master-01 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
together with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /root/perl5/bin/pt-online-schema-change line 6896.
*******************************************************************
Using the default of SSL_verify_mode of SSL_VERIFY_NONE for client
is deprecated! Please set SSL_verify_mode to SSL_VERIFY_PEER
together with SSL_ca_file|SSL_ca_path for verification.
If you really don't want to verify the certificate and keep the
connection open to Man-In-The-Middle attacks please set
SSL_verify_mode explicitly to SSL_VERIFY_NONE in your application.
*******************************************************************
at /root/perl5/bin/pt-online-schema-change line 6896.
# A software update is available:
# * The current version for Percona::Toolkit is 3.0.5
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 `collection`.`orgs`...
Creating new table...
Created new table collection._orgs_new OK.
Altering new table...
Altered `collection`.`_orgs_new` OK.
2018-01-03T14:38:25 Creating triggers...
2018-01-03T14:38:25 Created triggers OK.
2018-01-03T14:38:25 Copying approximately 59 rows...
2018-01-03T14:38:25 Copied rows OK.
2018-01-03T14:38:25 Analyzing new table...
2018-01-03T14:38:25 Swapping tables...
2018-01-03T14:38:25 Swapped original and new tables OK.
2018-01-03T14:38:25 Dropping old table...
2018-01-03T14:38:25 Dropped old table `collection`.`_orgs_old` OK.
2018-01-03T14:38:25 Dropping triggers...
2018-01-03T14:38:25 Dropped triggers OK.
Successfully altered `collection`.`orgs`.
root@storm-master-01:/root#
- --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.
数据库最昂贵的操作莫过于DDL操作,因为MySQL在修改表期间会阻塞任何读写操作的,基本上你的业务出于瘫痪状态了。对于庞大的可能历时好几个小时才完成,简直就是个恶梦,没法容忍的操作。
Percona开发了一系列工具Percona Toolkit包,其中有一工具pt-online-schema-change可以在线执行DDL操作,不会阻塞读写操作从而影响业务程序。当然啦,MySQL 5.6也增强了一些在线DDL功能。下面主要是说pt-online-schema-change在线更改表结构。
pt-online-schema-change原理
1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。
2、创建一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。
3、创建触发器,用于记录从拷贝数据开始之后,对源数据表继续进行数据修改的操作记录下来,用于数据拷贝结束后,执行这些操作,保证数据不会丢失。
4、拷贝数据,从源数据表中拷贝数据到新表中。
5、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。
6、rename源数据表为old表,把新表rename为源表名,并将old表删除。
7、删除触发器。
使用方法
参数列表
测试
1、当业务量较大时,修改操作会等待没有数据修改后,执行最后的rename操作。因此,在修改表结构时,应该尽量选择在业务相对空闲时,至少修改表上的数据操作较低时,执行较为妥当。
2、如果对外键表操作时,四种外键操作类型需要根据表的数据量和可靠程度,进行选择。处于可靠性的原因,尽量使用rebuild_constraints类型,如果没有可靠性要求,可以使用auto类型。
3、由于可能存在一定的风险,在操作之前,建议对数据表进行备份,可以使得操作更安全、可靠。
使用该工具的前提是处理的表需要有主键或则唯一索引。当处理有外键的表时,需要加--alter-foreign-keys-method参数,值可以根据情况设置。当是主从环境,不在乎从的延迟,则需要加--recursion-method=none参数。当需要尽可能的对服务产生小的影响,则需要加上--max-load参数。
添加字段
删除字段
修改字段
字段改名
增加索引
删除索引