- 工作过程
gh-ost 首先连接到主库上,根据 alter 语句创建幽灵表,
然后作为一个备库连接到其中一个真正的备库或者主库上(根据具体的参数来定),一边在主库上拷贝已有的数据到幽灵表,一边从备库上拉取增量数据的 binlog,然后不断的把 binlog 应用回主库。
等待全部数据同步完成,进行cut-over 幽灵表和原表切换。图中 cut-over 是最后一步,锁住主库的源表,等待 binlog 应用完毕,然后替换 gh-ost 表为源表。gh-ost 在执行中,会在原本的 binlog event 里面增加以下 hint 和心跳包,用来控制整个流程的进度,检测状态等。
- 安装
#https://github.com/github/gh-ost/releases
cd /tmp
下载gh-ost-binary-linux-20190214020851.tar.gz
tar -xvf gh-ost-binary-linux-20190214020851.tar.gz
cp gh-ost /usr/bin/
which gh-ost
- gh-ost --help
-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语句
-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权限。
-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将会由于该数据库严重负载而停止并退出。
-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:数据库名称。
-default-retries int:各种操作在panick前重试次数。(默认为60)
-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'交互式命令必须命名迁移的表
-heartbeat-interval-millis int:gh-ost心跳频率值,默认为500
-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失败。
-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阶段将会被推迟,数据仍然在复制,直到该文件被删除。
-skip-foreign-key-checks:确定你的表上没有外键时,设置为'true',并且希望跳过gh-ost验证的时间-skip-renamed-columns ALTER
-switch-to-rbr:让gh-ost自动将从库的binlog_format转换为ROW格式。
-table string:表名
-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-query string:节流查询。每秒钟执行一次。当返回值=0时不需要节流,当返回值>0时,需要执行节流操作。该查询会在数据迁移(migrated)服务器上操作,所以请确保该查询是轻量级的。
-timestamp-old-table:在旧表名中使用时间戳。这会使旧表名称具有唯一且无冲突的交叉迁移。
-user string :MYSQL用户
- 操作
#https://mp.weixin.qq.com/s?__biz=MzI4NjExMDA4NQ==&mid=2648451747&idx=1&sn=5dad442397fbe9b2410c5e13fd8ade16&chksm=f3c97249c4befb5fe76379cadb9f3204c268ff0b051312f29cefc6ca3270d67a6072de522f79&scene=21&key=2be50905ebf73239e61167e9572a9dc1540c9184c4ccc0efe633fe7cb6bc5e75c2df7829e583c4440ce1cd56a491f028837541c3d008bfb7456d841b8089b0dc5f97d97b4875def94e0bea188762a8ed&ascene=1&uin=Mjk1NTczNzI2Mg%3D%3D&devicetype=Windows+7&version=62060841&lang=zh_CN&pass_ticket=smE26CDAj05L1aNOitlC%2B0valz2l3u%2FojPmPkp4tLllsALIQNJKuWqZ1I554uwZb
gh-ost \
--ok-to-drop-table \
--initially-drop-ghost-table \
--initially-drop-socket-file \
--host="192.168.10.73" \
--port=3306 \
--user="lf" \
--password="123.com"\
--database="ylmonitor" \
--table="t_nginxupstream" \
--verbose \
--alter="add column test_field varchar(256) default '';" \
--serve-socket-file=/tmp/t1.sock \
--panic-flag-file=/tmp/ghost.panic.flag \
--allow-on-master \
--throttle-flag-file /tmp/1.log \
--execute
#暂停
echo throttle | socat - /tmp/t1.sock
#恢复
echo no-throttle | socat - /tmp/t1.sock
修改限速参数:
echo chunk-size=100 | socat - /tmp/t1.sock
echo max-lag-millis=200 | socat - /tmp/t1.sock
echo max-load=Thread_running=3 | socat - /tmp/t1.sock