国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫(kù)技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫(kù) - Mysql - MySQL在線(xiàn)DDL gh-ost使用總結(jié)

MySQL在線(xiàn)DDL gh-ost使用總結(jié)

2019-06-05 09:44jyzhou服務(wù)器之家 Mysql

在本篇內(nèi)容里小編給大家整理了關(guān)于MySQL在線(xiàn)DDL gh-ost使用方法和相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。

背景:

作為一個(gè)DBA,大表的DDL的變更大部分都是使用Percona的pt-online-schema-change,本文說(shuō)明下另一種工具gh-ost的使用:不依賴(lài)于觸發(fā)器,是因?yàn)樗峭ㄟ^(guò)模擬從庫(kù),在row binlog中獲取增量變更,再異步應(yīng)用到ghost表的。在使用gh-ost之前,可以先看GitHub 開(kāi)源的 MySQL 在線(xiàn)更改 Schema 工具【轉(zhuǎn)】文章或則官網(wǎng)了解其特性和原理。本文只對(duì)使用進(jìn)行說(shuō)明。

說(shuō)明:

1)下載安裝:https://github.com/github/gh-ost/tags

2)參數(shù)說(shuō)明:gh-ost --help

Usage of gh-ost:
 --aliyun-rds:是否在阿里云數(shù)據(jù)庫(kù)上執(zhí)行。true
 --allow-master-master:是否允許gh-ost運(yùn)行在雙主復(fù)制架構(gòu)中,一般與-assume-master-host參數(shù)一起使用
 --allow-nullable-unique-key:允許gh-ost在數(shù)據(jù)遷移依賴(lài)的唯一鍵可以為NULL,默認(rèn)為不允許為NULL的唯一鍵。如果數(shù)據(jù)遷移(migrate)依賴(lài)的唯一鍵允許NULL值,則可能造成數(shù)據(jù)不正確,請(qǐng)謹(jǐn)慎使用。
 --allow-on-master:允許gh-ost直接運(yùn)行在主庫(kù)上。默認(rèn)gh-ost連接的從庫(kù)。
 --alter string:DDL語(yǔ)句
 --approve-renamed-columns ALTER:如果你修改一個(gè)列的名字,gh-ost將會(huì)識(shí)別到并且需要提供重命名列名的原因,默認(rèn)情況下gh-ost是不繼續(xù)執(zhí)行的,除非提供-approve-renamed-columns ALTER。
 --ask-pass:MySQL密碼
 --assume-master-host string:為gh-ost指定一個(gè)主庫(kù),格式為”ip:port”或者”hostname:port”。在這主主架構(gòu)里比較有用,或則在gh-ost發(fā)現(xiàn)不到主的時(shí)候有用。
 --assume-rbr:確認(rèn)gh-ost連接的數(shù)據(jù)庫(kù)實(shí)例的binlog_format=ROW的情況下,可以指定-assume-rbr,這樣可以禁止從庫(kù)上運(yùn)行stop slave,start slave,執(zhí)行g(shù)h-ost用戶(hù)也不需要SUPER權(quán)限。
 --check-flag
 --chunk-size int:在每次迭代中處理的行數(shù)量(允許范圍:100-100000),默認(rèn)值為1000。
 --concurrent-rowcount:該參數(shù)如果為T(mén)rue(默認(rèn)值),則進(jìn)行row-copy之后,估算統(tǒng)計(jì)行數(shù)(使用explain select count(*)方式),并調(diào)整ETA時(shí)間,否則,gh-ost首先預(yù)估統(tǒng)計(jì)行數(shù),然后開(kāi)始row-copy。
 --conf string:gh-ost的配置文件路徑。
 --critical-load string:一系列逗號(hào)分隔的status-name=values組成,當(dāng)MySQL中status超過(guò)對(duì)應(yīng)的values,gh-ost將會(huì)退出。-critical-load Threads_connected=20,Connections=1500,指的是當(dāng)MySQL中的狀態(tài)值Threads_connected>20,Connections>1500的時(shí)候,gh-ost將會(huì)由于該數(shù)據(jù)庫(kù)嚴(yán)重負(fù)載而停止并退出。
  Comma delimited status-name=threshold, same format as --max-load. When status exceeds threshold, app panics and quits
 --critical-load-hibernate-seconds int :負(fù)載達(dá)到critical-load時(shí),gh-ost在指定的時(shí)間內(nèi)進(jìn)入休眠狀態(tài)。 它不會(huì)讀/寫(xiě)任何來(lái)自任何服務(wù)器的任何內(nèi)容。
 --critical-load-interval-millis int:當(dāng)值為0時(shí),當(dāng)達(dá)到-critical-load,gh-ost立即退出。當(dāng)值不為0時(shí),當(dāng)達(dá)到-critical-load,gh-ost會(huì)在-critical-load-interval-millis秒數(shù)后,再次進(jìn)行檢查,再次檢查依舊達(dá)到-critical-load,gh-ost將會(huì)退出。
 --cut-over string:選擇cut-over類(lèi)型:atomic/two-step,atomic(默認(rèn))類(lèi)型的cut-over是github的算法,two-step采用的是facebook-OSC的算法。
 --cut-over-exponential-backoff
 --cut-over-lock-timeout-seconds int:gh-ost在cut-over階段最大的鎖等待時(shí)間,當(dāng)鎖超時(shí)時(shí),gh-ost的cut-over將重試。(默認(rèn)值:3)
 --database string:數(shù)據(jù)庫(kù)名稱(chēng)。
 --debug:debug模式。
 --default-retries int:各種操作在panick前重試次數(shù)。(默認(rèn)為60)
 --discard-foreign-keys:該參數(shù)針對(duì)一個(gè)有外鍵的表,在gh-ost創(chuàng)建ghost表時(shí),并不會(huì)為ghost表創(chuàng)建外鍵。該參數(shù)很適合用于刪除外鍵,除此之外,請(qǐng)謹(jǐn)慎使用。
 --dml-batch-size int:在單個(gè)事務(wù)中應(yīng)用DML事件的批量大小(范圍1-100)(默認(rèn)值為10)
 --exact-rowcount:準(zhǔn)確統(tǒng)計(jì)表行數(shù)(使用select count(*)的方式),得到更準(zhǔn)確的預(yù)估時(shí)間。
 --execute:實(shí)際執(zhí)行alter&migrate表,默認(rèn)為noop,不執(zhí)行,僅僅做測(cè)試并退出,如果想要ALTER TABLE語(yǔ)句真正落實(shí)到數(shù)據(jù)庫(kù)中去,需要明確指定-execute
 --exponential-backoff-max-interval int
 --force-named-cut-over:如果為true,則'unpostpone | cut-over'交互式命令必須命名遷移的表
 --force-table-names string:在臨時(shí)表上使用的表名前綴
 --heartbeat-interval-millis int:gh-ost心跳頻率值,默認(rèn)為500
 --help
 --hooks-hint string:任意消息通過(guò)GH_OST_HOOKS_HINT注入到鉤子
 --hooks-path string:hook文件存放目錄(默認(rèn)為empty,即禁用hook)。hook會(huì)在這個(gè)目錄下尋找符合約定命名的hook文件來(lái)執(zhí)行。
 --host string :MySQL IP/hostname
 --initially-drop-ghost-table:gh-ost操作之前,檢查并刪除已經(jīng)存在的ghost表。該參數(shù)不建議使用,請(qǐng)手動(dòng)處理原來(lái)存在的ghost表。默認(rèn)不啟用該參數(shù),gh-ost直接退出操作。
 --initially-drop-old-table:gh-ost操作之前,檢查并刪除已經(jīng)存在的舊表。該參數(shù)不建議使用,請(qǐng)手動(dòng)處理原來(lái)存在的ghost表。默認(rèn)不啟用該參數(shù),gh-ost直接退出操作。
 --initially-drop-socket-file:gh-ost強(qiáng)制刪除已經(jīng)存在的socket文件。該參數(shù)不建議使用,可能會(huì)刪除一個(gè)正在運(yùn)行的gh-ost程序,導(dǎo)致DDL失敗。
 --master-password string :MySQL 主密碼
 --master-user string:MysQL主賬號(hào)
 --max-lag-millis int:主從復(fù)制最大延遲時(shí)間,當(dāng)主從復(fù)制延遲時(shí)間超過(guò)該值后,gh-ost將采取節(jié)流(throttle)措施,默認(rèn)值:1500s。
 --max-load string:逗號(hào)分隔狀態(tài)名稱(chēng)=閾值,如:'Threads_running=100,Threads_connected=500'. When status exceeds threshold, app throttles writes
 --migrate-on-replica:gh-ost的數(shù)據(jù)遷移(migrate)運(yùn)行在從庫(kù)上,而不是主庫(kù)上。 
 --nice-ratio float:每次chunk時(shí)間段的休眠時(shí)間,范圍[0.0…100.0]。0:每個(gè)chunk時(shí)間段不休眠,即一個(gè)chunk接著一個(gè)chunk執(zhí)行;1:每row-copy 1毫秒,則另外休眠1毫秒;0.7:每row-copy 10毫秒,則另外休眠7毫秒。
 --ok-to-drop-table:gh-ost操作結(jié)束后,刪除舊表,默認(rèn)狀態(tài)是不刪除舊表,會(huì)存在_tablename_del表。
 --panic-flag-file string:當(dāng)這個(gè)文件被創(chuàng)建,gh-ost將會(huì)立即退出。
 --password string :MySQL密碼
 --port int :MySQL端口,最好用從庫(kù)
 --postpone-cut-over-flag-file string:當(dāng)這個(gè)文件存在的時(shí)候,gh-ost的cut-over階段將會(huì)被推遲,數(shù)據(jù)仍然在復(fù)制,直到該文件被刪除。
 --quiet:靜默模式。
 --replica-server-id uint : gh-ost的server_id
 --replication-lag-query string:棄用
 --serve-socket-file string:gh-ost的socket文件絕對(duì)路徑。
 --serve-tcp-port int:gh-ost使用端口,默認(rèn)為關(guān)閉端口。
 --skip-foreign-key-checks:確定你的表上沒(méi)有外鍵時(shí),設(shè)置為'true',并且希望跳過(guò)gh-ost驗(yàn)證的時(shí)間-skip-renamed-columns ALTER
 --skip-renamed-columns ALTER:如果你修改一個(gè)列的名字(如change column),gh-ost將會(huì)識(shí)別到并且需要提供重命名列名的原因,默認(rèn)情況下gh-ost是不繼續(xù)執(zhí)行的。該參數(shù)告訴gh-ost跳該列的數(shù)據(jù)遷移,讓gh-ost把重命名列作為無(wú)關(guān)緊要的列。該操作很危險(xiǎn),你會(huì)損失該列的所有值。
 --stack:添加錯(cuò)誤堆棧追蹤。
 --switch-to-rbr:讓gh-ost自動(dòng)將從庫(kù)的binlog_format轉(zhuǎn)換為ROW格式。
 --table string:表名
 --test-on-replica:在從庫(kù)上測(cè)試gh-ost,包括在從庫(kù)上數(shù)據(jù)遷移(migration),數(shù)據(jù)遷移完成后stop slave,原表和ghost表立刻交換而后立刻交換回來(lái)。繼續(xù)保持stop slave,使你可以對(duì)比兩張表。
 --test-on-replica-skip-replica-stop:當(dāng)-test-on-replica執(zhí)行時(shí),該參數(shù)表示該過(guò)程中不用stop slave。
 --throttle-additional-flag-file string:當(dāng)該文件被創(chuàng)建后,gh-ost操作立即停止。該參數(shù)可以用在多個(gè)gh-ost同時(shí)操作的時(shí)候,創(chuàng)建一個(gè)文件,讓所有的gh-ost操作停止,或者刪除這個(gè)文件,讓所有的gh-ost操作恢復(fù)。
 --throttle-control-replicas string:列出所有需要被檢查主從復(fù)制延遲的從庫(kù)。
 --throttle-flag-file string:當(dāng)該文件被創(chuàng)建后,gh-ost操作立即停止。該參數(shù)適合控制單個(gè)gh-ost操作。-throttle-additional-flag-file string適合控制多個(gè)gh-ost操作。
 --throttle-http string
 --throttle-query string:節(jié)流查詢(xún)。每秒鐘執(zhí)行一次。當(dāng)返回值=0時(shí)不需要節(jié)流,當(dāng)返回值>0時(shí),需要執(zhí)行節(jié)流操作。該查詢(xún)會(huì)在數(shù)據(jù)遷移(migrated)服務(wù)器上操作,所以請(qǐng)確保該查詢(xún)是輕量級(jí)的。
 --timestamp-old-table:在舊表名中使用時(shí)間戳。 這會(huì)使舊表名稱(chēng)具有唯一且無(wú)沖突的交叉遷移
 --tungsten:告訴gh-ost你正在運(yùn)行的是一個(gè)tungsten-replication拓?fù)浣Y(jié)構(gòu)。
 --user string :MYSQL用戶(hù)
 --verbose
 --version

