ITPub博客

首页 > 数据库 > MySQL > pt-online-schema-change

pt-online-schema-change

MySQL 作者:笑平风 时间:2015-09-14 16:40:28 0 删除 编辑

背景:

      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的时候不会阻塞写和读取操作,客户端可以继续都和修改数据。注意执行这个工具的时候必须做好备份,操作之前最好详细读一下官方文档。

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

使用方法:

1.pt-online-schema-change [OPTIONS] DSN

具体说下 OPTIONS 的一些常用的参数:

01.<strong>--user:
02.</strong>-u,连接的用户名
03. 
04.<strong>--pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>:
05.</strong>-p,连接的密码
06. 
07.<strong>--database:
08.</strong>-D,连接的数据库
09. 
10.<strong>--port
11.</strong>-P,连接数据库的端口
12. 
13.<strong>--host:
14.</strong>-h,连接的主机地址
15. 
16.--socket:
17.-S,连接的套接字文件
18. 
19.--ask-pass
20.隐式输入连接MySQL的密码
21. 
22.<strong>--</strong><strong>charset</strong>
23.指定修改的字符集
24. 
25.--defaults-file
26.-F,读取配置文件
27. 
28.<strong>--</strong><strong>alter:</strong>
29.结构变更语句,不需要alter table关键字。可以指定多个更改,用逗号分隔。如下场景,需要注意:
30.不能用RENAME来重命名表。       
31.列不能通过先删除,再添加的方式进行重命名,不会将数据拷贝到新列。
32.如果加入的列非空而且没有默认值,则工具会失败。即其不会为你设置一个默认值,必须显示指定。
33.删除外键(drop foreign key constrain_name)时,需要指定名称_constraint_name,而不是原始的constraint_name。
34.如:CONSTRAINT `fk_foo` FOREIGN KEY (`foo_id`) REFERENCES `bar` (`foo_id`),需要指定:--alter "DROP FOREIGN KEY _fk_foo"
35. 
36.<strong>--alter-foreign-keys-</strong><strong>method</strong>
37.如何把外键引用到新表?需要特殊处理带有外键约束的表,以保证它们可以应用到新表.当重命名表的时候,外键关系会带到重命名后的表上。
38.该工具有两种方法,可以自动找到子表,并修改约束关系。
39.<strong>auto</strong>: 在rebuild_constraints和drop_swap两种处理方式中选择一个。
40.<strong>rebuild_constraints</strong>:使用 ALTER TABLE语句先删除外键约束,然后再添加.如果子表很大的话,会导致长时间的阻塞。
41.<strong>drop_swap</strong>: 执行FOREIGN_KEY_CHECKS=0,禁止外键约束,删除原表,再重命名新表。这种方式很快,也不会产生阻塞,但是有风险:
42.1, 在删除原表和重命名新表的短时间内,表是不存在的,程序会返回错误。
43.2, 如果重命名表出现错误,也不能回滚了.因为原表已经被删除。
44.<strong>none</strong>: 类似"drop_swap"的处理方式,但是它不删除原表,并且外键关系会随着重命名转到老表上面。
01.<strong>--[no]check-</strong><strong>alter</strong>
02.默认yes,语法解析。配合--dry-run 和 --print 一起运行,来检查是否有问题(change column,drop primary key)。
03. 
04.<strong>--max-</strong><strong>lag</strong>
05.默 认1s。每个chunk拷贝完成后,会查看所有复制Slave的延迟情况。要是延迟大于该值,则暂停复制数据,直到所有从的滞后小于这个值,使用 Seconds_Behind_Master。如果有任何从滞后超过此选项的值,则该工具将睡眠--check-interval指定的时间,再检查。如 果从被停止,将会永远等待,直到从开始同步,并且延迟小于该值。如果指定--check-slave-lag,该工具只检查该服务器的延迟,而不是所有服 务器。
06. 
07.--check-slave-lag
08.指定一个从库的DSN连接地址,如果从库超过--max-lag参数设置的值,就会暂停操作。
09. 
10.<strong>--recursion-</strong><strong>method</strong>
11.默认是show processlist,发现从的方法,也可以是host,但需要在从上指定report_host,通过show slave hosts来找到,可以指定none来不检查Slave。
1.METHOD       USES
2.===========  ==================
3.processlist  SHOW PROCESSLIST
4.hosts        SHOW SLAVE HOSTS
5.dsn=DSN      DSNs from a table
6.none         Do not find slaves
7.指定none则表示不在乎从的延迟。
01.--check-interval
02.默认是1。--max-lag检查的睡眠时间。
03. 
04.--[no]check-plan
05.默认yes。检查查询执行计划的安全性。
06. 
07.<strong>--[no]check-replication-</strong><strong>filters</strong>
08.默 认yes。如果工具检测到服务器选项中有任何复制相关的筛选,如指定binlog_ignore_db和replicate_do_db此类。发现有这样 的筛选,工具会报错且退出。因为如果更新的表Master上存在,而Slave上不存在,会导致复制的失败。使用–no-check- replication-filters选项来禁用该检查。
09. 
10.<strong>--[no]swap-</strong><strong>tables</strong>
11.默认yes。交换原始表和新表,除非你禁止--[no]drop-old-table。
12. 
13.<strong>--[no]drop-</strong><strong>triggers</strong>
14.默认yes,删除原表上的触发器。 --no-drop-triggers 会强制开启 --no-drop-old-table 即:不删除触发器就会强制不删除原表。
15. 
16.<strong>--new-table-</strong><strong>name</strong>
17.复制创建新表的名称,默认%T_new。
18. 
19.<strong>--[no]drop-new-</strong><strong>table</strong>
20.默认yes。删除新表,如果复制组织表失败。
21. 
22.<strong>--[no]drop-old-</strong><strong>table</strong>
23.默认yes。复制数据完成重命名之后,删除原表。如果有错误则会保留原表。
24. 
25.<strong>--max-</strong><strong>load</strong>
26.默认为Threads_running=25。 每个chunk拷贝完后,会检查SHOW GLOBAL STATUS的内容,检查指标是否超过了指定的阈值。如果超过,则先暂停。这里可以用逗号分隔,指定多个条件,每个条件格式: status指标=MAX_VALUE或者status指标:MAX_VALUE。如果不指定MAX_VALUE,那么工具会这只其为当前值的120%。
27. 
28.<strong>--critical-</strong><strong>load</strong>
29.默认为Threads_running=50。用法基本与--max-load类似,如果不指定MAX_VALUE,那么工具会这只其为当前值的200%。如果超过指定值,则工具直接退出,而不是暂停。
30. 
31.<strong>--default-</strong><strong>engine</strong>
32.默认情况下,新的表与原始表是相同的存储引擎,所以如果原来的表使用InnoDB的,那么新表将使用InnoDB的。在涉及复制某些情况下,很可能主从的存储引擎不一样。使用该选项会默认使用默认的存储引擎。
33. 
34.<strong>--set-</strong><strong>vars</strong>
35.设置MySQL变量,多个用逗号分割。默认该工具设置的是: wait_timeout=10000 innodb_lock_wait_timeout=1 lock_wait_timeout=60
36. 
37.--chunk-size-limit
38.当需要复制的块远大于设置的chunk-size大小,就不复制.默认值是4.0,一个没有主键或唯一索引的表,块大小就是不确定的。
39. 
40.--chunk-time
41.在chunk-time执行的时间内,动态调整chunk-size的大小,以适应服务器性能的变化,该参数设置为0,或者指定chunk-size,都可以禁止动态调整。
42. 
43.--chunk-size
44.指定块的大小,默认是1000行,可以添加k,M,G后缀.这个块的大小要尽量与--chunk-time匹配,如果明确指定这个选项,那么每个块就会指定行数的大小.
45. 
46.--[no]check-plan
47.默 认yes。为了安全,检查查询的执行计划.默认情况下,这个工具在执行查询之前会先EXPLAIN,以获取一次少量的数据,如果是不好的EXPLAIN, 那么会获取一次大量的数据,这个工具会多次执行EXPALIN,如果EXPLAIN不同的结果,那么就会认为这个查询是不安全的。 
48. 
49.<strong>--</strong><strong>statistics</strong>
50.打印出内部事件的数目,可以看到复制数据插入的数目。
51. 
52.<strong>--dry-</strong><strong>run</strong>
53.创建和修改新表,但不会创建触发器、复制数据、和替换原表。并不真正执行,可以看到生成的执行语句,了解其执行步骤与细节。--dry-run与--execute必须指定一个,二者相互排斥。和--print配合最佳。
54. 
55.<strong>--</strong><strong>execute</strong>
56.确定修改表,则指定该参数。真正执行。--dry-run与--execute必须指定一个,二者相互排斥。
57. 
58.<strong>--</strong><strong>print</strong>
59.打印SQL语句到标准输出。指定此选项可以让你看到该工具所执行的语句,和--dry-run配合最佳。
60. 
61.--progress
62.复制数据的时候打印进度报告,二部分组成:第一部分是百分比,第二部分是时间。
63. 
64.--quiet
65.-q,不把信息标准输出。

