MySQL在线DDL gh-ost 使用说明

背景:

      做为一个DBA,大表的DDL的变动大部分都是使用Percona的pt-online-schema-change,本文说明下另外一种工具gh-ost的使用:不依赖于触发器,是由于他是经过模拟从库,在row binlog中获取增量变动,再异步应用到ghost表的。在使用gh-ost以前,能够先看GitHub 开源的 MySQL 在线更改 Schema 工具【转】文章或则官网了解其特性和原理。本文只对使用进行说明。html

说明:

1)下载安装:https://github.com/github/gh-ost/tagspython

2)参数说明:gh-ost --help
Usage of gh-ost:
  --aliyun-rds:是否在阿里云数据库上执行。true
  --allow-master-master:是否容许gh-ost运行在双主复制架构中,通常与-assume-master-host参数一块儿使用
  --allow-nullable-unique-key:容许gh-ost在数据迁移依赖的惟一键能够为NULL,默认为不容许为NULL的惟一键。若是数据迁移(migrate)依赖的惟一键容许NULL值,则可能形成数据不正确,请谨慎使用。
  --allow-on-master:容许gh-ost直接运行在主库上。默认gh-ost链接的从库。
  --alter string:DDL语句
  --approve-renamed-columns ALTER:若是你修改一个列的名字,gh-ost将会识别到而且须要提供重命名列名的缘由,默认状况下gh-ost是不继续执行的,除非提供-approve-renamed-columns ALTER。
  --ask-pass:MySQL密码
  --assume-master-host string:为gh-ost指定一个主库,格式为”ip:port”或者”hostname:port”。在这主主架构里比较有用,或则在gh-ost发现不到主的时候有用。
  --assume-rbr:确认gh-ost链接的数据库实例的binlog_format=ROW的状况下,能够指定-assume-rbr,这样能够禁止从库上运行stop slave,start slave,执行gh-ost用户也不须要SUPER权限。
  --check-flag
  --chunk-size int:在每次迭代中处理的行数量(容许范围:100-100000),默认值为1000。
  --concurrent-rowcount:该参数若是为True(默认值),则进行row-copy以后,估算统计行数(使用explain select count(*)方式),并调整ETA时间,不然,gh-ost首先预估统计行数,而后开始row-copy。
  --conf string:gh-ost的配置文件路径。
  --critical-load string:一系列逗号分隔的status-name=values组成,当MySQL中status超过对应的values,gh-ost将会退出。-critical-load Threads_connected=20,Connections=1500,指的是当MySQL中的状态值Threads_connected>20,Connections>1500的时候,gh-ost将会因为该数据库严重负载而中止并退出。
        Comma delimited status-name=threshold, same format as --max-load. When status exceeds threshold, app panics and quits
  --critical-load-hibernate-seconds int :负载达到critical-load时,gh-ost在指定的时间内进入休眠状态。 它不会读/写任何来自任何服务器的任何内容。
  --critical-load-interval-millis int:当值为0时,当达到-critical-load,gh-ost当即退出。当值不为0时,当达到-critical-load,gh-ost会在-critical-load-interval-millis秒数后,再次进行检查,再次检查依旧达到-critical-load,gh-ost将会退出。
  --cut-over string:选择cut-over类型:atomic/two-step,atomic(默认)类型的cut-over是github的算法,two-step采用的是facebook-OSC的算法。
  --cut-over-exponential-backoff
  --cut-over-lock-timeout-seconds int:gh-ost在cut-over阶段最大的锁等待时间,当锁超时时,gh-ost的cut-over将重试。(默认值:3)
  --database string:数据库名称。
  --debug:debug模式。
  --default-retries int:各类操做在panick前重试次数。(默认为60)
  --discard-foreign-keys:该参数针对一个有外键的表,在gh-ost建立ghost表时,并不会为ghost表建立外键。该参数很适合用于删除外键,除此以外,请谨慎使用。
  --dml-batch-size int:在单个事务中应用DML事件的批量大小(范围1-100)(默认值为10)
  --exact-rowcount:准确统计表行数(使用select count(*)的方式),获得更准确的预估时间。
  --execute:实际执行alter&migrate表,默认为noop,不执行,仅仅作测试并退出,若是想要ALTER TABLE语句真正落实到数据库中去,须要明确指定-execute
  --exponential-backoff-max-interval int
  --force-named-cut-over:若是为true,则'unpostpone | cut-over'交互式命令必须命名迁移的表
  --force-table-names string:在临时表上使用的表名前缀
  --heartbeat-interval-millis int:gh-ost心跳频率值,默认为500
  --help
  --hooks-hint string:任意消息经过GH_OST_HOOKS_HINT注入到钩子
  --hooks-path string:hook文件存放目录(默认为empty,即禁用hook)。hook会在这个目录下寻找符合约定命名的hook文件来执行。
  --host string :MySQL IP/hostname
  --initially-drop-ghost-table:gh-ost操做以前,检查并删除已经存在的ghost表。该参数不建议使用,请手动处理原来存在的ghost表。默认不启用该参数,gh-ost直接退出操做。
  --initially-drop-old-table:gh-ost操做以前,检查并删除已经存在的旧表。该参数不建议使用,请手动处理原来存在的ghost表。默认不启用该参数,gh-ost直接退出操做。
  --initially-drop-socket-file:gh-ost强制删除已经存在的socket文件。该参数不建议使用,可能会删除一个正在运行的gh-ost程序,致使DDL失败。
  --master-password string :MySQL 主密码
  --master-user string:MysQL主帐号
  --max-lag-millis int:主从复制最大延迟时间,当主从复制延迟时间超过该值后,gh-ost将采起节流(throttle)措施,默认值:1500s。
  --max-load string:逗号分隔状态名称=阈值,如:'Threads_running=100,Threads_connected=500'. When status exceeds threshold, app throttles writes
  --migrate-on-replica:gh-ost的数据迁移(migrate)运行在从库上,而不是主库上。 
  --nice-ratio float:每次chunk时间段的休眠时间,范围[0.0…100.0]。0:每一个chunk时间段不休眠,即一个chunk接着一个chunk执行;1:每row-copy 1毫秒,则另外休眠1毫秒;0.7:每row-copy 10毫秒,则另外休眠7毫秒。
  --ok-to-drop-table:gh-ost操做结束后,删除旧表,默认状态是不删除旧表,会存在_tablename_del表。
  --panic-flag-file string:当这个文件被建立,gh-ost将会当即退出。
  --password string :MySQL密码
 --port int :MySQL端口,最好用从库
  --postpone-cut-over-flag-file string:当这个文件存在的时候,gh-ost的cut-over阶段将会被推迟,数据仍然在复制,直到该文件被删除。
  --quiet:静默模式。
  --replica-server-id uint : gh-ost的server_id
  --replication-lag-query string:弃用
  --serve-socket-file string:gh-ost的socket文件绝对路径。
  --serve-tcp-port int:gh-ost使用端口,默认为关闭端口。
  --skip-foreign-key-checks:肯定你的表上没有外键时,设置为'true',而且但愿跳过gh-ost验证的时间-skip-renamed-columns ALTER
  --skip-renamed-columns ALTER:若是你修改一个列的名字(如change column),gh-ost将会识别到而且须要提供重命名列名的缘由,默认状况下gh-ost是不继续执行的。该参数告诉gh-ost跳该列的数据迁移,让gh-ost把重命名列做为可有可无的列。该操做很危险,你会损失该列的全部值。
  --stack:添加错误堆栈追踪。
  --switch-to-rbr:让gh-ost自动将从库的binlog_format转换为ROW格式。
  --table string:表名
  --test-on-replica:在从库上测试gh-ost,包括在从库上数据迁移(migration),数据迁移完成后stop slave,原表和ghost表马上交换然后马上交换回来。继续保持stop slave,使你能够对比两张表。
  --test-on-replica-skip-replica-stop:当-test-on-replica执行时,该参数表示该过程当中不用stop slave。
  --throttle-additional-flag-file string:当该文件被建立后,gh-ost操做当即中止。该参数能够用在多个gh-ost同时操做的时候,建立一个文件,让全部的gh-ost操做中止,或者删除这个文件,让全部的gh-ost操做恢复。
  --throttle-control-replicas string:列出全部须要被检查主从复制延迟的从库。
  --throttle-flag-file string:当该文件被建立后,gh-ost操做当即中止。该参数适合控制单个gh-ost操做。-throttle-additional-flag-file string适合控制多个gh-ost操做。
  --throttle-http string
  --throttle-query string:节流查询。每秒钟执行一次。当返回值=0时不须要节流,当返回值>0时,须要执行节流操做。该查询会在数据迁移(migrated)服务器上操做,因此请确保该查询是轻量级的。
  --timestamp-old-table:在旧表名中使用时间戳。 这会使旧表名称具备惟一且无冲突的交叉迁移
  --tungsten:告诉gh-ost你正在运行的是一个tungsten-replication拓扑结构。
  --user string :MYSQL用户
  --verbose
  --version