3)使用說(shuō)明:條件是操作的MySQL上需要的binlog模式是ROW。如果在一個(gè)從上測(cè)試也必須是ROW模式,還要開(kāi)啟log_slave_updates。根據(jù)上面的參數(shù)說(shuō)明按照需求進(jìn)行調(diào)整。

環(huán)境:主庫(kù):192.168.163.131;從庫(kù):192.168.163.130

DDL過(guò)程:

① 檢查有沒(méi)有外鍵和觸發(fā)器。
② 檢查表的主鍵信息。
③ 檢查是否主庫(kù)或從庫(kù),是否開(kāi)啟log_slave_updates,以及binlog信息 
④ 檢查gho和del結(jié)尾的臨時(shí)表是否存在
⑤ 創(chuàng)建ghc結(jié)尾的表,存數(shù)據(jù)遷移的信息,以及binlog信息等 
---以上校驗(yàn)階段
⑥ 初始化stream的連接,添加binlog的監(jiān)聽(tīng)
---以下遷移階段
⑥ 創(chuàng)建gho結(jié)尾的臨時(shí)表,執(zhí)行DDL在gho結(jié)尾的臨時(shí)表上
⑦ 開(kāi)啟事務(wù),按照主鍵id把源表數(shù)據(jù)寫(xiě)入到gho結(jié)尾的表上,再提交,以及binlog apply。
---以下cut-over階段
⑧ lock源表,rename 表:rename 源表 to 源_del表,gho表 to 源表。
⑨ 清理ghc表。