更多的参数信息请见官方文档。

测试:

测试一:查看其实现方式、原理

1.测试表:
2.CREATE TABLE `online_table` (
3.`id` int(11) NOT NULL,
4.`name` varchar(10) DEFAULT NULL,
5.`age` int(11) DEFAULT NULL
6.)engine = innodb default charset utf8;
01.zhoujy@zhoujy:~$ <strong>pt-online-schema-change --user=root --pass<a href="http://www.it165.net/edu/ebg/" target="_blank" class="keylink">word</a>=123456 --host=192.168.200.25  --alter "ADD COLUMN content text" D=test,t=online_table --print --dry-</strong><strong>run</strong>
02. 
03.Operation, tries, wait:
04.copy_rows, 10, 0.25
05.create_triggers, 10, 1
06.drop_triggers, 10, 1
07.swap_tables, 10, 1
08.update_foreign_keys, 10, 1
09.#需要指定--execute去执行
10.Starting a dry run.  `test`.`online_table` will not be altered.  Specify --execute instead of --dry-run to alter the table.
11.Creating new table...
12.#新表
13.CREATE TABLE `test`.`_online_table_new` (
14.`id` int(11) NOT NULL,
15.`name` varchar(10) DEFAULT NULL,
16.`age` int(11) DEFAULT NULL
17.) ENGINE=InnoDB DEFAULT CHARSET=utf8
18.Created new table test._online_table_new OK.
19.#修改新表
20.Altering new table...
21.ALTER TABLE `test`.`_online_table_new` ADD COLUMN content text
22.Altered `test`.`_online_table_new` OK.
23.#删除新表,因为命令没有进行复制,所以默认下会删除新表,除非指定:--no-drop-new-table
24.Dropping new table...
25.DROP TABLE IF EXISTS `test`.`_online_table_new`;
26.Dropped new table OK.
27.#命令执行完成,因为没有指定--execute,所以原表没有被修改
28.Dry run complete.  `test`.`online_table` was not altered.
29.#原表需要一个主键或则唯一索引,因为删除的触发器需要,否则数据不会被复制
30.The new table `test`.`_online_table_new` does not have a PRIMARY KEY or a unique index which is required for the DELETE trigger.

