OS:centos 6.5
DB:percona server 5.6.32
gh-ost:1.0.21
DB-M:192.168.128.128
DB-S : 192.168.128.129
简介:
gh-ost是github开源的一款在线执行sql的开源工具。可以用于在线表结构变更(特别是大表,原因下面讲)
在线表结构变更,目前常用有以下几种方式:
1.小表:online ddl (5.6及其以后版本)
创建临时表为更改后的表结构,更新临时表数据,原表的新入数据记录入内存中的alter log,rename
2.大表:pt-ost工具。触发器机制
3.大表:gh-ost。,创建临时表。无触发器,根据binlog机制
测试:
test库下usertb表(1000w)
gh-ost测试:
gh-ost \
–max-load=Threads_running=25 \
–critical-load=Threads_running=64 \
–chunk-size=1000 \
–throttle-control-replicas=”192.168.128.129:3306″ \
–max-lag-millis=1500 \
–initially-drop-old-table \
–initially-drop-ghost-table \
–initially-drop-socket-file \
–ok-to-drop-table \
–conf=”/etc/my.cnf” \
–host=”192.168.128.129″ \
–port=3306 \
–user=”root” \
–password=”thy” \
–database=”test” \
–table=”usertb” \
–verbose \
–alter=”add ps_id int(10) default null” \
–switch-to-rbr \
–allow-master-master \
–cut-over=default \
–default-retries=120 \
–panic-flag-file=/tmp/ghost.panic.flag \
–postpone-cut-over-flag-file=/tmp/ghost.postpone.flag \
–execute
执行过程中
128:
129:
最后:
结论:从截图可以看出,gh-ost的原理是利用了M-S和binlog的工作模式,在无触发器的机制上最大程度的减少了对主库的侵入以及MDL对于源数据的保护,后期可以考虑作为大表ddl的工具。