1. 單實(shí)例上DDL: 單個(gè)實(shí)例相當(dāng)于主庫(kù),需要開(kāi)啟--allow-on-master參數(shù)和ROW模式。

gh-ost --user="root" --password="root" --host=192.168.163.131 --database="test" --table="t1" --alter="ADD COLUMN cc2 varchar(10),add column cc3 int not null default 0 comment 'test' " --allow-on-master --execute

2. 主從上DDL:

有2個(gè)選擇,一是按照1直接在主上執(zhí)行同步到從上,另一個(gè)連接到從庫(kù),在主庫(kù)做遷移(只要保證從庫(kù)的binlog為ROW即可,主庫(kù)不需要保證):

gh-ost --user="root" --password="root" --host=192.168.163.130 --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

此時(shí)的操作大致是:

  • 行數(shù)據(jù)在主庫(kù)上讀寫(xiě)
  • 讀取從庫(kù)的二進(jìn)制日志,將變更應(yīng)用到主庫(kù)
  • 在從庫(kù)收集表格式,字段&索引,行數(shù)等信息
  • 在從庫(kù)上讀取內(nèi)部的變更事件(如心跳事件)
  • 在主庫(kù)切換表

在執(zhí)行DDL中,從庫(kù)會(huì)執(zhí)行一次stop/start slave,要是確定從的binlog是ROW的話(huà)可以添加參數(shù):--assume-rbr。如果從庫(kù)的binlog不是ROW,可以用參數(shù)--switch-to-rbr來(lái)轉(zhuǎn)換成ROW,此時(shí)需要注意的是執(zhí)行完畢之后,binlog模式不會(huì)被轉(zhuǎn)換成原來(lái)的值。--assume-rbr和--switch-to-rbr參數(shù)不能一起使用。

