Mysql5.6 借助工具pt-online-schema-change在线DDL(还有加索引)

   MySQL在线ddl会阻塞所有insert,可以使用percona-toolkit中的pt-online-schema-change工具来对表进行online。

D=tpcctest 数据库名

t=customer 表名

[root@localhost bin]# ./pt-online-schema-change -u root -h127.0.0.1 -proot  --alter='add column col int ' --execute D=tpcctest,t=customer 

No slaves found.  See --recursion-method if host localhost.localdomain 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 `tpcctest`.`customer`...
Creating new table...
Created new table tpcctest._customer_new OK.
Altering new table...
Altered `tpcctest`.`_customer_new` OK.
2017-06-12T01:05:03 Creating triggers...
2017-06-12T01:05:03 Created triggers OK.
2017-06-12T01:05:03 Copying approximately 57629 rows...
2017-06-12T01:05:08 Copied rows OK.
2017-06-12T01:05:08 Swapping tables...
2017-06-12T01:05:08 Swapped original and new tables OK.
2017-06-12T01:05:08 Dropping old table...
2017-06-12T01:05:08 Dropped old table `tpcctest`.`_customer_old` OK.
2017-06-12T01:05:08 Dropping triggers...
2017-06-12T01:05:08 Dropped triggers OK.

Successfully altered `tpcctest`.`customer`.

online的操作步骤是:

1、创建一个和原来表结构一样的临时表并ddl;
2、将原表数据写入到临时表中(通过insert ...select方式),并且在原表上创建触发器,如果原表有数据写入,通过触发器方式将新增的数据写入临时表中(前提该表之前没有触发器);
3、写入完后,重命名临时表和原表名称;
4、删除原表。

注:表一定要有主键。


mysql> create table t2(id int primary key,name int);

mysql> show create table t2;              
+-------+---------------------------------
| Table | Create Table                    
+-------+---------------------------------
| t2    | CREATE TABLE `t2` (             
  `id` int(11) NOT NULL,                  
  `name` int(11) DEFAULT NULL,            
  PRIMARY KEY (`id`)                      
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |    
+-------+------------------------------   

mysql> insert into t2 values(1,1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t2 values(2,2);
Query OK, 1 row affected (0.01 sec)


[root@localhost bin]# ./pt-online-schema-change --user=root -proot  --host=localhost --socket=/opt/mysql/mysqldata/3306/mysql.sock --alter "add INDEX idx_t2_id(id)" D=test,t=t2  --execute
No slaves found.  See --recursion-method if host localhost.localdomain 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`.`t2`...
Creating new table...
Created new table test._t2_new OK.
Altering new table...
Altered `test`.`_t2_new` OK.
2017-06-20T01:39:24 Creating triggers...
2017-06-20T01:39:24 Created triggers OK.
2017-06-20T01:39:24 Copying approximately 2 rows...
2017-06-20T01:39:24 Copied rows OK.
2017-06-20T01:39:24 Swapping tables...
2017-06-20T01:39:24 Swapped original and new tables OK.
2017-06-20T01:39:24 Dropping old table...
2017-06-20T01:39:24 Dropped old table `test`.`_t2_old` OK.
2017-06-20T01:39:24 Dropping triggers...
2017-06-20T01:39:24 Dropped triggers OK.
Successfully altered `test`.`t2`.


mysql> show create table t2;             
+-------+--------------------------------
| Table | Create Table                   
+-------+--------------------------------
| t2    | CREATE TABLE `t2` (            
  `id` int(11) NOT NULL,                 
  `name` int(11) DEFAULT NULL,           
  PRIMARY KEY (`id`),                    
  KEY `idx_t2_id` (`id`)                 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |   
+-------+--------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值