上面已经说明了该工具的实现方式,下面来执行看是否有效。先为原表添加主键:

1.root@192.168.200.25 : test 10:14:21>alter table online_table add primary key (id),modify id int not null auto_increment;
2.Query OK, 0 rows affected (0.28 sec)
01.zhoujy@zhoujy:~$ <strong>pt-online-schema-change</strong> <strong>--</strong><strong>user=root --password=123456 --host=192.168.200.25  --alter "ADD COLUMN content text" D=test,t=online_table --print --execute
02.</strong>
03.Operation, tries, wait:
04.copy_rows, 10, 0.25
05.create_triggers, 10, 1
06.drop_triggers, 10, 1
07.swap_tables, 10, 1
08.update_foreign_keys, 10, 1
09.Altering `test`.`online_table`...
10.#创建新表
11.Creating new table...
12.CREATE TABLE `test`.`_online_table_new` (
13.`id` int(11) NOT NULL AUTO_INCREMENT,
14.`name` varchar(10) DEFAULT NULL,
15.`age` int(11) DEFAULT NULL,
16.PRIMARY KEY (`id`)
17.) ENGINE=InnoDB DEFAULT CHARSET=utf8
18.Created new table test._online_table_new OK.
19.Altering new table...
20.#修改新表
21.ALTER TABLE `test`.`_online_table_new` ADD COLUMN content text
22.Altered `test`.`_online_table_new` OK.
23.Creating triggers...
24.#触发器创建
25.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`
26.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`)
27.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`)
28.Created triggers OK.
29.#执行数据复制的操作,原表数据插入到新表
30.Copying approximately 1 rows...
31.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*/
32.Copied rows OK.
33.#插入完毕后交换表
34.Swapping tables...
35.RENAME TABLE `test`.`online_table` TO `test`.`_online_table_old`, `test`.`_online_table_new` TO `test`.`online_table`
36.Swapped original and new tables OK.
37.#数据复制完毕之后,删除原表
38.Dropping old table...
39.DROP TABLE IF EXISTS `test`.`_online_table_old`
40.Dropped old table `test`.`_online_table_old` OK.
41.#删除触发器
42.Dropping triggers...
43.DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_del`;
44.DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_upd`;
45.DROP TRIGGER IF EXISTS `test`.`pt_osc_test_online_table_ins`;
46.Dropped triggers OK.
47.Successfully altered `test`.`online_table`.

上面指定了--execute,输出的信息里面也表示原表已经被修改成功并且记录了很详细的操作信息,查看表是否已经被修改:

01.root@192.168.200.25 : test 10:15:12>desc online_table;
02.+---------+-------------+------+-----+---------+----------------+
03.| Field   | Type        | Null | Key | Default | Extra          |
04.+---------+-------------+------+-----+---------+----------------+
05.| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
06.| name    | varchar(10) | YES  |     | NULL    |                |
07.| age     | int(11)     | YES  |     | NULL    |                |
08.| content | text        | YES  |     | NULL    |                |
09.+---------+-------------+------+-----+---------+----------------+
10.4 rows in set (0.00 sec)

确定表已经添加了字段。通过上面的测试,已经基本了解该工具的工作原理了,下面开始测试一些正式场景下的使用方法。

测试二:主从环境的基本操作

1.测试表
2.zjy@192.168.200.111 : crm_production 11:25:48>desc tmp_test;
3.+-------+-------------+------+-----+---------+----------------+
4.| Field | Type        | Null | Key | Default | Extra          |
5.+-------+-------------+------+-----+---------+----------------+
6.| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
7.| name  | varchar(10) | YES  |     | NULL    |                |
8.+-------+-------------+------+-----+---------+----------------+
1.zhoujy@zhoujy:~$<strong> 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 --</strong><strong>execute</strong>
2.Enter MySQL password:
3.#报错,因为该工具在检测到服务器选项中有任何复制相关的筛选会退出,需要指定:--no-check-replication-filters
4.Replication filters are set on these hosts:
5.database2
6.replicate_do_db = crm_production
7.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.</stdin>

加上参数:--no-check-replication-filters

01.zhoujy@zhoujy:~$ <strong>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 --</strong><strong>execute</strong>
02.Enter MySQL password:
03. 
04.Operation, tries, wait:
05.copy_rows, 10, 0.25
06.create_triggers, 10, 1
07.drop_triggers, 10, 1
08.swap_tables, 10, 1
09.update_foreign_keys, 10, 1
10.Altering `crm_production`.`tmp_test`...
11.Creating new table...
12.CREATE TABLE `crm_production`.`_tmp_test_new` (
13.`id` int(11) NOT NULL AUTO_INCREMENT,
14.`name` varchar(10) DEFAULT NULL,
15.PRIMARY KEY (`id`)
16.) ENGINE=InnoDB DEFAULT CHARSET=utf8
17.Created new table crm_production._tmp_test_new OK.
18.Altering new table...
19.ALTER TABLE `crm_production`.`_tmp_test_new` ADD COLUMN content text
20.Altered `crm_production`.`_tmp_test_new` OK.
21.Creating triggers...
22.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`
23.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`)
24.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`)
25.Created triggers OK.
26.Copying approximately 1 rows...
27.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*/
28.Copied rows OK.
29.Swapping tables...
30.RENAME TABLE `crm_production`.`tmp_test` TO `crm_production`.`_tmp_test_old`, `crm_production`.`_tmp_test_new` TO `crm_production`.`tmp_test`
31.Swapped original and new tables OK.
32.Dropping old table...
33.DROP TABLE IF EXISTS `crm_production`.`_tmp_test_old`
34.Dropped old table `crm_production`.`_tmp_test_old` OK.
35.Dropping triggers...
36.DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_del`;
37.DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_upd`;
38.DROP TRIGGER IF EXISTS `crm_production`.`pt_osc_crm_production_tmp_test_ins`;
39.Dropped triggers OK.
40.Successfully altered `crm_production`.`tmp_test`.