3. 在從上進(jìn)行DDL測(cè)試:

gh-ost --user="root" --password="root" --host=192.168.163.130 --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

參數(shù)--test-on-replica:在從庫(kù)上測(cè)試gh-ost,包括在從庫(kù)上數(shù)據(jù)遷移(migration),數(shù)據(jù)遷移完成后stop slave,原表和ghost表立刻交換而后立刻交換回來(lái)。繼續(xù)保持stop slave,使你可以對(duì)比兩張表。如果不想stop slave,則可以再添加參數(shù):--test-on-replica-skip-replica-stop

上面三種是gh-ost操作模式,上面的操作中,到最后不會(huì)清理臨時(shí)表,需要手動(dòng)清理,再下次執(zhí)行之前果然臨時(shí)表還存在,則會(huì)執(zhí)行失敗,可以通過(guò)參數(shù)進(jìn)行刪除:

--initially-drop-ghost-table:gh-ost操作之前,檢查并刪除已經(jīng)存在的ghost表。該參數(shù)不建議使用,請(qǐng)手動(dòng)處理原來(lái)存在的ghost表。默認(rèn)不啟用該參數(shù),gh-ost直接退出操作。

--initially-drop-old-table:gh-ost操作之前,檢查并刪除已經(jīng)存在的舊表。該參數(shù)不建議使用,請(qǐng)手動(dòng)處理原來(lái)存在的ghost表。默認(rèn)不啟用該參數(shù),gh-ost直接退出操作。

--initially-drop-socket-file:gh-ost強(qiáng)制刪除已經(jīng)存在的socket文件。該參數(shù)不建議使用,可能會(huì)刪除一個(gè)正在運(yùn)行的gh-ost程序,導(dǎo)致DDL失敗。

--ok-to-drop-table:gh-ost操作結(jié)束后,刪除舊表,默認(rèn)狀態(tài)是不刪除舊表,會(huì)存在_tablename_del表。

還有其他的一些參數(shù),比如:--exact-rowcount、--max-lag-millis、--max-load等等,可以看上面的說(shuō)明,具體大部分常用的參數(shù)命令如下:

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

4)額外說(shuō)明:終止、暫停、限速

gh-ost --user="root" --password="root" --host=192.168.163.131 --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

① 標(biāo)示文件終止運(yùn)行:--panic-flag-file

創(chuàng)建文件終止運(yùn)行,例子中創(chuàng)建/tmp/gh-ost.panic.t1.flag文件,終止正在運(yùn)行的gh-ost,臨時(shí)文件清理需要手動(dòng)進(jìn)行。

② 表示文件禁止cut-over進(jìn)行,即禁止表名切換,數(shù)據(jù)復(fù)制正常進(jìn)行。--postpone-cut-over-flag-file

創(chuàng)建文件延遲cut-over進(jìn)行,即推遲切換操作。例子中創(chuàng)建/tmp/ghost.postpone.t1.flag文件,gh-ost 會(huì)完成行復(fù)制,但并不會(huì)切換表,它會(huì)持續(xù)的將原表的數(shù)據(jù)更新操作同步到臨時(shí)表中。

③ 使用socket監(jiān)聽(tīng)請(qǐng)求,操作者可以在命令運(yùn)行后更改相應(yīng)的參數(shù)。--serve-socket-file,--serve-tcp-port(默認(rèn)關(guān)閉)

創(chuàng)建socket文件進(jìn)行監(jiān)聽(tīng),通過(guò)接口進(jìn)行參數(shù)調(diào)整,當(dāng)執(zhí)行操作的過(guò)程中發(fā)現(xiàn)負(fù)載、延遲上升了,不得不終止操作,重新配置參數(shù),如 chunk-size,然后重新執(zhí)行操作命令,可以通過(guò)scoket接口進(jìn)行動(dòng)態(tài)調(diào)整。如:

暫停操作:

#暫停
echo throttle | socat - /tmp/gh-ost.test.t1.sock
#恢復(fù)
echo no-throttle | socat - /tmp/gh-ost.test.t1.sock

修改限速參數(shù):

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

4)和pt-online-schema-change對(duì)比測(cè)試

1. 表沒(méi)有寫(xiě)入并且參數(shù)為默認(rèn)的情況下,二者DDL操作時(shí)間差不多,畢竟都是copy row操作。

