MySQL Online DDL 工具之pt-online-schema-change
MySQL DDL:
DDL是一个令所有MySQL dDBA 诟病的一个功能,因为在MySQL中在对表进行dDDL时,会锁表,当表比较小比如小于1W行时,对前端影响较小,当时遇到千万级别的表,就会影响前端应用对表的写操作!
InnoDB引擎是通过以下步骤来进行DDL的:
1、按照原始表(original_table)的表结构和DDL语句,新建一个不可见的临时表(tmp_table)
2、在原表上加write lock,阻塞所有更新操作(insert、delete、update等)
3、执行insert into tmp_table select * from original_table
4、rename original_table和tmp_table,最后drop original_table
5、释放 write lock。
可以看见在InnoDB执行DDL的时候,原表是只能读不能写的。为此 perconal 推出一个工具 pt-online-schema-change ,其特点是修改过程中不会造成读写阻塞。
工作原理:
如果表有外键,除非使用 --alter-foreign-keys-method 指定特定的值,否则工具不予执行。
1 创建一个和你要执行 alter 操作的表一样的空表结构。
2 执行表结构修改,然后从原表中的数据到copy到 表结构修改后的表,
3 在原表上创建触发器将 copy 数据的过程中,在原表的更新操作 更新到新表.
注意:如果表中已经定义了触发器这个工具就不能工作了。
4 copy 完成以后,用rename table 新表代替原表,默认删除原表。
用法介绍:
pt-online-schema-change [OPTIONS] DSN
options 可以自行查看 help,DNS 为你要操作的数据库和表。这里有两个参数需要介绍一下:
--dry-run
这个参数不建立触发器,不拷贝数据,也不会替换原表。只是创建和更改新表。
--execute
这个参数的作用和前面工作原理的介绍的一样,会建立触发器,来保证最新变更的数据会影响至新表。注意:如果不加这个参数,这个工具会在执行一些检查后退出。
依赖条件:
操作的表必须有主键否则 报如下错误:
[root@hank-yoon ~]# pt-online-schema-change -u root -pyoon -h127.0.0.1 --alter='add column vname varchar(20)' --execute D=yoon,t=yoon
No slaves found. See --recursion-method if host hank-yoon.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 `yoon`.`yoon`...
Creating new table...
Created new table yoon._yoon_new OK.
Altering new table...
Altered `yoon`.`_yoon_new` OK.
2016-01-08T17:51:43 Dropping new table...
2016-01-08T17:51:43 Dropped new table OK.
`yoon`.`yoon` was not altered.
The new table `yoon`.`_yoon_new` does not have a PRIMARY KEY or a unique index which is required for the DELETE trigger.
字段vname没有添加成功!
mysql> show create table yoon\G
*************************** 1. row ***************************
Table: yoon
Create Table: CREATE TABLE `yoon` (
`actor_id` smallint(8) unsigned NOT NULL DEFAULT '0',
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8
添加主键:
mysql> alter table yoon modify actor_id smallint(8) unsigned primary key;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
[root@hank-yoon ~]# pt-online-schema-change -u root -pyoon -h127.0.0.1 --alter='add column vname varchar(20)' --execute D=yoon,t=yoon
No slaves found. See --recursion-method if host hank-yoon.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 `yoon`.`yoon`...
Creating new table...
Created new table yoon._yoon_new OK.
Altering new table...
Altered `yoon`.`_yoon_new` OK.
2016-01-08T17:57:09 Creating triggers...
2016-01-08T17:57:09 Created triggers OK.
2016-01-08T17:57:09 Copying approximately 200 rows...
2016-01-08T17:57:09 Copied rows OK.
2016-01-08T17:57:09 Swapping tables...
2016-01-08T17:57:09 Swapped original and new tables OK.
2016-01-08T17:57:09 Dropping old table...
2016-01-08T17:57:09 Dropped old table `yoon`.`_yoon_old` OK.
2016-01-08T17:57:09 Dropping triggers...
2016-01-08T17:57:09 Dropped triggers OK.
Successfully altered `yoon`.`yoon`.
mysql> show create table yoon\G
*************************** 1. row ***************************
Table: yoon
Create Table: CREATE TABLE `yoon` (
`actor_id` smallint(8) unsigned NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`vname` varchar(20) DEFAULT NULL,
PRIMARY KEY (`actor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
添加多个字段:
[root@hank-yoon ~]# pt-online-schema-change -u root -pyoon -h127.0.0.1 --alter='add column aname varchar(20),add column bname varchar(30)' --execute D=yoon,t=yoon
No slaves found. See --recursion-method if host hank-yoon.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 `yoon`.`yoon`...
Creating new table...
Created new table yoon._yoon_new OK.
Altering new table...
Altered `yoon`.`_yoon_new` OK.
2016-01-08T18:04:25 Creating triggers...
2016-01-08T18:04:25 Created triggers OK.
2016-01-08T18:04:25 Copying approximately 200 rows...
2016-01-08T18:04:25 Copied rows OK.
2016-01-08T18:04:25 Swapping tables...
2016-01-08T18:04:26 Swapped original and new tables OK.
2016-01-08T18:04:26 Dropping old table...
2016-01-08T18:04:26 Dropped old table `yoon`.`_yoon_old` OK.
2016-01-08T18:04:26 Dropping triggers...
2016-01-08T18:04:26 Dropped triggers OK.
Successfully altered `yoon`.`yoon`.
mysql> show create table yoon\G
*************************** 1. row ***************************
Table: yoon
Create Table: CREATE TABLE `yoon` (
`actor_id` smallint(8) unsigned NOT NULL,
`first_name` varchar(45) NOT NULL,
`last_name` varchar(45) NOT NULL,
`last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`vname` varchar(20) DEFAULT NULL,
`aname` varchar(20) DEFAULT NULL,
`bname` varchar(30) DEFAULT NULL,
PRIMARY KEY (`actor_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
删除字段:
[root@hank-yoon ~]# pt-online-schema-change -u root -pyoon -h127.0.0.1 --alter='drop column aname,drop column bname' --execute D=yoon,t=yoon
No slaves found. See --recursion-method if host hank-yoon.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 `yoon`.`yoon`...
Creating new table...
Created new table yoon._yoon_new OK.
Altering new table...
Altered `yoon`.`_yoon_new` OK.
2016-01-08T18:05:45 Creating triggers...
2016-01-08T18:05:45 Created triggers OK.
2016-01-08T18:05:45 Copying approximately 200 rows...
2016-01-08T18:05:45 Copied rows OK.
2016-01-08T18:05:45 Swapping tables...
2016-01-08T18:05:45 Swapped original and new tables OK.
2016-01-08T18:05:45 Dropping old table...
2016-01-08T18:05:45 Dropped old table `yoon`.`_yoon_old` OK.
2016-01-08T18:05:45 Dropping triggers...
2016-01-08T18:05:46 Dropped triggers OK.
Successfully altered `yoon`.`yoon`.
添加索引:
[root@hank-yoon ~]# pt-online-schema-change -u root -pyoon -h127.0.0.1 --alter='add key index_first(first_name)' --execute D=yoon,t=yoon No slaves found. See --recursion-method if host hank-yoon.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 `yoon`.`yoon`...
Creating new table...
Created new table yoon._yoon_new OK.
Altering new table...
Altered `yoon`.`_yoon_new` OK.
2016-01-08T18:06:38 Creating triggers...
2016-01-08T18:06:38 Created triggers OK.
2016-01-08T18:06:38 Copying approximately 200 rows...
2016-01-08T18:06:38 Copied rows OK.
2016-01-08T18:06:38 Swapping tables...
2016-01-08T18:06:38 Swapped original and new tables OK.
2016-01-08T18:06:38 Dropping old table...
2016-01-08T18:06:38 Dropped old table `yoon`.`_yoon_old` OK.
2016-01-08T18:06:38 Dropping triggers...
2016-01-08T18:06:38 Dropped triggers OK.
Successfully altered `yoon`.`yoon`.
MySQL Online DDL 工具之pt-online-schema-change的更多相关文章
- [资料收集]MySQL在线DDL工具pt-online-schema-change
MySQL在线DDL工具pt-online-schema-change pt-online-schema-change使用说明(未完待续) 官网
- MySQL在线DDL工具 gh-ost
一.简介 gh-ost基于 golang 语言,是 github 开源的一个 DDL 工具,是 GitHub's Online Schema Transmogrifier/Transfigurator ...
- MySQL Online DDL工具
MySQL在线表结构变更工具 MySQL的大表表结构变更常用的解决方案无外乎三种: 一是利用Percona的pt-online-schema-change,Facebook的OSC等三方工具, 二是在 ...
- MySQL在线DDL gh-ost 使用说明
背景: 作为一个DBA,大表的DDL的变更大部分都是使用Percona的pt-online-schema-change,本文说明下另一种工具gh-ost的使用:不依赖于触发器,是因为他是通过模拟从库, ...
- MySQL Binlog 解析工具 Maxwell 详解
maxwell 简介 Maxwell是一个能实时读取MySQL二进制日志binlog,并生成 JSON 格式的消息,作为生产者发送给 Kafka,Kinesis.RabbitMQ.Redis.Goog ...
- mysql online ddl
大家知道,互联网业务是典型的OLTP(online transaction process)应用,这种应用访问数据库的特点是大量的短事务高并发运行.因此任何限制高并发的动作都是不可接受的,甚至 ...
- Online Schema Change for MySQL
It is great to be able to build small utilities on top of an excellent RDBMS. Thank you MySQL. This ...
- MySQL 闪回工具之 binlog2sql
生产上误删数据.误改数据的现象也是时常发生的现象,作为 DBA 这时候就需要出来补锅了,最开始的做法是恢复备份,然后从中找到需要的数据再进行修复,但是这个时间太长了,对于大表少数数据的修复来讲,动作太 ...
- Online DDL工具的安装与使用
最近经常在线上经常遇到有性能问题的SQL,有些表没有合理添加索引,有些表添加的索引不合理,各种各样的问题,导致SQL的执行效率不高.这时DBA们不得不重构SQL,使其达到最好的性能,这时我们往往要在线 ...
随机推荐
- SQL语言简介
什么是SQL语言? 是高级非过程化语言(是沟通数据库服务器和客户端的工具) 作用:存取,查询,更新和管理关系数据库系统 SQL语言分类: 1.DDL:数据定义语言 作用:定义和管理数据当中的各种对象 ...
- angularjs表格方式显示数据
<table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td&g ...
- MySQL 中随机抽样:order by rand limit 的替代方案
最近由于需要大概研究了一下MYSQL的随机抽取实现方法.举个例子,要从tablename表中随机提取一条记录,大家一般的写法就是:SELECT * FROM tablename ORDER BY RA ...
- 【wpf WebBrowser 清空网站的Cookie&Session 清空用户登录状态】
最近做项目遇到了一个说小不小,说大不大的问题,那就是在WebBrowser中清空网站上用户的登陆状态, 一开始心想,那不就清空cookies就行啦,那么简单的事情,百度一下 …… …… 是的,正如你们 ...
- 支持多浏览器的镜像反转css效果
今天接到一个需求,通过一张图片得到其镜像对称的图片.本来说是后台处理的,但这样除了技术上难实现之外,还带来了资源消耗的问题,相当于每张图都要存储一个副本了. 只记得以前用过css的滤镜可以实现这个,但 ...
- [CAMCOCO][C#]我的系统架构.服务器端.(四)----Model层 实体的自我验证
这是Model的第二篇,上一篇点这里 这块完全是扒了@何镇汐大神博客里的教程实现的,在这之前完全没想到数据验证居然可以这样做!!在此表示严重感谢!!! 点击这里可以去了解这个方法的原理,老胡估计自己是 ...
- Unity3d 引擎原理详细介绍、Unity3D引擎架构设计
体系结构 为了更好地理解游戏的软件架构和对象模型,它获得更好的外观仅有一名Unity3D的游戏引擎和编辑器是非常有用的,它的主要原则. Unity3D 引擎 Unity3D的是一个屡获殊荣的工具,用于 ...
- 23----2013.07.01---Div和Span区别,Css常用属性,选择器,使用css的方式,脱离文档流,div+css布局,盒子模型,框架,js基本介绍
01 复习内容 复习之前的知识点 02演示VS创建元素 03div和span区别 通过display属性进行DIV与Span之间的转换.div->span 设置display:inline ...
- c++错误修复 数据库无法打开 无法右击 run outtiime
先前有安装vs2015失败的前提 现象:1.无法右击 显示 explorer.exe c++ run outtiime 这些字样 2. 安装有数据库的软件都不能用.显示数据库连接 ...
- Cisco基本命令配置
实验一 路由器的基本命令操作 1 实验目标 ü 熟悉路由器的命令行操作 ü 能够使用命令行帮助 ü 能够查看路由器接口信息 ü 能够产看路由器配置信息 ü 能够配置以太网接口 ü 能够配置广域网接口 ...