已经修改:

1.zjy@192.168.200.111 : crm_production 11:15:39>desc tmp_test;
2.+---------+-------------+------+-----+---------+----------------+
3.| Field   | Type        | Null | Key | Default | Extra          |
4.+---------+-------------+------+-----+---------+----------------+
5.| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
6.| name    | varchar(10) | YES  |     | NULL    |                |
7.| content | text        | YES  |     | NULL    |                |
8.+---------+-------------+------+-----+---------+----------------+

除了add column,也可以modify column,drop column,对于change column 则需要指定:--no-check-alter

测试三:主外键表的基本操作

1.测试表(只能是INNODB表)
2.create table tt(id int not null auto_increment,name varchar(10),primary key(id))engine =innodb default charset utf8;
3.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;
4.alter table xx add foreign key fk_xx_tt_id(tt_id) references tt(id);
01.zhoujy@zhoujy:~$ <strong>pt-online-schema-change</strong> <strong>--user=root --password=123456 --host=192.168.200.25  --alter "ADD COLUMN content text" D=aaa,t=tt --no-check-replication-filters --print --</strong><strong>execute
02. 
03. 
04.</strong>Operation, tries, wait:
05.copy_rows, 10, 0.25
06.create_triggers, 10, 1
07.drop_triggers, 10, 1
08.swap_tables, 10, 1
09.update_foreign_keys, 10, 1
10.Child tables:
11.`aaa`.`xx` (approx. 3 rows)
12.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。