2. 表有大量寫(xiě)入(sysbench)的情況下,因?yàn)閜t-osc是多線(xiàn)程處理的,很快就能執(zhí)行完成,而gh-ost是模擬“從”單線(xiàn)程應(yīng)用的,極端的情況下,DDL操作非常困難的執(zhí)行完畢。

結(jié)論:雖然pt-osc不需要觸發(fā)器,對(duì)于主庫(kù)的壓力和性能影響也小很多,但是針對(duì)高并發(fā)的場(chǎng)景進(jìn)行DDL效率還是比pt-osc低,所以還是需要在業(yè)務(wù)低峰的時(shí)候處理。相關(guān)的測(cè)試可以看gh-ost和pt-osc性能對(duì)比。

5)封裝腳本:

環(huán)境:M:192.168.163.131(ROW),S:192.168.163.130/132

封裝腳本:gh-ost.py

#!/bin/env python
# -*- encoding: utf-8 -*-
#----------------------------------------------
# Purpose:  gh-ost
# Created:  2018-06-16
#----------------------------------------------

import MySQLdb
import re
import sys
import time
import subprocess
import os
from optparse import OptionParser

def calc_time(func):
 def _deco(*args, **kwargs):
  begin_time = time.time()
  func(*args, **kwargs)
  cost_time = time.time() - begin_time
  print 'cost time: %ss' % round(cost_time,2)
 return _deco

def get_table_count(conn,dbname,tbname):
 query = ''' SELECT count(*) FROM %s.%s ''' %(dbname,tbname)
 cursor = conn.cursor()
 cursor.execute(query)
 row_nums = cursor.fetchone()
 cursor.close()
 conn.close() 
 return row_nums

def online_ddl(conn,ddl_cmd):
 cursor = conn.cursor()
 cursor.execute(ddl_cmd)
 conn.commit()
 cursor.close()
 conn.close() 

#@calc_time
def run_cmd(cmd):
 p = subprocess.Popen(cmd, shell=True)
 return p,p.pid

def drop_ghost_table(conn,ghost_name_list):
 try:
  cursor = conn.cursor()
  query = ''' DROP TABLE IF EXISTS %s; ''' %(ghost_name_list)
  cursor.execute(query)
  conn.commit()
  cursor.close()
  conn.close()
 except Exception,e:
  print e

if __name__ == "__main__":
 parser = OptionParser()
 parser.add_option("-P", "--Port", help="Port for search", dest="port")
 parser.add_option("-D", "--Dbname", help="the Dbname to use", dest="dbname")
 parser.add_option("-T", "--Table", help="the Table to use", dest="tablename")

 (options, args) = parser.parse_args()

 if not options.port:
  print 'params port need to apply'
  exit()

 if not options.dbname:
  print 'params dbname need to apply'
  exit()

 if not options.tablename:
  print 'params tablename need to apply'
  exit()

 gh_ost_socket = '/tmp/gh-ost.%s.%s.sock' %(options.dbname,options.tablename)
 #終止標(biāo)志
 panic_flag  = '/tmp/gh-ost.panic.%s.%s.flag' %(options.dbname,options.tablename)
 # postpone_flag = '/tmp/gh-ost.postpone.%s.%s.flag' %(options.dbname,options.tablename)
 #暫停標(biāo)志
 throttle_flag = '/tmp/gh-ost.throttle.%s.%s' %(options.dbname,options.tablename)
