pt-online-schema-change原理及常见问题

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

 通过创建表的空副本来进行更改,根据需要对其进行修改,然后将原始表中的行复制到新表中。复制完成后,它将移开原来的表格并用新的表格替换它。默认情况下,它也会删除原始表。

数据复制过程是以小块数据执行的,这些数据块有所不同,试图使它们在特定的时间内执行(请参阅参考资料 --chunk-time)。该过程与其他工具(如pt-table-checksum)的工作方式非常相似。在复制期间对原始表中数据的任何修改都将反映在新表中,因为该工具会在原始表上创建触发器以更新新表中的相应行。触发器的使用意味着如果已经在表格中定义了任何触发器,该工具将不起作用。

当工具完成将数据复制到新表中时,它使用原子 操作来同时重命名原始表和新表。完成后,该工具将放弃原始表格。RENAME TABLE

外键使工具的操作复杂化并带来额外的风险。当外键引用表时,原子重命名原始表和新表的技术不起作用。在模式更改完成后,该工具必须更新外键以引用新表。该工具支持两种完成此操作的方法。您可以在文档中阅读更多关于此的信息 --alter-foreign-keys-method。

外键也会引起一些副作用。最终表与原始表具有相同的外键和索引(除非您在ALTER语句中指定的内容不同),但可以稍微更改对象的名称以避免在MySQL和InnoDB中发生对象名冲突。

为了安全起见,除非您指定--execute默认情况下未启用的选项,否则该工具不会修改表格 。该工具支持多种其他措施来防止不必要的负载或其他问题,包括自动检测副本,连接到它们以及使用以下安全检查:

在大多数情况下,该工具将拒绝操作,除非表中存在PRIMARY KEYUNIQUE INDEX。详情请参阅--alter。
如果它检测到复制过滤器,该工具将拒绝操作。详情请参阅 --[no]check-replication-filters。
如果观察到任何复制中延迟的副本,该工具会暂停数据复制操作。详情请参阅--max-lag。
如果它检测到服务器上的负载太多,该工具会暂停或中止其操作。请参阅--max-load并--critical-load了解详情。
该工具设置innodb_lock_wait_timeout=1和(对于MySQL 5.5和更新版本), lock_wait_timeout=60以便更有可能成为任何锁争用的受害者,并且不太可能中断其他事务。这些值可以通过指定进行更改--set-vars。
除非您指定,否则该工具会拒绝在外键约束引用它时更改该表--alter-foreign-keys-method。
该工具无法更改“Percona XtraDB集群”节点上的MyISAM表。

语法实例:

 pt-online-schema-change  --alter "ADD COLUMN c1 INT(1)"  D=test,t=t2d_chatscene,h=127.0.0.1 --print
增加多个列:
 pt-online-schema-change  --alter "ADD COLUMN c2 INT(1),ADD COLUMN c3 INT(1)"  D=test,t=t2d_chatscene,h=127.0.0.1 --print --execute

错误1:

[root@sh-ksy-v4-kf_master-11 ~]# pt-online-schema-change  --alter "ADD COLUMN userrank varchar(64) NULL  COMMENT 'userrank'"  D=kf,t=chatjs_statis,h=127.0.0.1 --print --execute  --charset=utf
........
........
N SHARE MODE /*pt-online-schema-change 10417 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `kf`.`chatjs_statis` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
Copying `kf`.`chatjs_statis`:   1% 43:39 remain
Copying `kf`.`chatjs_statis`:   2% 43:27 remain
Copying `kf`.`chatjs_statis`:   3% 43:22 remain
Copying `kf`.`chatjs_statis`:   4% 42:28 remain
Copying `kf`.`chatjs_statis`:   5% 41:11 remain
Copying `kf`.`chatjs_statis`:   6% 41:15 remain
Copying `kf`.`chatjs_statis`:   8% 39:34 remain
2018-04-09T23:20:04 Dropping triggers...
DROP TRIGGER IF EXISTS `kf`.`pt_osc_kf_chatjs_statis_del`
DROP TRIGGER IF EXISTS `kf`.`pt_osc_kf_chatjs_statis_upd`
DROP TRIGGER IF EXISTS `kf`.`pt_osc_kf_chatjs_statis_ins`
2018-04-09T23:20:04 Dropped triggers OK.
2018-04-09T23:20:04 Dropping new table...
DROP TABLE IF EXISTS `kf`.`_chatjs_statis_new`;
2018-04-09T23:20:06 Dropped new table OK.
`kf`.`chatjs_statis` was not altered.
2018-04-09T23:20:04 Error copying rows from `kf`.`chatjs_statis` to `kf`.`_chatjs_statis_new`: Threads_running=54 exceeds its critical threshold 50

错误:“ Error copying rows from kf.chatjs_statis to kf._chatjs_statis_new: Threads_running=54 exceeds its critical threshold 50”

提示为Threads_running超过了警告的阀值,而官方默认值是50,需要手动增加值得大小。添加参数:–critical-load=”Threads_running=200”

错误2:

root@sh-ksy-v4-kf_master-11 ~]# pt-online-schema-change  --alter "ADD COLUMN lastmsgtime BIGINT(32) NULL  COMMENT '最后一句话时间'"  D=kf,t=t2d_chatuser_chattimelist,h=127.0.0.1 --print --execute  --charset=utf8
1> Cannot connect to A=utf8,h=172.67.4.22
1> Cannot connect to A=utf8,h=172.67.4.12
No slaves found.  See --recursion-method if host sh-ksy-v4-master-11 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
The table `kf`.`t2d_chatuser_chattimelist` has triggers.  This tool needs to create its own triggers, so the table cannot already have triggers.

提示:so the table cannot already have triggers 触发器已经存在了

进入mysql数据库查看触发器是否存在

mysql> show triggers like '%t2d_chatuser_chattimelist%'\G;
*************************** 1. row ***************************
             Trigger: pt_osc_kf_t2d_chatuser_chattimelist_ins
               Event: INSERT
               Table: t2d_chatuser_chattimelist
           Statement: REPLACE INTO `kf`.`_t2d_chatuser_chattimelist_new` (`id`, `siteid`, `sessionid`, `userid`, `usertype`, `fristmsgtime`, `entertime`, `leavetime`, `timedif`, `settingid`)VALUES (NEW.`id`, NEW.`siteid`, NEW.`sessionid`, NEW.`userid`, NEW.`usertype`, NEW.`fristmsgtime`, NEW.`entertime`, NEW.`leavetime`, NEW.`timedif`, NEW.`settingid`)
              Timing: AFTER
             Created: NULL
            sql_mode: NO_AUTO_VALUE_ON_ZERO
             Definer: root@localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
*************************** 2. row ***************************
             Trigger: pt_osc_kf_t2d_chatuser_chattimelist_upd
               Event: UPDATE
               Table: t2d_chatuser_chattimelist
           Statement: BEGIN DELETE IGNORE FROM `kf`.`_t2d_chatuser_chattimelist_new` WHERE !(OLD.`id` <=> NEW.`id`) AND `kf`.`_t2d_chatuser_chattimelist_new`.`id` <=> OLD.`id`;REPLACE INTO `kf`.`_t2d_chatuser_chattimelist_new` (`id`, `siteid`, `sessionid`, `userid`, `usertype`, `fristmsgtime`, `entertime`, `leavetime`, `timedif`, `settingid`) VALUES (NEW.`id`, NEW.`siteid`, NEW.`sessionid`, NEW.`userid`, NEW.`usertype`, NEW.`fristmsgtime`, NEW.`entertime`, NEW.`leavetime`, NEW.`timedif`, NEW.`settingid`);END
              Timing: AFTER
             Created: NULL
            sql_mode: NO_AUTO_VALUE_ON_ZERO
             Definer: root@localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
*************************** 3. row ***************************
             Trigger: pt_osc_kf_t2d_chatuser_chattimelist_del
               Event: DELETE
               Table: t2d_chatuser_chattimelist
           Statement: DELETE IGNORE FROM `kf`.`_t2d_chatuser_chattimelist_new` WHERE `kf`.`_t2d_chatuser_chattimelist_new`.`id` <=> OLD.`id`
              Timing: AFTER
             Created: NULL
            sql_mode: NO_AUTO_VALUE_ON_ZERO
             Definer: root@localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
3 rows in set (0.01 sec)

ERROR:
No query specified

删除触发器

mysql> DROP TRIGGER pt_osc_kf_t2d_chatuser_chattimelist_ins;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TRIGGER pt_osc_kf_t2d_chatuser_chattimelist_upd;
Query OK, 0 rows affected (0.00 sec)

mysql> show triggers like '%t2d_chatuser_chattimelist%'\G;
Empty set (0.00 sec)

ERROR:
No query specified

mysql>

猜你喜欢

转载自blog.csdn.net/q936889811/article/details/79874892