ITPub博客

首页 > 数据库 > MySQL > pt-online-schema-change工具的使用

pt-online-schema-change工具的使用

原创 MySQL 作者:oraclesuperman 时间:2015-09-06 10:15:36 0 删除 编辑

测试运行:

[root@newxywy ~]# pt-online-schema-change -usuperdba --ask-pass --port=3311 --alter "ADD COLUMN context text" D=test,t=t1 --socket=/tmp/mysql3311.sock  --print --dry-run
Enter MySQL password:
Starting a dry run.  `test`.`t1` will not be altered.  Specify --execute instead of --dry-run to alter the table.
Creating new table...
CREATE TABLE `test`.`_t1_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `context` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2490316 DEFAULT CHARSET=utf8
Created new table test._t1_new OK.
Altering new table...
ALTER TABLE `test`.`_t1_new` ADD COLUMN context text
Error altering new table `test`.`_t1_new`: DBD::mysql::db do failed: Duplicate column name 'context' [for Statement "ALTER TABLE `test`.`_t1_new` ADD COLUMN context text"] at /usr/local/bin/pt-online-schema-change line 5438, line 1.

Dropping new table...
DROP TABLE IF EXISTS `test`.`_t1_new`;
Dropped new table OK.
Dry run complete.  `test`.`t1` was not altered.


正式运行:

[root@newxywy ~]# pt-online-schema-change -usuperdba --ask-pass --port=3311 --alter "ADD COLUMN context text" D=test,t=t1 --socket=/tmp/mysql3311.sock  --print --execute
Enter MySQL password:
Altering `test`.`t1`...
Creating new table...
CREATE TABLE `test`.`_t1_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2490316 DEFAULT CHARSET=utf8
Created new table test._t1_new OK.
Altering new table...
ALTER TABLE `test`.`_t1_new` ADD COLUMN context text
Altered `test`.`_t1_new` OK.
Creating triggers...
CREATE TRIGGER `pt_osc_test_t1_del` AFTER DELETE ON `test`.`t1` FOR EACH ROW DELETE IGNORE FROM `test`.`_t1_new` WHERE `test`.`_t1_new`.`id` <=> OLD.`id`
CREATE TRIGGER `pt_osc_test_t1_upd` AFTER UPDATE ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
CREATE TRIGGER `pt_osc_test_t1_ins` AFTER INSERT ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
Created triggers OK.
Copying approximately 2097489 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_t1_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) /*pt-online-schema-change 44497 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
Copied rows OK.
Swapping tables...
RENAME TABLE `test`.`t1` TO `test`.`_t1_old`, `test`.`_t1_new` TO `test`.`t1`
Swapped original and new tables OK.
Dropping old table...
DROP TABLE IF EXISTS `test`.`_t1_old`
Dropped old table `test`.`_t1_old` OK.
Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_del`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_upd`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_ins`;
Dropped triggers OK.
Successfully altered `test`.`t1`.

[root@test01 ~]# pt-online-schema-change -uxxx --ask-pass --port=3328 --alter "DROP COLUMN context" D=test,t=t1 --socket=/tmp/mysql3328.sock  --print --execute
Enter MySQL password: 
No slaves found.  See --recursion-method if host test01.node.kddi.op.xywy.com has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
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`.`t1`...
Creating new table...
CREATE TABLE `test`.`_t1_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `context` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2949046 DEFAULT CHARSET=utf8
Created new table test._t1_new OK.
Altering new table...
ALTER TABLE `test`.`_t1_new` DROP COLUMN context
Altered `test`.`_t1_new` OK.
2015-11-06T10:31:52 Creating triggers...
CREATE TRIGGER `pt_osc_test_t1_del` AFTER DELETE ON `test`.`t1` FOR EACH ROW DELETE IGNORE FROM `test`.`_t1_new` WHERE `test`.`_t1_new`.`id` <=> OLD.`id`
CREATE TRIGGER `pt_osc_test_t1_upd` AFTER UPDATE ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
CREATE TRIGGER `pt_osc_test_t1_ins` AFTER INSERT ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
2015-11-06T10:31:52 Created triggers OK.
2015-11-06T10:31:52 Copying approximately 262735 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_t1_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 135838 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
2015-11-06T10:31:53 Copied rows OK.
2015-11-06T10:31:53 Swapping tables...
RENAME TABLE `test`.`t1` TO `test`.`_t1_old`, `test`.`_t1_new` TO `test`.`t1`
2015-11-06T10:31:53 Swapped original and new tables OK.
2015-11-06T10:31:53 Dropping old table...
DROP TABLE IF EXISTS `test`.`_t1_old`
2015-11-06T10:31:53 Dropped old table `test`.`_t1_old` OK.
2015-11-06T10:31:53 Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_del`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_upd`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_ins`;
2015-11-06T10:31:53 Dropped triggers OK.
Successfully altered `test`.`t1`.

添加索引:

[root@test01 ~]# pt-online-schema-change -usuperdba --ask-pass --port=3328 --alter "add key idx_na(name)" D=test,t=t1 --socket=/tmp/mysql3328.sock  --print --execute
Enter MySQL password: 
No slaves found.  See --recursion-method if host test01.node.kddi.op.xywy.com has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
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`.`t1`...
Creating new table...
CREATE TABLE `test`.`_t1_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2949046 DEFAULT CHARSET=utf8
Created new table test._t1_new OK.
Altering new table...
ALTER TABLE `test`.`_t1_new` add key idx_na(name)
Altered `test`.`_t1_new` OK.
2015-11-06T11:16:56 Creating triggers...
CREATE TRIGGER `pt_osc_test_t1_del` AFTER DELETE ON `test`.`t1` FOR EACH ROW DELETE IGNORE FROM `test`.`_t1_new` WHERE `test`.`_t1_new`.`id` <=> OLD.`id`
CREATE TRIGGER `pt_osc_test_t1_upd` AFTER UPDATE ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
CREATE TRIGGER `pt_osc_test_t1_ins` AFTER INSERT ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
2015-11-06T11:16:56 Created triggers OK.
2015-11-06T11:16:56 Copying approximately 262735 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_t1_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 6875 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
2015-11-06T11:16:58 Copied rows OK.
2015-11-06T11:16:58 Swapping tables...
RENAME TABLE `test`.`t1` TO `test`.`_t1_old`, `test`.`_t1_new` TO `test`.`t1`
2015-11-06T11:16:58 Swapped original and new tables OK.
2015-11-06T11:16:58 Dropping old table...
DROP TABLE IF EXISTS `test`.`_t1_old`
2015-11-06T11:16:58 Dropped old table `test`.`_t1_old` OK.
2015-11-06T11:16:58 Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_del`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_upd`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_ins`;
2015-11-06T11:16:58 Dropped triggers OK.
Successfully altered `test`.`t1`.


删除索引:

[root@test01 ~]# pt-online-schema-change -usuperdba --ask-pass --port=3328 --alter "drop key idx_na" D=test,t=t1 --socket=/tmp/mysql3328.sock  --print --execute
Enter MySQL password: 
No slaves found.  See --recursion-method if host test01.node.kddi.op.xywy.com has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
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`.`t1`...
Creating new table...
CREATE TABLE `test`.`_t1_new` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_na` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=2949046 DEFAULT CHARSET=utf8
Created new table test._t1_new OK.
Altering new table...
ALTER TABLE `test`.`_t1_new` drop key idx_na
Altered `test`.`_t1_new` OK.
2015-11-06T11:25:36 Creating triggers...
CREATE TRIGGER `pt_osc_test_t1_del` AFTER DELETE ON `test`.`t1` FOR EACH ROW DELETE IGNORE FROM `test`.`_t1_new` WHERE `test`.`_t1_new`.`id` <=> OLD.`id`
CREATE TRIGGER `pt_osc_test_t1_upd` AFTER UPDATE ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
CREATE TRIGGER `pt_osc_test_t1_ins` AFTER INSERT ON `test`.`t1` FOR EACH ROW REPLACE INTO `test`.`_t1_new` (`id`, `name`) VALUES (NEW.`id`, NEW.`name`)
2015-11-06T11:25:36 Created triggers OK.
2015-11-06T11:25:36 Copying approximately 262735 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_t1_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 10735 copy nibble*/
SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
2015-11-06T11:25:38 Copied rows OK.
2015-11-06T11:25:38 Swapping tables...
RENAME TABLE `test`.`t1` TO `test`.`_t1_old`, `test`.`_t1_new` TO `test`.`t1`
2015-11-06T11:25:38 Swapped original and new tables OK.
2015-11-06T11:25:38 Dropping old table...
DROP TABLE IF EXISTS `test`.`_t1_old`
2015-11-06T11:25:38 Dropped old table `test`.`_t1_old` OK.
2015-11-06T11:25:38 Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_del`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_upd`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t1_ins`;
2015-11-06T11:25:38 Dropped triggers OK.
Successfully altered `test`.`t1`.




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

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

注册时间:2015-03-20

  • 博文量
    178
  • 访问量
    422834