# socket = '/data/%s/tmp/mysql.sock' %(options.port)
 socket = '/var/run/mysqld/mysqld.sock'

 

 get_conn = MySQLdb.connect(host='192.168.163.131', port=int(options.port), user='root', passwd='root', db=options.dbname, unix_socket=socket,charset='utf8')
 conn  = MySQLdb.connect(host='192.168.163.131', port=int(options.port), user='root', passwd='root', db=options.dbname, unix_socket=socket,charset='utf8')
 
 (table_count,) = get_table_count(get_conn,options.dbname,options.tablename)
 print ("\033[0;32m%s\033[0m" % "表的數(shù)量:%s" %table_count)

 DDL_CMD = raw_input('Enter DDL CMD : ').replace('`','')

 gh_command_list = re.split('[ ]+',DDL_CMD)
 if gh_command_list[0].upper() == 'CHANGE' and gh_command_list[1] != gh_command_list[2]:
  print ("\033[0;31m%s\033[0m" % "renamed columns' data will be lost,pt-osc exit...")
  exit()

 if table_count <= 10000:
  ddl = ''' ALTER TABLE %s %s ''' %(options.tablename,DDL_CMD)
  print ("\033[0;36m%s\033[0m" %ddl)
  print ("\033[0;32m%s\033[0m" % "online ddl ...")
  online_ddl(conn,ddl)
  print ("\033[0;32m%s\033[0m" % "執(zhí)行完成 ...")
  exit()

 else:
  MAX_LOAD = raw_input('Enter Max Threads_running【25】 : ')
  if not MAX_LOAD:
   Threads_running = 25 
  else:
   try:
    Threads_running = int(MAX_LOAD)
   except ValueError:
    print ("\033[0;31m%s\033[0m" % "輸入類(lèi)型錯(cuò)誤,退出...")
    exit()

  CHUNK_SIZE = raw_input('Enter Max chunk-size【1000】 : ')
  if not CHUNK_SIZE:
   chunk_size = 1000
  else:
   try:
    chunk_size = int(CHUNK_SIZE)
   except ValueError:
    print ("\033[0;31m%s\033[0m" % "輸入類(lèi)型錯(cuò)誤,退出...")
    exit()

  print ("\033[0;32m%s\033[0m" % "gh-ost ddl ...")
  #--postpone-cut-over-flag-file=%s 
  gh_command = '''/usr/bin/gh-ost --user="root" --password="root" --host=192.168.163.131 --port=%s --database="%s" --table="%s" --allow-on-master --max-load='Threads_running=%d' --chunk-size=%d --serve-socket-file=%s --panic-flag-file=%s --throttle-additional-flag-file=%s --alter="%s" --execute ''' %(options.port,options.dbname,options.tablename,Threads_running,chunk_size,gh_ost_socket,panic_flag,throttle_flag,DDL_CMD)
  print ("\033[0;36m%s\033[0m" %gh_command)

 
  child,pid = run_cmd(gh_command)
  print ("\033[0;31mgh-ost's PID:%s\033[0m" %pid)
  print ("\033[0;33m創(chuàng)建:【touch %s】文件,暫停DDL ...\033[0m" %throttle_flag)
  try:
   child.wait()
  except:
   child.terminate()
   #clean
   ghost_name_list = '_%s_ghc,_%s_gho' %(options.tablename,options.tablename)
   drop_ghost_table(conn,ghost_name_list)
   if os.path.exists(gh_ost_socket):
    os.system('rm -r %s' %gh_ost_socket)
    print ("\033[0;32m%s\033[0m" % "清理完成 ...")
    exit()
   print ("\033[0;32m%s\033[0m" % "清理完成 ...")
   exit()
  finally :
   pass

運(yùn)行:

root@test2:~# python gh-ost.py -P3306 -Dtest -Tzjy
表的數(shù)量:1310720
Enter DDL CMD : ADD COLUMN q1 varchar(10),ADD COLUMN q2 varchar(10)
Enter Max Threads_running【25】 : 10
Enter Max chunk-size【1000】 : 200
gh-ost ddl ...
/usr/bin/gh-ost --user="root" --password="root" --host=192.168.163.131 --port=3306 --database="test" --table="zjy" --allow-on-master --max-load='Threads_running=10' --chunk-size=200 --serve-socket-file=/tmp/gh-ost.test.zjy.sock --panic-flag-file=/tmp/gh-ost.panic.test.zjy.flag --throttle-additional-flag-file=/tmp/gh-ost.throttle.test.zjy --alter="ADD COLUMN q1 varchar(10),ADD COLUMN q2 varchar(10)" --execute 
gh-ost's PID:2105
創(chuàng)建:【touch /tmp/gh-ost.throttle.test.zjy】文件,暫停DDL ...
2018/06/17 14:37:37 binlogsyncer.go:79: [info] create BinlogSyncer with config {99999 mysql 192.168.163.131 3306 root false false <nil>}
2018/06/17 14:37:37 binlogsyncer.go:246: [info] begin to sync binlog from position (mysql-bin.000013, 31197930)
2018/06/17 14:37:37 binlogsyncer.go:139: [info] register slave for master server 192.168.163.131:3306
2018/06/17 14:37:37 binlogsyncer.go:573: [info] rotate to (mysql-bin.000013, 31197930)
# Migrating `test`.`zjy`; Ghost table is `test`.`_zjy_gho`
# Migrating test2:3306; inspecting test2:3306; executing on test2
# Migration started at Sun Jun 17 14:37:37 +0800 2018
# chunk-size: 200; max-lag-millis: 1500ms; dml-batch-size: 10; max-load: Threads_running=10; critical-load: ; nice-ratio: 0.000000
# throttle-additional-flag-file: /tmp/gh-ost.throttle.test.zjy 
# panic-flag-file: /tmp/gh-ost.panic.test.zjy.flag
# Serving on unix socket: /tmp/gh-ost.test.zjy.sock
Copy: 0/1305600 0.0%; Applied: 0; Backlog: 0/1000; Time: 0s(total), 0s(copy); streamer: mysql-bin.000013:31199542; State: migrating; ETA: N/A
Copy: 0/1305600 0.0%; Applied: 0; Backlog: 0/1000; Time: 1s(total), 1s(copy); streamer: mysql-bin.000013:31202866; State: migrating; ETA: N/A
Copy: 44400/1305600 3.4%; Applied: 0; Backlog: 0/1000; Time: 2s(total), 2s(copy); streamer: mysql-bin.000013:33352548; State: migrating; ETA: 56s
Copy: 91200/1305600 7.0%; Applied: 0; Backlog: 0/1000; Time: 3s(total), 3s(copy); streamer: mysql-bin.000013:35598132; State: migrating; ETA: 39s
Copy: 135200/1305600 10.4%; Applied: 0; Backlog: 0/1000; Time: 4s(total), 4s(copy); streamer: mysql-bin.000013:37727925; State: migrating; ETA: 34s
Copy: 174000/1305600 13.3%; Applied: 0; Backlog: 0/1000; Time: 5s(total), 5s(copy); streamer: mysql-bin.000013:39588956; State: migrating; ETA: 32s
Copy: 212200/1305600 16.3%; Applied: 0; Backlog: 0/1000; Time: 6s(total), 6s(copy); streamer: mysql-bin.000013:41430090; State: migrating; ETA: 30s
Copy: 254800/1305600 19.5%; Applied: 0; Backlog: 0/1000; Time: 7s(total), 7s(copy); streamer: mysql-bin.000013:43483555; State: migrating; ETA: 28s
Copy: 303600/1305600 23.3%; Applied: 0; Backlog: 0/1000; Time: 8s(total), 8s(copy); streamer: mysql-bin.000013:45834978; State: migrating; ETA: 26s
Copy: 351200/1305600 26.9%; Applied: 0; Backlog: 0/1000; Time: 9s(total), 9s(copy); streamer: mysql-bin.000013:48128675; State: migrating; ETA: 24s
Copy: 401400/1305600 30.7%; Applied: 0; Backlog: 0/1000; Time: 10s(total), 10s(copy); streamer: mysql-bin.000013:50547454; State: migrating; ETA: 22s
Copy: 451200/1305600 34.6%; Applied: 0; Backlog: 0/1000; Time: 11s(total), 11s(copy); streamer: mysql-bin.000013:52946991; State: migrating; ETA: 20s
Copy: 490000/1305600 37.5%; Applied: 0; Backlog: 0/1000; Time: 12s(total), 12s(copy); streamer: mysql-bin.000013:54817320; State: migrating; ETA: 19s
Copy: 529600/1305600 40.6%; Applied: 0; Backlog: 0/1000; Time: 13s(total), 13s(copy); streamer: mysql-bin.000013:56735431; State: migrating; ETA: 19s
Copy: 589200/1305600 45.1%; Applied: 0; Backlog: 0/1000; Time: 14s(total), 14s(copy); streamer: mysql-bin.000013:59606450; State: migrating; ETA: 17s
Copy: 639400/1305600 49.0%; Applied: 0; Backlog: 0/1000; Time: 15s(total), 15s(copy); streamer: mysql-bin.000013:62025561; State: migrating; ETA: 15s
Copy: 695200/1305600 53.2%; Applied: 0; Backlog: 0/1000; Time: 16s(total), 16s(copy); streamer: mysql-bin.000013:64704138; State: migrating; ETA: 14s
Copy: 751200/1305600 57.5%; Applied: 0; Backlog: 0/1000; Time: 17s(total), 17s(copy); streamer: mysql-bin.000013:67401961; State: migrating; ETA: 12s
Copy: 803800/1305600 61.6%; Applied: 0; Backlog: 0/1000; Time: 18s(total), 18s(copy); streamer: mysql-bin.000013:69935884; State: migrating; ETA: 11s
Copy: 856400/1305600 65.6%; Applied: 0; Backlog: 0/1000; Time: 19s(total), 19s(copy); streamer: mysql-bin.000013:72470455; State: migrating; ETA: 9s
Copy: 907400/1305600 69.5%; Applied: 0; Backlog: 0/1000; Time: 20s(total), 20s(copy); streamer: mysql-bin.000013:74927401; State: migrating; ETA: 8s
Copy: 958800/1305600 73.4%; Applied: 0; Backlog: 0/1000; Time: 21s(total), 21s(copy); streamer: mysql-bin.000013:77404243; State: migrating; ETA: 7s
Copy: 999200/1305600 76.5%; Applied: 0; Backlog: 0/1000; Time: 22s(total), 22s(copy); streamer: mysql-bin.000013:79351223; State: migrating; ETA: 6s
Copy: 1009600/1305600 77.3%; Applied: 0; Backlog: 0/1000; Time: 23s(total), 23s(copy); streamer: mysql-bin.000013:79855229; State: migrating; ETA: 6s
Copy: 1059600/1305600 81.2%; Applied: 0; Backlog: 0/1000; Time: 24s(total), 24s(copy); streamer: mysql-bin.000013:82264712; State: migrating; ETA: 5s
Copy: 1107200/1305600 84.8%; Applied: 0; Backlog: 0/1000; Time: 25s(total), 25s(copy); streamer: mysql-bin.000013:84558411; State: migrating; ETA: 4s
Copy: 1147000/1305600 87.9%; Applied: 0; Backlog: 0/1000; Time: 26s(total), 26s(copy); streamer: mysql-bin.000013:86486148; State: migrating; ETA: 3s
Copy: 1198000/1305600 91.8%; Applied: 0; Backlog: 0/1000; Time: 27s(total), 27s(copy); streamer: mysql-bin.000013:88943747; State: migrating; ETA: 2s
Copy: 1245400/1305600 95.4%; Applied: 0; Backlog: 0/1000; Time: 28s(total), 28s(copy); streamer: mysql-bin.000013:91218202; State: migrating; ETA: 1s
Copy: 1286600/1305600 98.5%; Applied: 0; Backlog: 0/1000; Time: 29s(total), 29s(copy); streamer: mysql-bin.000013:93203991; State: migrating; ETA: 0s
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 0/1000; Time: 29s(total), 29s(copy); streamer: mysql-bin.000013:94366846; State: migrating; ETA: due
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 1/1000; Time: 30s(total), 29s(copy); streamer: mysql-bin.000013:94369042; State: migrating; ETA: due
# Migrating `test`.`zjy`; Ghost table is `test`.`_zjy_gho`
# Migrating test2:3306; inspecting test2:3306; executing on test2
# Migration started at Sun Jun 17 14:37:37 +0800 2018
# chunk-size: 200; max-lag-millis: 1500ms; dml-batch-size: 10; max-load: Threads_running=10; critical-load: ; nice-ratio: 0.000000
# throttle-additional-flag-file: /tmp/gh-ost.throttle.test.zjy 
# panic-flag-file: /tmp/gh-ost.panic.test.zjy.flag
# Serving on unix socket: /tmp/gh-ost.test.zjy.sock
Copy: 1310720/1310720 100.0%; Applied: 0; Backlog: 0/1000; Time: 30s(total), 29s(copy); streamer: mysql-bin.000013:94371928; State: migrating; ETA: due
2018/06/17 14:38:08 binlogsyncer.go:107: [info] syncer is closing... 
2018/06/17 14:38:08 binlogstreamer.go:47: [error] close sync with err: sync is been closing... (這里的error不影響使用,重復(fù)關(guān)閉了sync,等作者修復(fù))
2018/06/17 14:38:08 binlogsyncer.go:122: [info] syncer is closed 
# Done

