背景:html
MySQL 大字段的DDL操做:加减字段、索引、修改字段属性等,在5.1以前都是很是耗时耗力的,特别是会对MySQL服务产生影响。在5.1以后随着Plugin Innodb的出如今线加索引的提升了不少,可是还会影响(时间缩短了),主要是出现了MDL锁。不过5.6能够避免上面的状况,但目前大部分在用的版本都是5.6以前的,因此DDL操做一直是运维人员“头疼"的事。那如何在不锁表的状况下安全快速地更新表结构?如今来讲明下percona-toolkit 的 pt-online-schema-change 的使用说明,能够很好的解决上述的问题。数据库
工做原理:安全
模仿MySQL的alter,但不一样的是在alter操做更改表结构的时候不用锁定表,也就是说执行alter的时候不会阻塞写和读取操做,客户端能够继续都和修改数据。注意执行这个工具的时候必须作好备份,操做以前最好详细读一下官方文档。服务器
一、若是存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,作相应设置的处理。没有使用 --alter-foreign-keys-method 指定特定的值,该工具不予执行
二、建立一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。
三、建立触发器,用于记录从拷贝数据开始以后,对源数据表继续进行数据修改的操做记录下来,用于数据拷贝结束后,执行这些操做,保证数据不会丢失。若是表中已经定义了触发器这个工具就不能工做了。
四、拷贝数据,从源数据表中拷贝数据到新表中。
五、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。
六、rename源数据表为old表,把新表rename为源表名,并将old表删除。
七、删除触发器。多线程
使用方法:app
pt-online-schema-change [OPTIONS] DSN
具体说下 OPTIONS 的一些经常使用的参数:运维
--user: -u,链接的用户名 --password: -p,链接的密码 --database: -D,链接的数据库 --port -P,链接数据库的端口 --host: -h,链接的主机地址 --socket: -S,链接的套接字文件 --ask-pass 隐式输入链接MySQL的密码 --charset 指定修改的字符集 --defaults-file -F,读取配置文件 --alter: 结构变动语句,不须要alter table关键字。能够指定多个更改,用逗号分隔。以下场景,须要注意: 不能用RENAME来重命名表。 列不能经过先删除,再添加的方式进行重命名,不会将数据拷贝到新列。 若是加入的列非空并且没有默认值,则工具会失败。即其不会为你设置一个默认值,必须显示指定。 删除外键(drop foreign key constrain_name)时,须要指定名称_constraint_name,而不是原始的constraint_name。 如:CONSTRAINT `fk_foo` FOREIGN KEY (`foo_id`) REFERENCES `bar` (`foo_id`),须要指定:--alter "DROP FOREIGN KEY _fk_foo" --alter-foreign-keys-method 如何把外键引用到新表?须要特殊处理带有外键约束的表,以保证它们能够应用到新表.当重命名表的时候,外键关系会带到重命名后的表上。
该工具备两种方法,能够自动找到子表,并修改约束关系。
auto: 在rebuild_constraints和drop_swap两种处理方式中选择一个。
rebuild_constraints:使用 ALTER TABLE语句先删除外键约束,而后再添加.若是子表很大的话,会致使长时间的阻塞。
drop_swap: 执行FOREIGN_KEY_CHECKS=0,禁止外键约束,删除原表,再重命名新表。这种方式很快,也不会产生阻塞,可是有风险:
1, 在删除原表和重命名新表的短期内,表是不存在的,程序会返回错误。
2, 若是重命名表出现错误,也不能回滚了.由于原表已经被删除。
none: 相似"drop_swap"的处理方式,可是它不删除原表,而且外键关系会随着重命名转到老表上面。
--[no]check-alter 默认yes,语法解析。配合--dry-run 和 --print 一块儿运行,来检查是否有问题(change column,drop primary key)。 --max-lag 默认1s。每一个chunk拷贝完成后,会查看全部复制Slave的延迟状况。要是延迟大于该值,则暂停复制数据,直到全部从的滞后小于这个值,使用Seconds_Behind_Master。若是有任何从滞后超过此选项的值,则该工具将睡眠--check-interval指定的时间,再检查。若是从被中止,将会永远等待,直到从开始同步,而且延迟小于该值。若是指定--check-slave-lag,该工具只检查该服务器的延迟,而不是全部服务器。
--check-slave-lag
指定一个从库的DSN链接地址,若是从库超过--max-lag参数设置的值,就会暂停操做。
--recursion-method 默认是show processlist,发现从的方法,也能够是host,但须要在从上指定report_host,经过show slave hosts来找到,能够指定none来不检查Slave。
METHOD USES =========== ================== processlist SHOW PROCESSLIST hosts SHOW SLAVE HOSTS dsn=DSN DSNs from a table none Do not find slaves
指定none则表示不在意从的延迟。
--check-interval
默认是1。--max-lag检查的睡眠时间。
--[no]check-plan
默认yes。检查查询执行计划的安全性。
--[no]check-replication-filters
默认yes。若是工具检测到服务器选项中有任何复制相关的筛选,如指定binlog_ignore_db和replicate_do_db此类。发现有这样的筛选,工具会报错且退出。由于若是更新的表Master上存在,而Slave上不存在,会致使复制的失败。使用–no-check-replication-filters选项来禁用该检查。
--[no]swap-tables
默认yes。交换原始表和新表,除非你禁止--[no]drop-old-table。
--[no]drop-triggers
默认yes,删除原表上的触发器。 --no-drop-triggers 会强制开启 --no-drop-old-table 即:不删除触发器就会强制不删除原表。
--new-table-name
复制建立新表的名称,默认%T_new。
--[no]drop-new-table
默认yes。删除新表,若是复制组织表失败。
--[no]drop-old-table
默认yes。复制数据完成重命名以后,删除原表。若是有错误则会保留原表。
--max-load
默认为Threads_running=25。每一个chunk拷贝完后,会检查SHOW GLOBAL STATUS的内容,检查指标是否超过了指定的阈值。若是超过,则先暂停。这里能够用逗号分隔,指定多个条件,每一个条件格式: status指标=MAX_VALUE或者status指标:MAX_VALUE。若是不指定MAX_VALUE,那么工具会这只其为当前值的120%。
--critical-load
默认为Threads_running=50。用法基本与--max-load相似,若是不指定MAX_VALUE,那么工具会这只其为当前值的200%。若是超过指定值,则工具直接退出,而不是暂停。
--default-engine
默认状况下,新的表与原始表是相同的存储引擎,因此若是原来的表使用InnoDB的,那么新表将使用InnoDB的。在涉及复制某些状况下,极可能主从的存储引擎不同。使用该选项会默认使用默认的存储引擎。
--set-vars
设置MySQL变量,多个用逗号分割。默认该工具设置的是: wait_timeout=10000 innodb_lock_wait_timeout=1 lock_wait_timeout=60
--chunk-size-limit
当须要复制的块远大于设置的chunk-size大小,就不复制.默认值是4.0,一个没有主键或惟一索引的表,块大小就是不肯定的。
--chunk-time
在chunk-time执行的时间内,动态调整chunk-size的大小,以适应服务器性能的变化,该参数设置为0,或者指定chunk-size,均可以禁止动态调整。
--chunk-size 指定块的大小,默认是1000行,能够添加k,M,G后缀.这个块的大小要尽可能与--chunk-time匹配,若是明确指定这个选项,那么每一个块就会指定行数的大小.
--[no]check-plan 默认yes。为了安全,检查查询的执行计划.默认状况下,这个工具在执行查询以前会先EXPLAIN,以获取一次少许的数据,若是是很差的EXPLAIN,那么会获取一次大量的数据,这个工具会屡次执行EXPALIN,若是EXPLAIN不一样的结果,那么就会认为这个查询是不安全的。
--statistics 打印出内部事件的数目,能够看到复制数据插入的数目。 --dry-run 建立和修改新表,但不会建立触发器、复制数据、和替换原表。并不真正执行,能够看到生成的执行语句,了解其执行步骤与细节。--dry-run与--execute必须指定一个,两者相互排斥。和--print配合最佳。 --execute 肯定修改表,则指定该参数。真正执行。--dry-run与--execute必须指定一个,两者相互排斥。 --print 打印SQL语句到标准输出。指定此选项可让你看到该工具所执行的语句,和--dry-run配合最佳。 --progress 复制数据的时候打印进度报告,二部分组成:第一部分是百分比,第二部分是时间。 --quiet -q,不把信息标准输出。
更多的参数信息请见官方文档。socket
测试:工具
测试一:查看其实现方式、原理性能
测试表:
CREATE TABLE `online_table` ( `id` int(11) NOT NULL, `name` varchar(10) DEFAULT NULL, `age` int(11) DEFAULT NULL )engine = innodb default charset utf8;
zhoujy@zhoujy:~$ pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN content text" D=test,t=online_table --print --dry-run Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1
#须要指定--execute去执行 Starting a dry run. `test`.`online_table` will not be altered. Specify --execute instead of --dry-run to alter the table. Creating new table...
#新表 CREATE TABLE `test`.`_online_table_new` ( `id` int(11) NOT NULL, `name` varchar(10) DEFAULT NULL, `age` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Created new table test._online_table_new OK.
#修改新表 Altering new table... ALTER TABLE `test`.`_online_table_new` ADD COLUMN content text Altered `test`.`_online_table_new` OK.
#删除新表,由于命令没有进行复制,因此默认下会删除新表,除非指定:--no-drop-new-table Dropping new table... DROP TABLE IF EXISTS `test`.`_online_table_new`; Dropped new table OK.
#命令执行完成,由于没有指定--execute,因此原表没有被修改 Dry run complete. `test`.`online_table` was not altered.
#原表须要一个主键或则惟一索引,由于删除的触发器须要,不然数据不会被复制 The new table `test`.`_online_table_new` does not have a PRIMARY KEY or a unique index which is required for the DELETE trigger.
上面已经说明了该工具的实现方式,下面来执行看是否有效。先为原表添加主键:
root@192.168.200.25 : test 10:14:21>alter table online_table add primary key (id),modify id int not null auto_increment; Query OK, 0 rows affected (0.28 sec)
zhoujy@zhoujy:~$ pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN content text" D=test,t=online_table --print --execute
Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`online_table`...
#建立新表 Creating new table... CREATE TABLE `test`.`_online_table_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Created new table test._online_table_new OK. Altering new table...
#修改新表 ALTER TABLE `test`.`_online_table_new` ADD COLUMN content text Altered `test`.`_online_table_new` OK. Creating triggers...
#触发器建立 CREATE TRIGGER `pt_osc_test_online_table_del` AFTER DELETE ON `test`.`online_table` FOR EACH ROW DELETE IGNORE FROM `test`.`_online_table_new` WHERE `test`.`_online_table_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_test_online_table_upd` AFTER UPDATE ON `test`.`online_table` FOR EACH ROW REPLACE INTO `test`.`_online_table_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) CREATE TRIGGER `pt_osc_test_online_table_ins` AFTER INSERT ON `test`.`online_table` FOR EACH ROW REPLACE INTO `test`.`_online_table_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) Created triggers OK.
#执行数据复制的操做,原表数据插入到新表 Copying approximately 1 rows... INSERT LOW_PRIORITY IGNORE INTO `test`.`_online_table_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`online_table` LOCK IN SHARE MODE /*pt-online-schema-change 6167 copy table*/ Copied rows OK.
#插入完毕后交换表 Swapping tables... RENAME TABLE `test`.`online_table` TO `test`.`_online_table_old`, `test`.`_online_table_new` TO `test`.`online_table` Swapped original and new tables OK.
#数据复制完毕以后,删除原表 Dropping old table... DROP TABLE IF EXISTS `test`.`_online_table_old` Dropped old table `test`.`_online_table_old` OK.
#删除触发器 Dropping triggers... DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_del`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_upd`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_ins`; Dropped triggers OK. Successfully altered `test`.`online_table`.
上面指定了--execute,输出的信息里面也表示原表已经被修改为功而且记录了很详细的操做信息,查看表是否已经被修改:
root@192.168.200.25 : test 10:15:12>desc online_table; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | YES | | NULL | | | age | int(11) | YES | | NULL | | | content | text | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
肯定表已经添加了字段。经过上面的测试,已经基本了解该工具的工做原理了,下面开始测试一些正式场景下的使用方法。
测试二:主从环境的基本操做
测试表
zjy@192.168.200.111 : crm_production 11:25:48>desc tmp_test; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+
zhoujy@zhoujy:~$ pt-online-schema-change --user=zjy --host=192.168.200.111 --alter "ADD COLUMN content text" D=crm_production,t=tmp_test --ask-pass --print --execute Enter MySQL password: #报错,由于该工具在检测到服务器选项中有任何复制相关的筛选会退出,须要指定:--no-check-replication-filters Replication filters are set on these hosts: database2 replicate_do_db = crm_production Please read the --check-replication-filters documentation to learn how to solve this problem. at /usr/local/bin/pt-online-schema-change line 8015, <STDIN> line 2.
加上参数:--no-check-replication-filters
zhoujy@zhoujy:~$ pt-online-schema-change --user=zjy --host=192.168.200.111 --alter "ADD COLUMN content text" D=crm_production,t=tmp_test --ask-pass --no-check-replication-filters --print --execute Enter MySQL password: Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `crm_production`.`tmp_test`... Creating new table... CREATE TABLE `crm_production`.`_tmp_test_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Created new table crm_production._tmp_test_new OK. Altering new table... ALTER TABLE `crm_production`.`_tmp_test_new` ADD COLUMN content text Altered `crm_production`.`_tmp_test_new` OK. Creating triggers... CREATE TRIGGER `pt_osc_crm_production_tmp_test_del` AFTER DELETE ON `crm_production`.`tmp_test` FOR EACH ROW DELETE IGNORE FROM `crm_production`.`_tmp_test_new` WHERE `crm_production`.`_tmp_test_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_crm_production_tmp_test_upd` AFTER UPDATE ON `crm_production`.`tmp_test` FOR EACH ROW REPLACE INTO `crm_production`.`_tmp_test_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`) CREATE TRIGGER `pt_osc_crm_production_tmp_test_ins` AFTER INSERT ON `crm_production`.`tmp_test` FOR EACH ROW REPLACE INTO `crm_production`.`_tmp_test_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`) Created triggers OK. Copying approximately 1 rows... INSERT LOW_PRIORITY IGNORE INTO `crm_production`.`_tmp_test_new` (`id`, `name`) SELECT `id`, `name` FROM `crm_production`.`tmp_test` LOCK IN SHARE MODE /*pt-online-schema-change 6982 copy table*/ Copied rows OK. Swapping tables... RENAME TABLE `crm_production`.`tmp_test` TO `crm_production`.`_tmp_test_old`, `crm_production`.`_tmp_test_new` TO `crm_production`.`tmp_test` Swapped original and new tables OK. Dropping old table... DROP TABLE IF EXISTS `crm_production`.`_tmp_test_old` Dropped old table `crm_production`.`_tmp_test_old` OK. Dropping triggers... DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_del`; DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_upd`; DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_ins`; Dropped triggers OK. Successfully altered `crm_production`.`tmp_test`.
已经修改:
zjy@192.168.200.111 : crm_production 11:15:39>desc tmp_test; +---------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | YES | | NULL | | | content | text | YES | | NULL | | +---------+-------------+------+-----+---------+----------------+
除了add column,也能够modify column,drop column,对于change column 则须要指定:--no-check-alter
测试三:主外键表的基本操做
测试表(只能是INNODB表)
create table tt(id int not null auto_increment,name varchar(10),primary key(id))engine =innodb default charset utf8; create table xx(id int not null auto_increment,tt_id int not null,name varchar(10),primary key(id))engine =innodb default charset utf8; alter table xx add foreign key fk_xx_tt_id(tt_id) references tt(id);
zhoujy@zhoujy:~$ pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN content text" D=aaa,t=tt --no-check-replication-filters --print --execute
Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Child tables: `aaa`.`xx` (approx. 3 rows) You did not specify --alter-foreign-keys-method, but there are foreign keys that reference the table. Please read the tool's documentation carefully.
执行错误退出,提示须要指定:--alter-foreign-keys-method 参数来操做有外键的表。要是没有外键而加了参数的话会出现:
No foreign keys reference `aaa`.`xx`; ignoring --alter-foreign-keys-method。
zhoujy@zhoujy:~$ pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN content text" D=aaa,t=tt --no-check-replication-filters --alter-foreign-keys-method auto --print --execute
Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1
#子表 Child tables: `aaa`.`xx` (approx. 3 rows) Will automatically choose the method to update foreign keys. Altering `aaa`.`tt`...
#建立新表 Creating new table... CREATE TABLE `aaa`.`_tt_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 Created new table aaa._tt_new OK.
#修改新表 Altering new table... ALTER TABLE `aaa`.`_tt_new` ADD COLUMN content text Altered `aaa`.`_tt_new` OK. Creating triggers... CREATE TRIGGER `pt_osc_aaa_tt_del` AFTER DELETE ON `aaa`.`tt` FOR EACH ROW DELETE IGNORE FROM `aaa`.`_tt_new` WHERE `aaa`.`_tt_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_aaa_tt_upd` AFTER UPDATE ON `aaa`.`tt` FOR EACH ROW REPLACE INTO `aaa`.`_tt_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`) CREATE TRIGGER `pt_osc_aaa_tt_ins` AFTER INSERT ON `aaa`.`tt` FOR EACH ROW REPLACE INTO `aaa`.`_tt_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`) Created triggers OK.
#复制数据 Copying approximately 3 rows... INSERT LOW_PRIORITY IGNORE INTO `aaa`.`_tt_new` (`id`, `name`) SELECT `id`, `name` FROM `aaa`.`tt` LOCK IN SHARE MODE /*pt-online-schema-change 8969 copy table*/ Copied rows OK.
#处理外键的方式,选择的是auto,会根据状况进行选择:重建或则禁用外键检测。 Max rows for the rebuild_constraints method: 17898 Determining the method to update foreign keys... `aaa`.`xx`: 3 rows; can use rebuild_constraints
#交换表 Swapping tables... RENAME TABLE `aaa`.`tt` TO `aaa`.`_tt_old`, `aaa`.`_tt_new` TO `aaa`.`tt` Swapped original and new tables OK.
#重建外键 Rebuilding foreign key constraints... ALTER TABLE `aaa`.`xx` DROP FOREIGN KEY `xx_ibfk_1`, ADD CONSTRAINT `_xx_ibfk_1` FOREIGN KEY (`tt_id`) REFERENCES `aaa`.`tt` (`id`) Rebuilt foreign key constraints OK. Dropping old table... DROP TABLE IF EXISTS `aaa`.`_tt_old` Dropped old table `aaa`.`_tt_old` OK. Dropping triggers... DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_del`; DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_upd`; DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_ins`; Dropped triggers OK. Successfully altered `aaa`.`tt`.
对可靠性要求不高能够用auto模式更新,要是可靠性要求高则须要用rebuild_constraints模式。即:
--alter-foreign-keys-method=rebuild_constraints
其余:
上面的测试都是把原表删除了,要是不删除原表则: --no-drop-old-table,这样会让原表(_test_binlog_old)保留。
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN a text" D=aaa,t=test_binlog --no-check-replication-filters --no-drop-old-table --print --execute
要是在线上环境上添加字段,但又不想影响到服务,能够用参数:--max-load 去执行该工具,默认是 Threads_running=25,即当前有这么多线程在运行的时候就暂停数据的复制,等少于该值则继续复制数据到新表:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "add INDEX idx_address(address)" D=aaa,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --max-load=Threads_running=2 --statistics --print --execute
暂停的时候标准输出里面会有:
Pausing because Threads_running=2。等到运行的线程数小于给定的值,则就继续复制数据,直到完成。
总结:
一、当业务量较大时,修改操做会等待没有数据修改后,执行最后的rename操做。所以,在修改表结构时,应该尽可能选择在业务相对空闲时,至少修改表上的数据操做较低时,执行较为稳当。
二、若是对外键表操做时,四种外键操做类型须要根据表的数据量和可靠程度,进行选择。处于可靠性的缘由,尽可能使用rebuild_constraints类型,若是没有可靠性要求,可使用auto类型。
三、因为可能存在必定的风险,在操做以前,建议对数据表进行备份,可使得操做更安全、可靠。
使用该工具的前提是处理的表须要有主键或则惟一索引。当处理有外键的表时,须要加--alter-foreign-keys-method参数,值能够根据状况设置。当是主从环境,不在意从的延迟,则须要加--recursion-method=none参数。当须要尽量的对服务产生小的影响,则须要加上--max-load参数。
经过上面的测试,总结下该工具的使用方法:
表:
CREATE TABLE `tmp_test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(10) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
1,增长字段:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD COLUMN content text" D=aaa,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute
2,删除字段:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "DROP COLUMN content " D=aaa,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute
3,修改字段:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "MODIFY COLUMN age TINYINT NOT NULL DEFAULT 0" D=aaa,t=tmp_test --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execute
4,字段更名:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "CHANGE COLUMN age address varchar(30)" D=aaa,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --quiet --execut
5,增长索引:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "ADD INDEX idx_address(address)" D=aaa,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute
6,删除索引:
pt-online-schema-change --user=root --password=123456 --host=192.168.200.25 --alter "DROP INDEX idx_address" D=aaa,t=tmp_test --no-check-alter --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute
使用方法就介绍到这里,更多的信息见官方文档。在执行过程当中,是否会锁住其余操做,你们能够自行测试。
更多信息:
http://www.percona.com/doc/percona-toolkit/2.2/pt-online-schema-change.html