View Code

3)使用说明:条件是操做的MySQL上须要的binlog模式是ROW。若是在一个从上测试也必须是ROW模式,还要开启log_slave_updates。根据上面的参数说明按照需求进行调整。mysql

     环境:主库:192.168.163.131;从库:192.168.163.130git

DDL过程github

① 检查有没有外键和触发器。
② 检查表的主键信息。
③ 检查是否主库或从库,是否开启log_slave_updates,以及binlog信息  
④ 检查gho和del结尾的临时表是否存在
⑤ 建立ghc结尾的表,存数据迁移的信息,以及binlog信息等    
---以上校验阶段
⑥ 初始化stream的链接,添加binlog的监听
---如下迁移阶段
⑥ 建立gho结尾的临时表,执行DDL在gho结尾的临时表上
⑦ 开启事务,按照主键id把源表数据写入到gho结尾的表上,再提交,以及binlog apply。
---如下cut-over阶段
⑧ lock源表,rename 表:rename 源表 to 源_del表,gho表 to 源表。
⑨ 清理ghc表。

1. 单实例上DDL: 单个实例至关于主库,须要开启--allow-on-master参数和ROW模式。算法

gh-ost --user="root" --password="root" --host=192.168.163.131  --database="test" --table="t1"  --alter="ADD COLUMN cc2 varchar(10),add column cc3 int not null default 0 comment 'test' " --allow-on-master  --execute

