mydumper原理介紹


 
 
mydumper介紹
 
MySQL自身的mysqldump工具支持單線程工作,依次一個個導出多個表,沒有一個並行的機,這就使得它無法迅速的備份數據。
 
mydumper作為一個實用工具,能夠良好支持多線程工作,可以並行的多線程的從表中讀入數據並同時寫到不同的文件里,這使得它在處理速度方面快於傳統的mysqldump。其特征之一是在處理過程中需要對列表加以鎖定,因此如果我們需要在工作時段執行備份工作,那么會引起DML阻塞。但一般現在的MySQL都有主從,備份也大部分在從上進行,所以鎖的問題可以不用考慮。這樣,mydumper能更好的完成備份任務。
 
mydumper特性
 
①多線程備份
②因為是多線程邏輯備份,備份后會生成多個備份文件
③備份時對MyISAM表施加FTWRL(FLUSH TABLES WITH READ LOCK),會阻塞DML語句
④保證備份數據的一致性
⑤支持文件壓縮
⑥支持導出binlog
⑦支持多線程恢復
⑧支持以守護進程模式工作,定時快照和連續二進制日志
⑨支持將備份文件切塊
 
mydumper參數詳解
 
$ mydumper --help
Usage:
  mydumper [OPTION...] multi-threaded MySQL dumping

Help Options:
  -?, --help                  Show help options

Application Options:
  -B, --database              要備份的數據庫,不指定則備份所有庫
  -T, --tables-list           需要備份的表,名字用逗號隔開
  -o, --outputdir             備份文件輸出的目錄
  -s, --statement-size        生成的insert語句的字節數,默認1000000
  -r, --rows                  Try to split tables into chunks of this many rows. This option turns off --chunk-filesize
  -F, --chunk-filesize        Split tables into chunks of this output file size. This value is in MB
  -c, --compress              Compress output files壓縮輸出文件
  -e, --build-empty-files     如果表數據是空,還是產生一個空文件(默認無數據則只有表結構文件)
  -x, --regex                 Regular expression for 'db.table' matching 使用正則表達式匹配'db.table'
  -i, --ignore-engines        Comma delimited list of storage engines to ignore忽略的存儲引擎,用逗號分割
  -m, --no-schemas            Do not dump table schemas with the data不備份表結構,只備份數據
  -d, --no-data               Do not dump table data備份表結構,不備份數據
  -G, --triggers              Dump triggers備份觸發器
  -E, --events                Dump events
  -R, --routines              Dump stored procedures and functions備份存儲過程和函數
  -k, --no-locks              不使用臨時共享只讀鎖,使用這個選項會造成數據不一致
  --less-locking              Minimize locking time on InnoDB tables.減少對InnoDB表的鎖施加時間
  -l, --long-query-guard      設定阻塞備份的長查詢超時時間,單位是秒,默認是60秒(超時后默認mydumper將會退出)
  -K, --kill-long-queries     Kill long running queries (instead of aborting)殺掉長查詢 (不退出)
  -D, --daemon                Enable daemon mode啟用守護進程模式,守護進程模式以某個間隔不間斷對數據庫進行備
  -I, --snapshot-interval     dump快照間隔時間,默認60s,需要在daemon模式下
  -L, --logfile               使用的日志文件名(mydumper所產生的日志), 默認使用標准輸出
  --tz-utc                    SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones, defaults to on use --skip-tz-utc to disable.
  --skip-tz-utc               
  --use-savepoints            使用savepoints來減少采集metadata所造成的鎖時間,需要 SUPER 權限
  --success-on-1146           Not increment error count and Warning instead of Critical in case of table doesn't exist
  --lock-all-tables           Use LOCK TABLE for all, instead of FTWRL
  -U, --updated-since         Use Update_time to dump only tables updated in the last U days
  --trx-consistency-only      Transactional consistency only
  -h, --host                  連接的主機名
  -u, --user                  用來備份的用戶名
  -p, --password              用戶密碼
  -P, --port                  連接端口
  -S, --socket                使用socket通信時的socket文件
  -t, --threads               開啟的備份線程數,默認是4
  -C, --compress-protocol     壓縮與mysql通信的數據
  -V, --version               顯示版本號
  -v, --verbose               輸出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默認為2
 
mydumper主要流程概括
 
1、主線程 FLUSH TABLES WITH READ LOCK, 施加全局只讀鎖,以阻止DML語句寫入,保證數據的一致性
2、讀取當前時間點的二進制日志文件名和日志寫入的位置並記錄在metadata文件中,以供即使點恢復使用
3、N個(線程數可以指定,默認是4)dump線程 START TRANSACTION WITH CONSISTENT SNAPSHOT; 開啟讀一致的事務
4、dump non-InnoDB tables, 首先導出非事務引擎的表
5、主線程 UNLOCK TABLES 非 事務引擎備份完后,釋放全局只讀鎖
6、dump InnoDB tables, 基於 事務導出InnoDB表
7、事務結束
 
  mydumper的less locking模式:
 
     mydumper使用--less-locking可以減少鎖等待時間,此時mydumper的執行機制大致為
 
     1、主線程 FLUSH TABLES WITH READ LOCK (全局鎖)
     2、Dump線程 START TRANSACTION WITH CONSISTENT SNAPSHOT;
     3、LL Dump線程 LOCK TABLES non-InnoDB (線程內部鎖)
     4、主線程UNLOCK TABLES
     5、LL Dump線程 dump non-InnoDB tables
     6、LL DUmp線程 UNLOCK non-InnoDB
     7、Dump線程 dump InnoDB tables
 
 
mydumper備份流程圖
 

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM