percona-toolkit 之 【pt-online-schema-change】说明

背景:

      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、删除触发器。

使用方法:

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,不把信息标准输出。

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

测试:

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

测试表:
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。等到运行的线程数小于给定的值,则就继续复制数据,直到完成。

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

       使用该工具的前提是处理的表需要有主键或则唯一索引。当处理有外键的表时,需要加--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

http://blog.chinaunix.net/uid-28437434-id-3977100.html

http://blog.chinaunix.net/uid-26896862-id-3476953.html

 

posted @ 2013-12-26 16:15  jyzhou  阅读(9401)  评论(2编辑  收藏  举报