2. 主从上DDLsql

有2个选择,一是按照1直接在主上执行同步到从上,另外一个链接到从库,在主库作迁移(只要保证从库的binlog为ROW便可,主库不须要保证):shell

gh-ost --user="root" --password="root" --host=192.168.163.130  --database="test" --table="t" --initially-drop-old-table --alter="ADD COLUMN y1 varchar(10),add column y2 int not null default 0 comment 'test' "  --execute

此时的操做大体是:数据库

  • 行数据在主库上读写segmentfault

  • 读取从库的二进制日志,将变动应用到主库

  • 在从库收集表格式,字段&索引,行数等信息

  • 在从库上读取内部的变动事件(如心跳事件)

  • 在主库切换表

在执行DDL中,从库会执行一次stop/start slave,要是肯定从的binlog是ROW的话能够添加参数:--assume-rbr。若是从库的binlog不是ROW,能够用参数--switch-to-rbr来转换成ROW,此时须要注意的是执行完毕以后,binlog模式不会被转换成原来的值。--assume-rbr和--switch-to-rbr参数不能一块儿使用。

3. 在从上进行DDL测试

gh-ost --user="root" --password="root" --host=192.168.163.130  --database="test" --table="t"  --alter="ADD COLUMN abc1 varchar(10),add column abc2 int not null default 0 comment 'test' " --test-on-replica  --switch-to-rbr --execute

参数--test-on-replica:在从库上测试gh-ost,包括在从库上数据迁移(migration),数据迁移完成后stop slave,原表和ghost表马上交换然后马上交换回来。继续保持stop slave,使你能够对比两张表。若是不想stop slave,则能够再添加参数:--test-on-replica-skip-replica-stop

上面三种是gh-ost操做模式,上面的操做中,到最后不会清理临时表,须要手动清理,再下次执行以前果真临时表还存在,则会执行失败,能够经过参数进行删除:

--initially-drop-ghost-table:gh-ost操做以前,检查并删除已经存在的ghost表。该参数不建议使用,请手动处理原来存在的ghost表。默认不启用该参数,gh-ost直接退出操做。

--initially-drop-old-table:gh-ost操做以前,检查并删除已经存在的旧表。该参数不建议使用,请手动处理原来存在的ghost表。默认不启用该参数,gh-ost直接退出操做。