01.zhoujy@zhoujy:~$ <strong>pt-online-schema-change</strong> <strong>--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 --</strong><strong>execute
02. 
03.</strong>Operation, tries, wait:
04.copy_rows, 10, 0.25
05.create_triggers, 10, 1
06.drop_triggers, 10, 1
07.swap_tables, 10, 1
08.update_foreign_keys, 10, 1
09.#子表
10.Child tables:
11.`aaa`.`xx` (approx. 3 rows)
12.Will automatically choose the method to update foreign keys.
13.Altering `aaa`.`tt`...
14.#创建新表
15.Creating new table...
16.CREATE TABLE `aaa`.`_tt_new` (
17.`id` int(11) NOT NULL AUTO_INCREMENT,
18.`name` varchar(10) DEFAULT NULL,
19.PRIMARY KEY (`id`)
20.) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
21.Created new table aaa._tt_new OK.
22.#修改新表
23.Altering new table...
24.ALTER TABLE `aaa`.`_tt_new` ADD COLUMN content text
25.Altered `aaa`.`_tt_new` OK.
26.Creating triggers...
27.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`
28.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`)
29.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`)
30.Created triggers OK.
31.#复制数据
32.Copying approximately 3 rows...
33.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*/
34.Copied rows OK.
35.#处理外键的方式,选择的是auto,会根据情况进行选择:重建或则禁用外键检测。
36.Max rows for the rebuild_constraints method: 17898
37.Determining the method to update foreign keys...
38.`aaa`.`xx`: 3 rows; can use rebuild_constraints
39.#交换表
40.Swapping tables...
41.RENAME TABLE `aaa`.`tt` TO `aaa`.`_tt_old`, `aaa`.`_tt_new` TO `aaa`.`tt`
42.Swapped original and new tables OK.
43.#重建外键
44.Rebuilding foreign key constraints...
45.ALTER TABLE `aaa`.`xx` DROP FOREIGN KEY `xx_ibfk_1`, ADD CONSTRAINT `_xx_ibfk_1` FOREIGN KEY (`tt_id`) REFERENCES `aaa`.`tt` (`id`)
46.Rebuilt foreign key constraints OK.
47.Dropping old table...
48.DROP TABLE IF EXISTS `aaa`.`_tt_old`
49.Dropped old table `aaa`.`_tt_old` OK.
50.Dropping triggers...
51.DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_del`;
52.DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_upd`;
53.DROP TRIGGER IF EXISTS `aaa`.`pt_osc_aaa_tt_ins`;
54.Dropped triggers OK.
55.Successfully altered `aaa`.`tt`.

对可靠性要求不高可以用auto模式更新,要是可靠性要求高则需要用rebuild_constraints模式。即:

1.--alter-foreign-keys-method=<strong>rebuild_constraints</strong>

其他:
上面的测试都是把原表删除了,要是不删除原表则: --no-drop-old-table,这样会让原表(_test_binlog_old)保留。

1.<strong> pt-online-schema-change</strong> <strong>--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</strong>

要是在线上环境上添加字段,但又不想影响到服务,可以用参数:--max-load 去执行该工具,默认是 Threads_running=25,即当前有这么多线程在运行的时候就暂停数据的复制,等少于该值则继续复制数据到新表:

1.<strong>pt-online-schema-change</strong> --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 <strong>--max-load=Threads_running=2</strong> --statistics --print --execute

暂停的时候标准输出里面会有:

Pausing because Threads_running=2。等到运行的线程数小于给定的值,则就继续复制数据,直到完成。

总结:
       1、当业务量较大时,修改操作会等待没有数据修改后,执行最后的rename操作。因此,在修改表结构时,应该尽量选择在业务相对空闲时,至少修改表上的数据操作较低时,执行较为妥当。
       2、如果对外键表操作时,四种外键操作类型需要根据表的数据量和可靠程度,进行选择。处于可靠性的原因,尽量使用rebuild_constraints类型,如果没有可靠性要求,可以使用auto类型。
       3、由于可能存在一定的风险,在操作之前,建议对数据表进行备份,可以使得操作更安全、可靠。

       使用该工具的前提是处理的表需要有主键或则唯一索引。当处理有外键的表时,需要加--alter-foreign-keys-method参数,值可以根 据情况设置。当是主从环境,不在乎从的延迟,则需要加--recursion-method=none参数。当需要尽可能的对服务产生小的影响,则需要加 上--max-load参数。

      通过上面的测试,总结下该工具的使用方法:

1.表:
2.CREATE TABLE `tmp_test` (
3.`id` int(11) NOT NULL AUTO_INCREMENT,
4.`name` varchar(10) DEFAULT NULL,
5.`age` int(11) DEFAULT NULL,
6.PRIMARY KEY (`id`)
7.) ENGINE=InnoDB DEFAULT CHARSET=utf8

1,增加字段:

1.<strong>pt-online-schema-change</strong> --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,删除字段:

1.<strong>pt-online-schema-change</strong> --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,修改字段:

1.<strong>pt-online-schema-change</strong> --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,字段改名:

1.<strong>pt-online-schema-change</strong> --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,增加索引:

1.<strong>pt-online-schema-change</strong> --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,删除索引:

1.<strong>pt-online-schema-change</strong> --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

 使用方法就介绍到这里,更多的信息见官方文档。在执行过程中,是否会锁住其他操作,大家可以自行测试。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29798713/viewspace-1799254/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-08-09

  • 博文量
    75
  • 访问量
    194550