安裝
下載安裝包:
wget https://launchpad.net/mydumper/0.9/0.9.1/+download/mydumper-0.9.1.tar.gz
安裝依賴包:
yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake
## mydumper是C語句開發,編譯依賴gcc和gcc-c++,如果編譯出現C語言相關報錯可yum install gcc gcc-c++
## mydumper-0.9.1依賴5.7的mysql-devel,所以必須安裝5.7的mysql-devel
tar zxvf mydumper-0.9.1.tar.gz
cd mydumper-0.9.1
cmake .
make
make install
安裝完成後生成兩個二進制文件mydumper和myloader位於/usr/local/bin目錄下
查看是否正常:
mydumper --help
***********************另一個版本********************
運行環境:centos 7.5 + mysql8.0
1.介紹:
1.1 基本信息簡介:
mydumper是第三方支持mysql的備份恢復工具,支持並行備份恢復速度快。
https://github.com/maxbube/mydumper
Parallelism (hence, speed) and performance (avoids expensive character set conversion routines, efficient code overall)
Easier to manage output (separate files for tables, dump metadata, etc, easy to view/parse data)
Consistency - maintains snapshot across all threads, provides accurate master and slave log positions, etc
Manageability - supports PCRE for specifying database and tables inclusions and exclusions
1.2發佈歷史:
1.3 編譯版本:
# mydumper --version
mydumper 0.9.1, built against MySQL 5.1.73
# mydumper --version
mydumper 0.9.3, built against MySQL 5.7.19-17
## mydumper --version
mydumper 0.9.5, built against MySQL 5.7.21-21
2.安裝:
# wget https://github.com/maxbube/mydumper/archive/v0.9.5.tar.gz
# tar -xzvf v0.9.5.tar.gz -C /usr/local/
# mv /usr/local/mydumper-0.9.5/ /usr/local/mydumper
切花到程序目錄下查看README文件視操作系統而定:
# cat README.md
安裝依賴的軟件包:
# yum -y install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel gcc cmake make
#cmake .
#make
編譯安裝完成之後在/usr/local/mydumper目錄下生成mydumper和myloader二進制文件。
編譯報錯的話查看報錯信息:
#cat /usr/local/mydumper/CMakeFiles/CMakeOutput.log
3.使用RPM的安裝方式:
#
yum install https://github.com/maxbube/mydumper/releases/download/v0.9.5/mydumper-0.9.5-1.el7.x86_64.rpm
4.mydumper的正則表達式:
mydumper使用--regex來使用正則表達式功能。
不備份mysql和test庫:
mydumper --regex '^(?!(mysql\.|test\.))'
僅備份mysql和test庫:
mydumper --regex '^(mysql\.|test\.)'
不備份以test開頭的數據庫:
mydumper --regex '^(?!(test))'
Of course, regex functionality can be used to describe pretty much any list of tables.
********************
流程
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的執行機制大致爲
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參數詳解
Application Options:
-B, --database Database to dump #指定數據庫
-T, --tables-list Comma delimited table list to dump (does not exclude regex option) #指定表
-o, --outputdir Directory to output files to #指定備份路徑
-s, --statement-size Attempted size of INSERT statement in bytes, default 1000000 #生成的insert語句的字節數,默認1000000
-r, --rows Try to split tables into chunks of this many rows. This option turns off --chunk-filesize #指定行切割表,使用時關閉--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 Build dump files even if no data available from table #如果表數據是空,還是產生一個空文件(默認無數據則只有表結構文件)
-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 Do not execute the temporary shared read lock. WARNING: This will cause inconsistent backups #不使用臨時共享只讀鎖,使用這個選項會造成數據不一致
--less-locking Minimize locking time on InnoDB tables. #減少對InnoDB表的鎖施加時間
-l, --long-query-guard Set long query timer in seconds, default 60 #設定阻塞備份的長查詢超時時間,單位是秒,默認是60秒(超時後默認mydumper將會退出)
-K, --kill-long-queries Kill long running queries (instead of aborting) #殺掉長查詢 (不退出)
-D, --daemon Enable daemon mode #啓用守護進程模式,守護進程模式以某個間隔不間斷對數據庫進行備
-I, --snapshot-interval Interval between each dump snapshot (in minutes), requires --daemon, default 60 #dump快照間隔時間,默認60s,需要在daemon模式下
-L, --logfile Log file name to use, by default stdout is used #使用的日誌文件名(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 Use savepoints to reduce metadata locking issues, needs SUPER privilege #使用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 The host to connect to #主機名
-u, --user Username with privileges to run the dump #用戶
-p, --password User password #密碼
-P, --port TCP/IP port to connect to #端口
-S, --socket UNIX domain socket file to use for connection #使用socket通信時的socket文件
-t, --threads Number of threads to use, default 4 #開啓的備份線程數,默認是4
-C, --compress-protocol Use compression on the MySQL connection #壓縮與mysql通信的數據
-V, --version Show the program version and exit #顯示版本號
-v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2 #輸出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默認爲2
PS:mydumper沒有—where參數,部分場景不適用
myloader參數詳解
Application Options:
-d, --directory Directory of the dump to import #備份文件路徑
-q, --queries-per-transaction Number of queries per transaction, default 1000 #每個事務的query數量, 默認1000
-o, --overwrite-tables Drop tables if they already exist #如果表存在則先刪除,使用該參數,需要備份時候要備份表結構,不然還原會找不到表
-B, --database An alternative database to restore into #指定需要還原的數據庫
-s, --source-db Database to restore #還原的數據庫
-e, --enable-binlog Enable binary logging of the restore data #啓用二進制日誌恢復數據
-h, --host The host to connect to #主機名
-u, --user Username with privileges to run the dump #賬號
-p, --password User password #密碼
-P, --port TCP/IP port to connect to #端口
-S, --socket UNIX domain socket file to use for connection #使用socket通信時的socket文件
-t, --threads Number of threads to use, default 4 #開啓的恢復線程數,默認是4
-C, --compress-protocol Use compression on the MySQL connection #壓縮與mysql通信的數據
-V, --version Show the program version and exit#顯示版本號
-v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2 #輸出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默認爲2
樣例
備份多線程單表,必須啓用-r指定行數拆分單表爲多個文件,會生成多個文件
mydumper -u xxx -p xxx -h xxx -P 3307 -B lgjdb -T yyd_users,liangtab -t 4 -r 3000000 -o /root/lgj -c -C -l 200 &
還原數據
myloader -u xxx -p xxx -h xxx -P 3307 -B lgjdb -d /root/lgj
-- 結論:
使用myloader恢復某一個表的時候,備份的文件夾裏面只有這個表的sql就行了,如果有其他表的sql,其他表的備份會被恢復到指定的表裏面。
感覺myloader恢復的時候,不能指定某一個文件進行恢復,是指定某一個文件夾進行恢復。所以文件夾裏面只能有要恢復的表 。
另外,恢復的文件夾裏面必須要有metadata這個文件,否則沒法恢復,會提示這個文件夾不是dumper文件夾。
如果在主庫上進行恢復,需要同步在備庫,則需要加上-e參數,產生binlog日誌 。
**********************************會鎖表***********************
mydumper是針對mysql數據庫備份的一個輕量級第三方的開源工具,備份方式爲邏輯備份。它支持多線程,備份速度遠高於原生態的mysqldump以及衆多優異特性。因此該工具是DBA們的不二選擇。本文主要描述該工具的使用方法並給出示例。
有關mydumper安裝參考:mydumper安裝及安裝故障彙總
1、mydumper的特點
a、多線程邏輯備份,生產的多個備份文件
b、與mysqldump相同,備份時對 MyISAM 表施加FTWRL (FLUSH TABLES WITH READ LOCK), 會阻塞DML 語句
c、保證備份數據的一致性
d、支持文件壓縮,支持導出binlog,支持多線程恢復,支持將備份文件切塊
e、支持以守護進程模式工作,定時快照和連續二進制日誌
2、mydumper語法
mydumper -u [USER] -p [PASSWORD] -h [HOST] -P [PORT] -t [THREADS] -b -c -B [DB] -o [directory]
參數說明
-B, --database 需要備份的庫
-T, --tables-list 需要備份的表,用逗號分隔
-o, --outputdir 輸出文件的目錄
-s, --statement-size 生成插入語句的字節數, 默認 1000000
-r, --rows 分裂成很多行塊表
-c, --compress 壓縮輸出文件
-e, --build-empty-files 即使表沒有數據,還是產生一個空文件
-x, --regex 正則表達式: 'db.table'
-i, --ignore-engines 忽略的存儲引擎,用逗號分隔
-m, --no-schemas 不導出表結構
-k, --no-locks 不執行共享讀鎖 警告:這將導致不一致的備份
-l, --long-query-guard 設置長查詢時間,默認60秒
--kill-long-queries kill掉長時間執行的查詢
-b, --binlogs 導出binlog
-D, --daemon 啓用守護進程模式
-I, --snapshot-interval dump快照間隔時間,默認60s,需要在daemon模式下
-L, --logfile 日誌文件
-h, --host The host to connect to
-u, --user Username with privileges to run the dump
-p, --password User password
-P, --port TCP/IP port to connect to
-S, --socket UNIX domain socket file to use for connection
-t, --threads 使用的線程數,默認4
-C, --compress-protocol 在mysql連接上使用壓縮協議
-V, --version Show the program version and exit
-v, --verbose 更多輸出, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2
3、mydumper的工作過程
a、連接目標數據庫;
b、通過show processlist來判斷是否有長查詢,根據參數long-query-guard和kill-long-queries決定退出或殺掉長查詢;
c、鎖定myisam表,flush tables with read lock;針對innodb table開啓事務,start transaction;
d、創建worker子線程,缺省爲4個;
e、確定候選表,根據類別分別插入innodb_table,non_innodb_table以及table_schemas鏈表(表結構);
f、將候選表通過g_async_queue_push加入任務隊列(隊列最後元素是thread shutdown),由worker子線程從隊列中讀取表信息並執行數據導出
g、執行unlock tables,處理完myisam表後立即解鎖,以減少鎖定時間;
h、等待worker退出;
4、備份文件相關信息
a、所有的備份文件在一個目錄中,未指定時爲當前目錄, 且自動生成備份日期時間文件夾,如export-20150703-145806
b、目錄中包含一個 metadata 文件,該文件記錄了備份時間點的二進制日誌文件名,日誌的寫入位置
c、如果是在從庫進行備份,還會記錄備份時同步至主庫的二進制日誌文件及寫入位置
d、每個表有兩個備份文件:database.table-schema.sql 表結構文件,database.table.sql 表數據文件
e、如果對錶文件分片,將生成多個備份數據文件,可以指定行數或指定大小分片
5、常用備份示例:
###備份單個庫
# mydumper -u leshami -p pwd -B sakila -o /tmp/bak
###備份所有數據庫,全庫備份期間除了information_schema與performance_schema之外的庫都會被備份
# mydumper -u leshami -p pwd -o /tmp/bak
###備份單表
# mydumper -u leshami -p pwd -B sakila -T actor -o /tmp/bak
###備份多表
# mydumper -u leshami -p pwd -B sakila -T actor,city -o /tmp/bak
###當前目錄自動生成備份日期時間文件夾,不指定-o參數及值時,如文件夾爲:export-20150703-145806
mydumper -u leshami -p pwd -B sakila -T actor
###不帶表結構備份表
# mydumper -u leshami -p pwd -B sakila -T actor -m
###壓縮備份及連接使用壓縮協議(非本地備份時)
# mydumper -u leshami -p pwd -B sakila -o /tmp/bak -c -C
###備份特定表
# mydumper -u leshami -p pwd -B sakila --regex=actor* -o /tmp/bak
###過濾特定庫,如本來不備份mysql及test庫
# mydumper -u leshami -p pwd -B sakila --regex '^(?!(mysql|test))' -o /tmp/bak
###基於空表產生表結構文件
# mydumper -u leshami -p pwd -B sakila -T actor -e -o /tmp/bak
##設置長查詢的上限,如果存在比這個還長的查詢則退出mydumper,也可以設置殺掉這個長查詢
#mydumper -u leshami -p pwd -B sakila --long-query-guard 200 --kill-long-queries
###備份時輸出詳細日誌
# mydumper -u leshami -p pwd -B sakila -T actor -v 3 -o /tmp/bak
###導出binlog,使用-b參數,會自動在導出目錄生成binlog_snapshot文件夾及binlog
# mydumper -u leshami -p pwd -P 3306 -b -o /tmp/bak