--initially-drop-socket-file:gh-ost强制删除已经存在的socket文件。该参数不建议使用,可能会删除一个正在运行的gh-ost程序,致使DDL失败。

--ok-to-drop-table:gh-ost操做结束后,删除旧表,默认状态是不删除旧表,会存在_tablename_del表。

还有其余的一些参数,好比:--exact-rowcount、--max-lag-millis、--max-load等等,能够看上面的说明,具体大部分经常使用的参数命令以下:

gh-osc --user= --password= --host= --database= --table= --max-load=Threads_running=30, --chunk-size=1000 --serve-socket-file=/tmp/gh-ost.test.sock --exact-rowcount --allow-on-master/--test-on-replica --initially-drop-ghost-table/--initially-drop-old-table/--initially-drop-socket-file --max-lag-millis= --max-load='Threads_running=100,Threads_connected=500' --ok-to-drop-table

4)额外说明:终止、暂停、限速

gh-ost --user="root" --password="root" --host=192.168.163.131  --database="test" --table="t1"  --alter="ADD COLUMN o2 varchar(10),add column o1 int not null default 0 comment 'test' " --exact-rowcount --serve-socket-file=/tmp/gh-ost.t1.sock --panic-flag-file=/tmp/gh-ost.panic.t1.flag  --postpone-cut-over-flag-file=/tmp/ghost.postpone.t1.flag --allow-on-master  --execute

① 标示文件终止运行--panic-flag-file

建立文件终止运行,例子中建立/tmp/gh-ost.panic.t1.flag文件,终止正在运行的gh-ost,临时文件清理须要手动进行。

表示文件禁止cut-over进行,即禁止表名切换,数据复制正常进行。--postpone-cut-over-flag-file

建立文件延迟cut-over进行,即推迟切换操做。例子中建立/tmp/ghost.postpone.t1.flag文件,gh-ost 会完成行复制,但并不会切换表,它会持续的将原表的数据更新操做同步到临时表中。

③ 使用socket监听请求,操做者能够在命令运行后更改相应的参数。--serve-socket-file,--serve-tcp-port(默认关闭)

建立socket文件进行监听,经过接口进行参数调整,当执行操做的过程当中发现负载、延迟上升了,不得不终止操做,从新配置参数,如 chunk-size,而后从新执行操做命令,能够经过scoket接口进行动态调整。如:

暂停操做:

#暂停
echo throttle | socat - /tmp/gh-ost.test.t1.sock
#恢复
echo no-throttle | socat - /tmp/gh-ost.test.t1.sock

修改限速参数:

echo chunk-size=100 | socat - /tmp/gh-ost.t1.sock

echo max-lag-millis=200 | socat - /tmp/gh-ost.t1.sock

echo max-load=Thread_running=3 | socat - /tmp/gh-ost.t1.sock

 4)和pt-online-schema-change对比测试

 1. 表没有写入而且参数为默认的状况下,两者DDL操做时间差很少,毕竟都是copy row操做。

 2. 表有大量写入(sysbench)的状况下,由于pt-osc是多线程处理的,很快就能执行完成,而gh-ost是模拟“从”单线程应用的,极端的状况下,DDL操做很是困难的执行完毕。

 结论:虽然gh-ost不须要触发器,对于主库的压力和性能影响也小不少,可是针对高并发的场景进行DDL效率仍是比pt-osc低,因此仍是须要在业务低峰的时候处理。相关的测试能够看gh-ost和pt-osc性能对比

 5)封装脚本:

环境:M:192.168.163.131(ROW),S:192.168.163.130/132

封装脚本:gh-ost.py

#!/bin/env python
# -*- encoding: utf-8 -*-
#----------------------------------------------
# Purpose:     gh-ost
# Created:     2018-06-16
#----------------------------------------------

import MySQLdb
import re
import sys
import time
import subprocess
import os
from optparse import OptionParser

def calc_time(func):
    def _deco(*args, **kwargs):
        begin_time = time.time()
        func(*args, **kwargs)
        cost_time = time.time() - begin_time
        print 'cost time: %ss' % round(cost_time,2)
    return _deco

def get_table_count(conn,dbname,tbname):
    query  = ''' SELECT count(*) FROM %s.%s ''' %(dbname,tbname)
    cursor = conn.cursor()
    cursor.execute(query)
    row_nums = cursor.fetchone()
    cursor.close()
    conn.close() 
    return row_nums

