目录git
GitHub’s online schema migration for MySQL
项目地址:gh-ostgithub
最初它被命名为gh-osc: GitHub Online Schema Change,相似于Facebook online schema change和pt-online-schema-change。算法
但后来发生了一种罕见的基因突变,c变成了t。这让咱们走上了寻找一个新的缩略词的道路。gh-ost(发音:Ghost),表明GitHub的在线模式转换器/变形器。数据库
详细说明都在安全
cheatsheetbash
。您可能对以各类模式调用gh-ost感兴趣:服务器
gh-ost 首先链接到主库上,根据 alter 语句建立幽灵表,而后做为一个”备库“链接到其中一个真正的备库上,一边在主库上拷贝已有的数据到幽灵表,一边从备库上拉取增量数据的 binlog,而后不断的把 binlog 应用回主库。图中 cut-over 是最后一步,锁住主库的源表,等待 binlog 应用完毕,而后替换 gh-ost 表为源表。gh-ost 在执行中,会在本来的 binlog event 里面增长如下 hint 和心跳包,用来控制整个流程的进度,检测状态等。这种架构带来诸多好处,例如:架构
这是 gh-ost 默认的工做模式,它会查看从库状况,找到集群的主库而且链接上去。修改操做的具体步骤是app
事实上,即便把从库改为 Row 格式,这仍然是对主库侵入最少的工做模式。less
若是没有从库,或者不想在从库上操做,那直接用主库也是能够的。gh-ost 就会在主库上直接作全部的操做。仍然能够在上面查看主从复制延迟。
这种模式会在从库上作修改。gh-ost 仍然会连上主库,但全部操做都是在从库上作的,不会对主库产生任何影响。在操做过程当中,gh-ost 也会不时地暂停,以便从库的数据能够保持最新。
option | meaning | default value |
---|---|---|
--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
|
为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
|
在每次迭代中处理的行数量(容许范围:100-100000) | 1000 |
--concurrent-rowcount | 该参数若是为True,则进行row-copy以后,估算统计行数(使用explain select count(*)方式),并调整ETA时间,不然,gh-ost首先预估统计行数,而后开始row-copy | true |
--conf
|
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将会因为该数据库严重负载而中止并退出 | |
--critical-load-hibernate-seconds
|
负载达到critical-load时,gh-ost在指定的时间内进入休眠状态。 它不会读/写任何来自任何服务器的任何内容 | |
--critical-load-interval-millis
|
当值为0时,当达到-critical-load,gh-ost当即退出。当值不为0时,当达到-critical-load,gh-ost会在-critical-load-interval-millis秒数后,再次进行检查,再次检查依旧达到-critical-load,gh-ost将会退出 | |
--cut-over
|
选择cut-over类型:atomic/two-step,atomic(默认)类型的cut-over是github的算法,two-step采用的是facebook-OSC的算法 | |
--cut-over-exponential-backoff | ||
--cut-over-lock-timeout-seconds
|
gh-ost在cut-over阶段最大的锁等待时间,当锁超时时,gh-ost的cut-over将重试 | 3 |
--database string | 数据库名 | |
--debug | debug模式 | |
--default-retries
|
各类操做在panick前重试次数 | 60 |
--discard-foreign-keys | 该参数针对一个有外键的表,在gh-ost建立ghost表时,并不会为ghost表建立外键。该参数很适合用于删除外键,除此以外,请谨慎使用 | |
--dml-batch-size
|
在单个事务中应用DML事件的批量大小(范围1-100) | 1 |
--exact-rowcount | 准确统计表行数(使用select count(*)的方式),获得更准确的预估时间 | |
--execute | 实际执行alter&migrate表,默认为noop,不执行,仅仅作测试并退出,若是想要ALTER TABLE语句真正落实到数据库中去,须要明确指定-execute | |
--exponential-backoff-max-interval
|
||
--force-named-cut-over | 若是为true,则unpostpone / cut-over交互式命令必须命名迁移的表 | |
--force-table-names
|
在临时表上使用的表名前缀 | |
--heartbeat-interval-millis
|
gh-ost心跳频率值 | 500 |
--hooks-hint
|
任意消息经过 GH_OST_HOOKS_HINT 注入到 hook | |
--hooks-path
|
hook文件存放目录(默认为empty,即禁用hook)。hook会在这个目录下寻找符合约定命名的hook文件来执行 | |
--host
|
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
|
MySQL 主密码 | |
--master-user
|
MysQL主帐号 | |
--max-lag-millis
|
主从复制最大延迟时间,当主从复制延迟时间超过该值后,gh-ost将采起节流(throttle)措施 | 1500s |
--max-load
|
逗号分隔状态名称=阈值,如:'Threads_running=100,Threads_connected=500'. When status exceeds threshold, app throttles writes | |
--migrate-on-replica | gh-ost的数据迁移(migrate)运行在从库上,而不是主库上 | |
--nice-ratio
|
每次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
|
当这个文件被建立,gh-ost将会当即退出 | |
--password
|
MySQL密码 | |
--port
|
MySQL端口,最好用从库 | |
--postpone-cut-over-flag-file
|
当这个文件存在的时候,gh-ost的cut-over阶段将会被推迟,数据仍然在复制,直到该文件被删除 | |
--quiet | 静默模式 | |
--replica-server-id
|
gh-ost的server_id | |
--replication-lag-query
|
弃用 | |
--serve-socket-file
|
gh-ost的socket文件绝对路径 | |
--serve-tcp-port
|
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
|
表名 | |
--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
|
当该文件被建立后,gh-ost操做当即中止。该参数能够用在多个gh-ost同时操做的时候,建立一个文件,让全部的gh-ost操做中止,或者删除这个文件,让全部的gh-ost操做恢复 | |
--throttle-control-replicas
|
列出全部须要被检查主从复制延迟的从库 | |
--throttle-flag-file
|
当该文件被建立后,gh-ost操做当即中止。该参数适合控制单个gh-ost操做。-throttle-additional-flag-file string适合控制多个gh-ost操做 | |
--throttle-http
|
||
--throttle-query string | 节流查询。每秒钟执行一次。当返回值=0时不须要节流,当返回值>0时,须要执行节流操做。该查询会在数据迁移(migrated)服务器上操做,因此请确保该查询是轻量级的 | |
--timestamp-old-table | 在旧表名中使用时间戳。 这会使旧表名称具备惟一且无冲突的交叉迁移 | |
--tungsten | 告诉gh-ost你正在运行的是一个tungsten-replication拓扑结构 | |
--user
|
MYSQL用户 | |
--verbose | ||
--version |
gh-ost --user="root" --password="root" --host=192.168.1.101 --database="test" --table="t1" --alter="ADD COLUMN cc2 varchar(10),add column cc3 int not null default 0 comment 'test' " --allow-on-master --execute
gh-ost --user="root" --password="root" --host=192.168.1.102 --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
gh-ost --user="root" --password="root" --host=192.168.1.102 --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
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
gh-ost --user="root" --password="root" --host=192.168.1.101 --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
10.1.4.1.暂停操做:
#暂停 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