摘要:openarkkit工具包中oak-online-alter-table的使用
什么是OAK
openark工具包是一组由Python语言编写的用于MySQL的工具集。可以解决一些日常复杂或耗时的维护任务。
包括如下工具:
oak-apply-ri: apply referential integrity on two columns with parent-child relationship.
oak-block-account: block or release MySQL users accounts, disabling them or enabling them to login.
oak-chunk-update: perform long, non-blocking UPDATE/DELETE operation in auto managed small chunks.
oak-get-slave-lag: print slave replication lag and terminate with respective exit code.
oak-hook-general-log: hook up and filter general log entries based on entry type or execution plan criteria.
oak-kill-slow-queries: terminate long running queries.
oak-modify-charset: change the character set (and collation) of a textual column.
oak-online-alter-table: perform a non-blocking ALTER TABLE operation.
oak-prepare-shutdown: make for a fast and safe MySQL shutdown.
oak-purge-master-logs: purge master logs, depending on the state of replicating slaves.
oak-repeat-query: repeat query execution until some condition holds.
oak-security-audit: audit accounts, passwords, privileges and other security settings.
oak-show-limits: show AUTO_INCREMENT “free space”.
oak-show-replication-status: show how far behind are replicating slaves on a given master.
下载安装
|
操作流程
1、检查被操作的表是否符合如下条件:
- 单列唯一索引(联合索引和联合主键是不可以的,会触发mysql的一个bug)
- 没有外键
- 没有触发器(有的话要先删除,做完DDL再加上)
2、执行OAK命令
3、数据一致性校验
4、表切换
5、删除触发器
6、删除临时表在线DDL实操
准备测试数据
[root@mysql ~]# sysbench --test=/root/sysbench-0.5/sysbench/tests/db/insert.lua --oltp-table-size=1000000 --mysql-table-engine=innodb --mysql-user=root --mysql-password=root123 --mysql-port=3306 --mysql-host=192.168.1.172 --mysql-db=jfedu --max-requests=0 --max-time=60 --oltp-tables-count=1 --report-interval=10 --num_threads=1 preparesysbench 0.5: multi-threaded system evaluation benchmarkCreating table 'sbtest1'...Inserting 1000000 records into 'sbtest1'Creating secondary indexes on 'sbtest1'...[root@mysql ~]#root@jfedu 01:20:08>show create table sbtest1;+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| sbtest1 | CREATE TABLE `sbtest1` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT,`k` int(10) unsigned NOT NULL DEFAULT '0',`c` char(120) NOT NULL DEFAULT '',`pad` char(60) NOT NULL DEFAULT '',PRIMARY KEY (`id`),KEY `k_1` (`k`)) ENGINE=InnoDB AUTO_INCREMENT=1000001 DEFAULT CHARSET=utf8 MAX_ROWS=1000000 |+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.02 sec)mysql> select count(*) from sbtest1;+----------+| count(*) |+----------+| 1060893 |+----------+1 row in set (0.16 sec)
检查是否符合在线DDL的条件
根据表结构可以看出是符合单列唯一索引,且无外键的。通过下面SQL查看结果显示是没触发器的:
执行OAK命令,给表加字段
|
可以看到只拷贝了1060893条数据,我们后来插入的数据有没有过去呢?
数据一致性校验
|
验证一个疑问
用oak执行完ddl后,尚未rename的时候,在老表上做DML操作是否会同步到新表?
1、在老表插入数据
2、在老表修改数据
可以看出在老表中的插入和更新操作都被同步到了新表。这是由OAK创建的三个触发器来实现的:
看看这三个触发器的内容:
也就是说在我们rename切换表之前,对旧表的任何增、删、改操作都会同步到新表!
表切换
|
在切换表之前,触发器一定不能删掉,否则数据就会不一致的!
遇到问题及解决
1、安装MySQLdb遇到的问题
2、用oak做Online DDL,如果rename那一步操作错了?不小心在从库上做rename了怎么恢复?(包含旧表删除和trigger删除)
可以使用oak在主库再做一遍,因为从库已经做过了因此要加一个参数–skip-binlog来关闭binlog写入。这样主库的操作就不会复制到从库,避免主从复制异常!