def online_ddl(conn,ddl_cmd):
    cursor = conn.cursor()
    cursor.execute(ddl_cmd)
    conn.commit()
    cursor.close()
    conn.close()  

#@calc_time
def run_cmd(cmd):
    p = subprocess.Popen(cmd, shell=True)
    return p,p.pid

def drop_ghost_table(conn,ghost_name_list):
    try:
        cursor = conn.cursor()
        query  = ''' DROP TABLE IF EXISTS %s; ''' %(ghost_name_list)
        cursor.execute(query)
        conn.commit()
        cursor.close()
        conn.close()
    except Exception,e:
        print e

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option("-P", "--Port", help="Port for search", dest="port")
    parser.add_option("-D", "--Dbname", help="the Dbname to use", dest="dbname")
    parser.add_option("-T", "--Table", help="the Table to use", dest="tablename")

    (options, args) = parser.parse_args()

    if not options.port:
        print 'params port need to apply'
        exit()

    if not options.dbname:
        print 'params dbname need to apply'
        exit()

    if not options.tablename:
        print 'params tablename need to apply'
        exit()

    gh_ost_socket   = '/tmp/gh-ost.%s.%s.sock' %(options.dbname,options.tablename)
    #终止标志
    panic_flag      = '/tmp/gh-ost.panic.%s.%s.flag' %(options.dbname,options.tablename)
    # postpone_flag   =  '/tmp/gh-ost.postpone.%s.%s.flag' %(options.dbname,options.tablename)
    #暂停标志
    throttle_flag   = '/tmp/gh-ost.throttle.%s.%s' %(options.dbname,options.tablename)
#    socket = '/data/%s/tmp/mysql.sock' %(options.port)
    socket = '/var/run/mysqld/mysqld.sock'

    

    get_conn = MySQLdb.connect(host='192.168.163.131', port=int(options.port), user='root', passwd='root', db=options.dbname, unix_socket=socket,charset='utf8')
    conn     = MySQLdb.connect(host='192.168.163.131', port=int(options.port), user='root', passwd='root', db=options.dbname, unix_socket=socket,charset='utf8')
    
    (table_count,) = get_table_count(get_conn,options.dbname,options.tablename)
    print ("\033[0;32m%s\033[0m" % "表的数量:%s" %table_count)

    DDL_CMD    = raw_input('Enter DDL CMD   : ').replace('`','')

    gh_command_list = re.split('[ ]+',DDL_CMD)
    if gh_command_list[0].upper() == 'CHANGE' and gh_command_list[1] != gh_command_list[2]:
        print ("\033[0;31m%s\033[0m" % "renamed columns' data will be lost,pt-osc exit...")
        exit()

    if table_count <= 10000:
        ddl = ''' ALTER TABLE %s %s ''' %(options.tablename,DDL_CMD)
        print ("\033[0;36m%s\033[0m" %ddl)
        print ("\033[0;32m%s\033[0m" % "online ddl ...")
        online_ddl(conn,ddl)
        print ("\033[0;32m%s\033[0m" % "执行完成 ...")
        exit()

    else:
        MAX_LOAD   = raw_input('Enter Max Threads_running【25】 : ')
        if not MAX_LOAD:
            Threads_running = 25 
        else:
            try:
                Threads_running = int(MAX_LOAD)
            except ValueError:
                print ("\033[0;31m%s\033[0m" % "输入类型错误,退出...")
                exit()

        CHUNK_SIZE = raw_input('Enter Max chunk-size【1000】    : ')
        if not CHUNK_SIZE:
            chunk_size = 1000
        else:
            try:
                chunk_size = int(CHUNK_SIZE)
            except ValueError:
                print ("\033[0;31m%s\033[0m" % "输入类型错误,退出...")
                exit()

        print ("\033[0;32m%s\033[0m" % "gh-ost ddl ...")
        #--postpone-cut-over-flag-file=%s 
        gh_command = '''/usr/bin/gh-ost --user="root" --password="root" --host=192.168.163.131 --port=%s --database="%s" --table="%s" --allow-on-master  --max-load='Threads_running=%d' --chunk-size=%d --serve-socket-file=%s --panic-flag-file=%s --throttle-additional-flag-file=%s --alter="%s"  --execute ''' %(options.port,options.dbname,options.tablename,Threads_running,chunk_size,gh_ost_socket,panic_flag,throttle_flag,DDL_CMD)
        print ("\033[0;36m%s\033[0m" %gh_command)

    
        child,pid = run_cmd(gh_command)
        print ("\033[0;31mgh-ost's PID:%s\033[0m" %pid)
        print ("\033[0;33m建立:【touch %s】文件,暂停DDL ...\033[0m" %throttle_flag)
        try:
            child.wait()
        except:
            child.terminate()
            #clean
            ghost_name_list = '_%s_ghc,_%s_gho'  %(options.tablename,options.tablename)
            drop_ghost_table(conn,ghost_name_list)
            if os.path.exists(gh_ost_socket):
                os.system('rm -r %s' %gh_ost_socket)
                print ("\033[0;32m%s\033[0m" % "清理完成 ...")
                exit()
            print ("\033[0;32m%s\033[0m" % "清理完成 ...")
            exit()
        finally :
            pass
