本文主要給大家介紹MySQL數(shù)據(jù)庫備份常用工具之MySQL Data Dumper簡析,希望可以給大家補充和更新些知識,如有其它問題需要了解的可以持續(xù)在創(chuàng)新互聯(lián)行業(yè)資訊里面關(guān)注我的更新文章的。
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站制作、網(wǎng)站設(shè)計與策劃設(shè)計,棲霞網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:棲霞等地區(qū)。棲霞做網(wǎng)站價格咨詢:18982081108
說到MySQL數(shù)據(jù)庫的備份, MySQL Data Dumper(項目)也是常用的工具,其有兩個可執(zhí)行程序: mydumper,負責(zé)導(dǎo)出數(shù)據(jù); myloader, 負責(zé)導(dǎo)入數(shù)據(jù). mydumper相對于mysqldump,多了些特性,在下面分析選項的過程中能體會到.
由于是第三方工具,先來看下安裝,及可能遇到的問題.
a. mydumper需要依賴一些開發(fā)庫,使用yum安裝即可.
root@db01: ~# yum install glib* zlib* pcre* -y
b.添加連接MySQL需要的動態(tài)鏈接庫.
root@db01: ~# cat /etc/ld.so.conf.d/mysql.conf
/opt/mysql/lib
root@db01: ~#ldconfig
root@db01: ~#ldconfig --print-cache | grep 'mysql'
libmysqlclient.so.18 (libc6,x86-64)=> /opt/mysql/lib/libmysqlclient.so.18
root@db01: ~# ls -l /opt/mysql/lib/libmysqlclient.so.18
lrwxrwxrwx 1 rootroot 26 Aug 25 14:21 /opt/mysql/lib/libmysqlclient.so.18 ->libmysqlclient_r.so.18.1.0
c.編譯安裝.
root@db01: ~# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mydumper
root@db01: ~# make install
添加可執(zhí)行命令的路徑到環(huán)境變量PATH中.
mysql@db01: ~$grep 'PATH' .bash_profile
PATH=/usr/local/mydumper/bin:/opt/mysql/bin/:$PATH:$HOME/bin
export PATH
d.在命令行敲入mydumper回車,看下面的返回信息,安裝是正常的.
mysql@db01: ~$mydumper
**(mydumper:723): CRITICAL **: Error connecting to database: Access denied foruser 'root'@'localhost' (using password:NO)
mysql@db01: ~$myloader
**(myloader:5288): CRITICAL **: a directory needs to be specified, see --help
若出現(xiàn)如下報錯,可能是步驟b有問題.
mysql@db01: ~$mydumper
mydumper: errorwhile loading shared libraries: libmysqlclient.so.18: cannot open shared objectfile: No such file or directory
下面是演示用到的數(shù)據(jù)庫數(shù)據(jù)表的信息:
(root@localhost)[(none)]> SELECT table_schema, table_name, engine FROM information_schema.tables WHERE (engine = 'InnoDB' OR engine = 'MyISAM') AND table_schema NOT IN('mysql', 'performance_schema' ,'information_schema');
+--------------+------------+--------+
| table_schema |table_name | engine |
+--------------+------------+--------+
| product | pr1 | MyISAM |
| product | pr2 | MyISAM |
| product | pr3 | InnoDB |
| stage | st1 | InnoDB |
| stage | st2 | InnoDB |
| test | tb1 | InnoDB |
| test | tb2 |InnoDB |
+--------------+------------+--------+
7 rows in set(0.01 sec)
mydumper的選項也不少,按照分析mysqldump一樣,將其分成若干組,看看重點選項的含義.
Connection Options組
該組選項指明了如何連接數(shù)據(jù)庫.
-h, --host The host to connect to
-u, --user Username with privileges to run the dump
-p,--password User password
-P, --port TCP/IPport to connect to
-S, --socket domainsocket file to use for connection
Debug Options組
改組指明了日志放在哪里,以及日志的級別.
-L,--logfile Log file name to use, by defaultstdout is used
-v,--verbose Verbosity of output, 0 =silent, 1 = errors, 2 = warnings, 3 = info, default 2
Filtering Options組
改組指明了備份哪些數(shù)據(jù)庫對象,以及對備份文件做什么附加處理(壓縮,分割等).
-B,--database Database to dump
-T,--tables-list Comma delimitedtable list to dump (does not exclude regex option)
-o,--outputdir Directory to outputfiles to
-s,--statement-size Attempted size ofINSERT statement in bytes, default 1000000
-r, --rows Try to split tables into chunks ofthis many rows. This option turns off --chunk-filesize
-F,--chunk-filesize Split tables into chunks of this output filesize. This value is in MB
-c,--compress Compress output files
-e,--build-empty-files Build dump files even if no data availablefrom table
-x, --regex Regular expression for 'db.table'matching
-m,--no-schemas Do not dump tableschemas with the data
-d,--no-data Do not dump tabledata
-G,--triggers Dump triggers
-E, --events Dump events
-R, --routines Dump stored procedures and functions
Transactional Options組
該組主要涉及到備份時如何加鎖,下面使用該命令行進行測試mydumper --regex '^(?!(mysql))'--threads=1 [Option],同時結(jié)合general log,看mydumper是如何工作的.
1.先看不加選項時,是什么情況.
Master線程,獲取GLOBAL READ LOCK,開啟一致性讀事物,得到二進制日志的坐標(biāo).
1587512Query FLUSH TABLES WITH READ LOCK
1587512Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1587512Query SHOW MASTER STATUS
Dump線程,設(shè)置事物隔離級別為REPEATABLE READ,開啟一致性讀事物進行非事物數(shù)據(jù)表的備份.
1587513Query SET SESSION TRANSACTION ISOLATIONLEVEL REPEATABLE READ
1587513Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1587513Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr1`
1587513Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr2`
Master線程,待Dump線程備份完非事物數(shù)據(jù)表后,釋放鎖.
1587512Query UNLOCK TABLES /* FTWRL */
Dump線程,繼續(xù)其它事物數(shù)據(jù)表的備份.
2. -k,--no-locks Do not execute the temporaryshared read lock. WARNING: This willcause inconsistent backups
使用該選項時, mydumper會有如下類似提示:
**(mydumper:4095): WARNING **: Executing in no-locks mode, snapshot will notbeconsistent
其主要作用過程如下:
Master線程,開啟一致性讀事物,得到二進制日志的坐標(biāo).
1586766Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1586766Query SHOW MASTER STATUS
Dump線程,設(shè)置事物隔離級別為REPEATABLE READ,開啟一致性讀事物進行數(shù)據(jù)表的備份.
1586767Query SET SESSION TRANSACTION ISOLATIONLEVEL REPEATABLE READ
1586767Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
該過程由于未執(zhí)行FLUSH TABLES WITH READ LOCK,得到的二進制日志坐標(biāo)可能不準確; (多個)線程開啟一致性讀事物時,數(shù)據(jù)表可能會有變動,這兩點會造成備份數(shù)據(jù)不一致.
3.--less-locking Minimize locking time onInnoDB tables.
Master線程,獲取GLOBAL READ LOCK,開啟一致性讀事物,得到二進制日志的坐標(biāo).
1588054Query FLUSH TABLES WITH READ LOCK
1588054Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1588054Query SHOW MASTER STATUS
Dump2線程,設(shè)置事物隔離級別為REPEATABLE READ,開啟一致性讀事物.
1588056Query SET SESSION TRANSACTION ISOLATIONLEVEL REPEATABLE READ
1588056Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
Dump1線程,鎖定非事物數(shù)據(jù)表.
1588055 Query LOCK TABLES `product`.`pr1` READ LOCAL,`product`.`pr2` READ LOCAL
Master線程,釋放鎖.
1588054Query UNLOCK TABLES /* FTWRL */
Dump1線程,備份非事物數(shù)據(jù)表.
1588055Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr1`
1588055Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr2`
Dump1線程,備份完成后,釋放鎖.
1588055Query UNLOCK TABLES /* Non Innodb */
Dump2線程,繼續(xù)其它事物數(shù)據(jù)表的備份.
4.--use-savepoints Use savepoints toreduce metadata locking issues, needs SUPER privilege
該選項含義是,盡快釋放元數(shù)據(jù)鎖,其它過程和1相同.
1601611 Query SAVEPOINT mydumper
1601611 Query ROLLBACK TO SAVEPOINT mydumper
5.--lock-all-tables Use LOCK TABLE forall, instead of FTWRL
Master線程,獲取有那些數(shù)據(jù)庫和數(shù)據(jù)庫表,然后把需要備份的數(shù)據(jù)表加鎖,開啟一致性讀事物,再后得到二進制日志的坐標(biāo).
1586979Query SELECT TABLE_SCHEMA, TABLE_NAMEFROM information_schema.TABLES WHERE TABLE_TYPE ='BASE TABLE' AND TABLE_SCHEMANOT IN ('information_schema', 'performance_schema', 'data_dictionary') AND NOT(TABLE_SCHEMA = 'mysql' AND (TABLE_NAME = 'slow_log' OR TABLE_NAME ='general_log'))
1586979Query LOCK TABLE `product`.`pr1` READ,`product`.`pr2` READ, `product`.`pr3` READ, `stage`.`st1` READ, `stage`.`st2`READ, `test`.`tb1` READ, `test`.`tb2` READ
1586979Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1586979Query SHOW MASTER STATUS
Dump線程,設(shè)置事物隔離級別為REPEATABLE READ,開啟一致性讀事物進行非事物數(shù)據(jù)表的備份.
1586980Query SET SESSION TRANSACTION ISOLATIONLEVEL REPEATABLE READ
1586980Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1586980Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr1`
1586980Query SELECT /*!40001 SQL_NO_CACHE */ *FROM `product`.`pr2`
Master線程,待Dump線程備份完非事物數(shù)據(jù)表后,釋放鎖.
1586979Query UNLOCK TABLES /* FTWRL */
Dump線程,繼續(xù)其它事物數(shù)據(jù)表的備份.
此種加鎖方式,若數(shù)據(jù)庫數(shù)據(jù)表比較多時,加鎖效率不高.
6.--trx-consistency-only Transactionalconsistency only
使用該選項時, mydumper會有如下類似提示:
**(mydumper:2573): WARNING **: Using trx_consistency_only, binlog coordinateswill not be accurate if you are writing to non transactional tables
Master線程,獲取GLOBAL READ LOCK,開啟一致性讀事物,得到二進制日志的坐標(biāo).
1588315Query FLUSH TABLES WITH READ LOCK
1588315Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
1588315Query SHOW MASTER STATUS
Dump線程,設(shè)置事物隔離級別為REPEATABLE READ,開啟一致性讀事物.
1588316Query SET SESSION TRANSACTION ISOLATIONLEVEL REPEATABLE READ
1588316Query START TRANSACTION /*!40108 WITHCONSISTENT SNAPSHOT */
Master線程,釋放鎖.
1588315Query UNLOCK TABLES /* trx-only */
Dump線程,備份數(shù)據(jù)表.
此方式,從加鎖到釋放鎖,時間最短,效率最高.
經(jīng)上面的分析,可得到加鎖過程影響大小順序如下:
--lock-all-tables>不加該組選項 = --use-savepoints >--less-locking > --trx-consistency-only > --no-locks
Performance Options組
該組指定了線程數(shù)量,和如何處理長查詢.
-t,--threads Number of threads touse, default 4
-l,--long-query-guard Set long query timerin seconds, default 60
-K,--kill-long-queries Kill long runningqueries (instead of aborting)
參數(shù)了解完了,看兩個實際工作中例子.
1.備份除數(shù)據(jù)庫mysql之外的其它數(shù)據(jù)庫.
mysql@db01:~/dbbackup$ mydumper --outputdir=20170826 --compress --build-empty-files--regex '^(?!(mysql))' --triggers --events --routines --logfile=error.txt--use-savepoints --trx-consistency-only --threads=4 --verbose=3
2.備份全部數(shù)據(jù)庫.
mysql@db01:~/dbbackup$ mydumper --outputdir=20170826 --compress --build-empty-files--triggers --events --routines --long-query-guard=60 --kill-long-queries--logfile=error.txt --use-savepoints --trx-consistency-only --threads=4--verbose=3
經(jīng)過選項分析和實踐過程,總結(jié)下mydumper的特點:
1.多線程備份,可指定線程數(shù)量,其也是速度優(yōu)于mysqldump的關(guān)鍵.
2.對于備份數(shù)據(jù)一致性方面考慮較多,主要體現(xiàn)在非事物數(shù)據(jù)表的備份上.
3.分析選項時,沒有指定字符集的,查看general log后,發(fā)現(xiàn)是這樣處理的/*!40101 SET NAMES binary*/,即省去了轉(zhuǎn)換字符集的開銷.
4.提供了如何應(yīng)對長查詢的選項.
myloader并沒有太多需要說明的,看下選項解釋,實踐下即可.
mydumper在備份時,效率有了很大提升,但其終究還是將數(shù)據(jù)轉(zhuǎn)化為SQL語句,即常說的邏輯備份.
看了以上關(guān)于MySQL數(shù)據(jù)庫備份常用工具之MySQL Data Dumper簡析,希望能給大家在實際運用中帶來一定的幫助。本文由于篇幅有限,難免會有不足和需要補充的地方,如有需要更加專業(yè)的解答,可在官網(wǎng)聯(lián)系我們的24小時售前售后,隨時幫您解答問題的。
本文題目:MySQL數(shù)據(jù)庫備份常用工具之MySQLDataDumper簡析
URL鏈接:http://aaarwkj.com/article40/jejsho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、做網(wǎng)站、電子商務(wù)、網(wǎng)站制作、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)