總結(jié):

gh-ost 放棄了觸發(fā)器,使用 binlog 來(lái)同步。gh-ost 作為一個(gè)偽裝的備庫(kù),可以從主庫(kù)/備庫(kù)上拉取 binlog,過(guò)濾之后重新應(yīng)用到主庫(kù)上去,相當(dāng)于主庫(kù)上的增量操作通過(guò) binlog 又應(yīng)用回主庫(kù)本身,不過(guò)是應(yīng)用在幽靈表上。

MySQL在線(xiàn)DDL gh-ost使用總結(jié)

gh-ost 首先連接到主庫(kù)上,根據(jù) alter 語(yǔ)句創(chuàng)建幽靈表,然后作為一個(gè)”備庫(kù)“連接到其中一個(gè)真正的備庫(kù)上,一邊在主庫(kù)上拷貝已有的數(shù)據(jù)到幽靈表,一邊從備庫(kù)上拉取增量數(shù)據(jù)的 binlog,然后不斷的把 binlog 應(yīng)用回主庫(kù)。圖中 cut-over 是最后一步,鎖住主庫(kù)的源表,等待 binlog 應(yīng)用完畢,然后替換 gh-ost 表為源表。gh-ost 在執(zhí)行中,會(huì)在原本的 binlog event 里面增加以下 hint 和心跳包,用來(lái)控制整個(gè)流程的進(jìn)度,檢測(cè)狀態(tài)等。這種架構(gòu)帶來(lái)諸多好處,例如:

  • 整個(gè)流程異步執(zhí)行,對(duì)于源表的增量數(shù)據(jù)操作沒(méi)有額外的開(kāi)銷(xiāo),高峰期變更業(yè)務(wù)對(duì)性能影響小。
  • 降低寫(xiě)壓力,觸發(fā)器操作都在一個(gè)事務(wù)內(nèi),gh-ost 應(yīng)用 binlog 是另外一個(gè)連接在做。
  • 可停止,binlog 有位點(diǎn)記錄,如果變更過(guò)程發(fā)現(xiàn)主庫(kù)性能受影響,可以立刻停止拉binlog,停止應(yīng)用 binlog,穩(wěn)定之后繼續(xù)應(yīng)用。
  • 可測(cè)試,gh-ost 提供了測(cè)試功能,可以連接到一個(gè)備庫(kù)上直接做 Online DDL,在備庫(kù)上觀察變更結(jié)果是否正確,再對(duì)主庫(kù)操作,心里更有底。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美一区二区三区视频在线 | 免费观看一区二区三区 | 四虎新网站 | 99久久婷婷国产综合精品电影 | 日韩电影免费在线观看中文字幕 | 不卡视频一二三区 | 狠狠ri| 精品美女久久久 | 夜夜av| 国产亚洲一区二区三区 | а√天堂中文在线资源8 | 国产美女一区二区三区 | 国产精品第52页 | 曰韩av | 亚洲欧美日韩在线 | 亚洲 欧美 自拍偷拍 | 亚洲一本 | 久久综合久久久 | 91一区二区 | 国产色| 亚洲电影一区二区三区 | 日日麻批免费视频40分钟 | 欧美日韩中文 | 在线亚洲精品 | 欧美激情在线播放 | 龙珠z普通话国语版在线观看 | 亚洲精品国产成人 | 欧美视频在线观看 | 欧美成人黄色 | 成人午夜精品一区二区三区 | 视频一区二区三区中文字幕 | 精品久久99 | 狠狠久| 亚洲毛片在线 | 日韩专区视频 | 毛片免费的 | www.免费av| 亚洲一区二区三区免费观看 | 亚洲精品综合中文字幕 | 色狠狠一区二区三区香蕉 | 欧美一级二级三级 |