View Code

运行:

root@test2:~# python gh-ost.py -P3306 -Dtest -Tzjy
表的数量:1310720
Enter DDL CMD   : ADD COLUMN q1 varchar(10),ADD COLUMN q2 varchar(10)
Enter Max Threads_running【25】 : 10
Enter Max chunk-size【1000】    : 200
gh-ost ddl ...
/usr/bin/gh-ost --user="root" --password="root" --host=192.168.163.131 --port=3306 --database="test" --table="zjy" --allow-on-master  --max-load='Threads_running=10' --chunk-size=200 --serve-socket-file=/tmp/gh-ost.test.zjy.sock --panic-flag-file=/tmp/gh-ost.panic.test.zjy.flag --throttle-additional-flag-file=/tmp/gh-ost.throttle.test.zjy --alter="ADD COLUMN q1 varchar(10),ADD COLUMN q2 varchar(10)"  --execute 
gh-ost's PID:2105
建立:【touch /tmp/gh-ost.throttle.test.zjy】文件,暂停DDL ...
2018/06/17 14:37:37 binlogsyncer.go:79: [info] create BinlogSyncer with config {99999 mysql 192.168.163.131 3306 root   false false <nil>}
2018/06/17 14:37:37 binlogsyncer.go:246: [info] begin to sync binlog from position (mysql-bin.000013, 31197930)
2018/06/17 14:37:37 binlogsyncer.go:139: [info] register slave for master server 192.168.163.131:3306
2018/06/17 14:37:37 binlogsyncer.go:573: [info] rotate to (mysql-bin.000013, 31197930)
# Migrating `test`.`zjy`; Ghost table is `test`.`_zjy_gho`
# Migrating test2:3306; inspecting test2:3306; executing on test2
# Migration started at Sun Jun 17 14:37:37 +0800 2018
# chunk-size: 200; max-lag-millis: 1500ms; dml-batch-size: 10; max-load: Threads_running=10; critical-load: ; nice-ratio: 0.000000
# throttle-additional-flag-file: /tmp/gh-ost.throttle.test.zjy 
# panic-flag-file: /tmp/gh-ost.panic.test.zjy.flag
# Serving on unix socket: /tmp/gh-ost.test.zjy.sock
Copy: 0/1305600 0.0%; Applied: 0; Backlog: 0/1000; Time: 0s(total), 0s(copy); streamer: mysql-bin.000013:31199542; State: migrating; ETA: N/A
Copy: 0/1305600 0.0%; Applied: 0; Backlog: 0/1000; Time: 1s(total), 1s(copy); streamer: mysql-bin.000013:31202866; State: migrating; ETA: N/A
Copy: 44400/1305600 3.4%; Applied: 0; Backlog: 0/1000; Time: 2s(total), 2s(copy); streamer: mysql-bin.000013:33352548; State: migrating; ETA: 56s
Copy: 91200/1305600 7.0%; Applied: 0; Backlog: 0/1000; Time: 3s(total), 3s(copy); streamer: mysql-bin.000013:35598132; State: migrating; ETA: 39s
Copy: 135200/1305600 10.4%; Applied: 0; Backlog: 0/1000; Time: 4s(total), 4s(copy); streamer: mysql-bin.000013:37727925; State: migrating; ETA: 34s
Copy: 174000/1305600 13.3%; Applied: 0; Backlog: 0/1000; Time: 5s(total), 5s(copy); streamer: mysql-bin.000013:39588956; State: migrating; ETA: 32s
Copy: 212200/1305600 16.3%; Applied: 0; Backlog: 0/1000; Time: 6s(total), 6s(copy); streamer: mysql-bin.000013:41430090; State: migrating; ETA: 30s
Copy: 254800/1305600 19.5%; Applied: 0; Backlog: 0/1000; Time: 7s(total), 7s(copy); streamer: mysql-bin.000013:43483555; State: migrating; ETA: 28s
Copy: 303600/1305600 23.3%; Applied: 0; Backlog: 0/1000; Time: 8s(total), 8s(copy); streamer: mysql-bin.000013:45834978; State: migrating; ETA: 26s
Copy: 351200/1305600 26.9%; Applied: 0; Backlog: 0/1000; Time: 9s(total), 9s(copy); streamer: mysql-bin.000013:48128675; State: migrating; ETA: 24s
Copy: 401400/1305600 30.7%; Applied: 0; Backlog: 0/1000; Time: 10s(total), 10s(copy); streamer: mysql-bin.000013:50547454; State: migrating; ETA: 22s
Copy: 451200/1305600 34.6%; Applied: 0; Backlog: 0/1000; Time: 11s(total), 11s(copy); streamer: mysql-bin.000013:52946991; State: migrating; ETA: 20s
Copy: 490000/1305600 37.5%; Applied: 0; Backlog: 0/1000; Time: 12s(total), 12s(copy); streamer: mysql-bin.000013:54817320; State: migrating; ETA: 19s
Copy: 529600/1305600 40.6%; Applied: 0; Backlog: 0/1000; Time: 13s(total), 13s(copy); streamer: mysql-bin.000013:56735431; State: migrating; ETA: 19s
Copy: 589200/1305600 45.1%; Applied: 0; Backlog: 0/1000; Time: 14s(total), 14s(copy); streamer: mysql-bin.000013:59606450; State: migrating; ETA: 17s
Copy: 639400/1305600 49.0%; Applied: 0; Backlog: 0/1000; Time: 15s(total), 15s(copy); streamer: mysql-bin.000013:62025561; State: migrating; ETA: 15s
Copy: 695200/1305600 53.2%; Applied: 0; Backlog: 0/1000; Time: 16s(total), 16s(copy); streamer: mysql-bin.000013:64704138; State: migrating; ETA: 14s
Copy: 751200/1305600 57.5%; Applied: 0; Backlog: 0/1000; Time: 17s(total), 17s(copy); streamer: mysql-bin.000013:67401961; State: migrating; ETA: 12s
Copy: 803800/1305600 61.6%; Applied: 0; Backlog: 0/1000; Time: 18s(total), 18s(copy); streamer: mysql-bin.000013:69935884; State: migrating; ETA: 11s
Copy: 856400/1305600 65.6%; Applied: 0; Backlog: 0/1000; Time: 19s(total), 19s(copy); streamer: mysql-bin.000013:72470455; State: migrating; ETA: 9s
Copy: 907400/1305600 69.5%; Applied: 0; Backlog: 0/1000; Time: 20s(total), 20s(copy); streamer: mysql-bin.000013:74927401; State: migrating; ETA: 8s
Copy: 958800/1305600 73.4%; Applied: 0; Backlog: 0/1000; Time: 21s(total), 21s(copy); streamer: mysql-bin.000013:77404243; State: migrating; ETA: 7s
Copy: 999200/1305600 76.5%; Applied: 0; Backlog: 0/1000; Time: 22s(total), 22s(copy); streamer: mysql-bin.000013:79351223; State: migrating; ETA: 6s
Copy: 1009600/1305600 77.3%; Applied: 0; Backlog: 0/1000; Time: 23s(total), 23s(copy); streamer: mysql-bin.000013:79855229; State: migrating; ETA: 6s
Copy: 1059600/1305600 81.2%; Applied: 0; Backlog: 0/1000; Time: 24s(total), 24s(copy); streamer: mysql-bin.000013:82264712; State: migrating; ETA: 5s
Copy: 1107200/1305600 84.8%; Applied: 0; Backlog: 0/1000; Time: 25s(total), 25s(copy); streamer: mysql-bin.000013:84558411; State: migrating; ETA: 4s
Copy: 1147000/1305600 87.9%; Applied: 0; Backlog: 0/1000; Time: 26s(total), 26s(copy); streamer: mysql-bin.000013:86486148; State: migrating; ETA: 3s
Copy: 1198000/1305600 91.8%; Applied: 0; Backlog: 0/1000; Time: 27s(total), 27s(copy); streamer: mysql-bin.000013:88943747; State: migrating; ETA: 2s
Copy: 1245400/1305600 95.4%; Applied: 0; Backlog: 0/1000; Time: 28s(total), 28s(copy); streamer: mysql-bin.000013:91218202; State: migrating; ETA: 1s
Copy: 1286600/1305600 98.5%; Applied: 0; Backlog: 0/1000; Time: 29s(total), 29s(copy); streamer: mysql-bin.000013:93203991; State: migrating; ETA: 0s
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 0/1000; Time: 29s(total), 29s(copy); streamer: mysql-bin.000013:94366846; State: migrating; ETA: due
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 1/1000; Time: 30s(total), 29s(copy); streamer: mysql-bin.000013:94369042; State: migrating; ETA: due
# Migrating `test`.`zjy`; Ghost table is `test`.`_zjy_gho`
# Migrating test2:3306; inspecting test2:3306; executing on test2
# Migration started at Sun Jun 17 14:37:37 +0800 2018
# chunk-size: 200; max-lag-millis: 1500ms; dml-batch-size: 10; max-load: Threads_running=10; critical-load: ; nice-ratio: 0.000000
# throttle-additional-flag-file: /tmp/gh-ost.throttle.test.zjy 
# panic-flag-file: /tmp/gh-ost.panic.test.zjy.flag
# Serving on unix socket: /tmp/gh-ost.test.zjy.sock
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 0/1000; Time: 30s(total), 29s(copy); streamer: mysql-bin.000013:94371928; State: migrating; ETA: due
2018/06/17 14:38:08 binlogsyncer.go:107: [info] syncer is closing... 
2018/06/17 14:38:08 binlogstreamer.go:47: [error] close sync with err: sync is been closing... (这里的error不影响使用,重复关闭了sync,等做者修复)
2018/06/17 14:38:08 binlogsyncer.go:122: [info] syncer is closed 
# Done
View Code

