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的工具。