天天看點

線上ddl變更工具​ gh-ost

  • 工作過程
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
  1. -allow-master-master:是否允許gh-ost運作在雙主複制架構中,一般與-assume-master-host參數一起使用

  2. -allow-nullable-unique-key:允許gh-ost在資料遷移依賴的唯一鍵可以為NULL,預設為不允許為NULL的唯一鍵。如果資料遷移(migrate)依賴的唯一鍵允許NULL值,則可能造成資料不正确,請謹慎使用。

  3. -allow-on-master:允許gh-ost直接運作在主庫上。預設gh-ost連接配接的從庫。

  4. -alter string:DDL語句

  5. -assume-master-host string:為gh-ost指定一個主庫,格式為”ip:port”或者”hostname:port”。在這主主架構裡比較有用,或則在gh-ost發現不到主的時候有用。

  6. -assume-rbr:确認gh-ost連接配接的資料庫執行個體的binlog_format=ROW的情況下,可以指定-assume-rbr,這樣可以禁止從庫上運作stop slave,start slave,執行gh-ost使用者也不需要SUPER權限。

  7. -chunk-size int:在每次疊代中處理的行數量(允許範圍:100-100000),預設值為1000。

  8. -concurrent-rowcount:該參數如果為True(預設值),則進行row-copy之後,估算統計行數(使用explain select count(*)方式),并調整ETA時間,否則,gh-ost首先預估統計行數,然後開始row-copy。

  9. -conf string:gh-ost的配置檔案路徑。

  10. -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将會由于該資料庫嚴重負載而停止并退出。

  11. -critical-load-hibernate-seconds int :負載達到critical-load時,gh-ost在指定的時間内進入休眠狀态。 它不會讀/寫任何來自任何伺服器的任何内容。

  12. -critical-load-interval-millis int:當值為0時,當達到-critical-load,gh-ost立即退出。當值不為0時,當達到-critical-load,gh-ost會在-critical-load-interval-millis秒數後,再次進行檢查,再次檢查依舊達到-critical-load,gh-ost将會退出。

  13. -cut-over string:選擇cut-over類型:atomic/two-step,atomic(預設)類型的cut-over是github的算法,two-step采用的是facebook-OSC的算法。

  14. -cut-over-exponential-backoff

  15. -cut-over-lock-timeout-seconds int:gh-ost在cut-over階段最大的鎖等待時間,當鎖逾時時,gh-ost的cut-over将重試。(預設值:3)

  16. -database string:資料庫名稱。

  17. -default-retries int:各種操作在panick前重試次數。(預設為60)

  18. -dml-batch-size int:在單個事務中應用DML事件的批量大小(範圍1-100)(預設值為10)

  19. -exact-rowcount:準确統計表行數(使用select count(*)的方式),得到更準确的預估時間。

  20. -execute:實際執行alter&migrate表,預設為noop,不執行,僅僅做測試并退出,如果想要ALTER TABLE語句真正落實到資料庫中去,需要明确指定-execute

  21. -exponential-backoff-max-interval int

  22. -force-named-cut-over:如果為true,則'unpostpone | cut-over'互動式指令必須命名遷移的表

  23. -heartbeat-interval-millis int:gh-ost心跳頻率值,預設為500

  24. -initially-drop-ghost-table:gh-ost操作之前,檢查并删除已經存在的ghost表。該參數不建議使用,請手動處理原來存在的ghost表。預設不啟用該參數,gh-ost直接退出操作。

  25. -initially-drop-old-table:gh-ost操作之前,檢查并删除已經存在的舊表。該參數不建議使用,請手動處理原來存在的ghost表。預設不啟用該參數,gh-ost直接退出操作。

  26. -initially-drop-socket-file:gh-ost強制删除已經存在的socket檔案。該參數不建議使用,可能會删除一個正在運作的gh-ost程式,導緻DDL失敗。

  27. -max-lag-millis int:主從複制最大延遲時間,當主從複制延遲時間超過該值後,gh-ost将采取節流(throttle)措施,預設值:1500s。

  28. -max-load string:逗号分隔狀态名稱=門檻值,如:'Threads_running=100,Threads_connected=500'. When status exceeds threshold, app throttles writes

  29. -migrate-on-replica:gh-ost的資料遷移(migrate)運作在從庫上,而不是主庫上。

  30. -nice-ratio float:每次chunk時間段的休眠時間,範圍[0.0…100.0]。0:每個chunk時間段不休眠,即一個chunk接着一個chunk執行;1:每row-copy 1毫秒,則另外休眠1毫秒;0.7:每row-copy 10毫秒,則另外休眠7毫秒。

  31. -ok-to-drop-table:gh-ost操作結束後,删除舊表,預設狀态是不删除舊表,會存在_tablename_del表。

  32. -panic-flag-file string:當這個檔案被建立,gh-ost将會立即退出。

  33. -password string :MySQL密碼

  34. -port int :MySQL端口,最好用從庫

  35. -postpone-cut-over-flag-file string:當這個檔案存在的時候,gh-ost的cut-over階段将會被推遲,資料仍然在複制,直到該檔案被删除。

  36. -skip-foreign-key-checks:确定你的表上沒有外鍵時,設定為'true',并且希望跳過gh-ost驗證的時間-skip-renamed-columns ALTER

  37. -switch-to-rbr:讓gh-ost自動将從庫的binlog_format轉換為ROW格式。

  38. -table string:表名

  39. -throttle-additional-flag-file string:當該檔案被建立後,gh-ost操作立即停止。該參數可以用在多個gh-ost同時操作的時候,建立一個檔案,讓所有的gh-ost操作停止,或者删除這個檔案,讓所有的gh-ost操作恢複。

  40. -throttle-control-replicas string:列出所有需要被檢查主從複制延遲的從庫。

  41. -throttle-flag-file string:當該檔案被建立後,gh-ost操作立即停止。該參數适合控制單個gh-ost操作。-throttle-additional-flag-file string适合控制多個gh-ost操作。

  42. -throttle-query string:節流查詢。每秒鐘執行一次。當傳回值=0時不需要節流,當傳回值>0時,需要執行節流操作。該查詢會在資料遷移(migrated)伺服器上操作,是以請確定該查詢是輕量級的。

  43. -timestamp-old-table:在舊表名中使用時間戳。這會使舊表名稱具有唯一且無沖突的交叉遷移。

  44. -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
           

繼續閱讀