总结:

gh-ost 放弃了触发器,使用 binlog 来同步。gh-ost 做为一个假装的备库,能够从主库/备库上拉取 binlog,过滤以后从新应用到主库上去,至关于主库上的增量操做经过 binlog 又应用回主库自己,不过是应用在幽灵表上。

gh-ost 首先链接到主库上,根据 alter 语句建立幽灵表,而后做为一个”备库“链接到其中一个真正的备库上,一边在主库上拷贝已有的数据到幽灵表,一边从备库上拉取增量数据的 binlog,而后不断的把 binlog 应用回主库。图中 cut-over 是最后一步,锁住主库的源表,等待 binlog 应用完毕,而后替换 gh-ost 表为源表。gh-ost 在执行中,会在本来的 binlog event 里面增长如下 hint 和心跳包,用来控制整个流程的进度,检测状态等。这种架构带来诸多好处,例如:

  • 整个流程异步执行,对于源表的增量数据操做没有额外的开销,高峰期变动业务对性能影响小。
  • 下降写压力,触发器操做都在一个事务内,gh-ost 应用 binlog 是另一个链接在作。
  • 可中止,binlog 有位点记录,若是变动过程发现主库性能受影响,能够马上中止拉binlog,中止应用 binlog,稳定以后继续应用。
  • 可测试,gh-ost 提供了测试功能,能够链接到一个备库上直接作 Online DDL,在备库上观察变动结果是否正确,再对主库操做,内心更有底。

 

参考文档:

https://github.com/github/gh-ost

GitHub 开源的 MySQL 在线更改 Schema 工具

Online DDL 工具 gh-ost 支持阿里云 RDS

gh-ost:不同的在线表结构变动

GitHub开源MySQL Online DDL工具gh-ost参数解析 

相关文章
相关标签/搜索