TB級(jí)mysql數(shù)據(jù)之xtrabackup壓縮備份遷移方案
開(kāi)始遷移數(shù)據(jù)了,因?yàn)闅v史遺留的問(wèn)題,又因?yàn)椴块T的數(shù)據(jù)統(tǒng)一性,多個(gè)不同的庫(kù)表都在一個(gè)節(jié)點(diǎn)上,然后后面好多個(gè)從…..
首先看下我們數(shù)據(jù)庫(kù)的大小… , 再加上一個(gè)binlog日志,會(huì)更大的…. 當(dāng)然對(duì)于咱們數(shù)據(jù)遷移來(lái)說(shuō),只需要把數(shù)據(jù)就可以了. 既然是遷移,
那么大家一定想做到無(wú)縫的遷移… 首先在mysql master做備份,我們?cè)谏虾C(jī)房搭建一個(gè)從服務(wù)節(jié)點(diǎn),把文件pull下來(lái),然后跟master做同步… 當(dāng)數(shù)據(jù)校驗(yàn)ok后,修改所有配置的ip地址。
老規(guī)矩,最近爬蟲太狠,經(jīng)常爬了我的頁(yè)面,然后作者換成他們自己的….. http://xiaorui.cc/?p=1755
3.4T,可以說(shuō)是我見(jiàn)過(guò)的最大的數(shù)據(jù)量了,我們公司本身就是大數(shù)據(jù)相關(guān)的,但大數(shù)據(jù)基本是在hbase和Elasticsearch、solr中。 可能有人問(wèn)了,為毛分庫(kù)分表,因?yàn)橐郧暗膽?yīng)用實(shí)在太多,不可能把每個(gè)select的邏輯都做分庫(kù)分表的邏輯查詢 …. 其實(shí)最好的方法是中間件來(lái)承擔(dān)語(yǔ)句的時(shí)間range切分,但是這同樣需要開(kāi)發(fā)的成本,一些開(kāi)源的mysql proxy,也是無(wú)法做到這樣語(yǔ)句切分,是需要一定程度的二次開(kāi)發(fā)的…. 話說(shuō)開(kāi)源的各種mysql 中間件,貌似還沒(méi)有給力到完美支持語(yǔ)句分庫(kù)分表…..
廢話不多說(shuō),這是我們數(shù)據(jù)庫(kù)的大小….
- [root@bj-buzz-db01 ssd]# du -sh mysql/
- .4T mysql/
mysql的備份肯定不會(huì)用mysqldump這種鎖表的渣渣,必須用Percona的備份工具.
XtraBackup 有兩個(gè)工具:xtrabackup 和 innobackupex:
xtrabackup 本身只能備份 InnoDB 和 XtraDB ,不能備份 MyISAM;
innobackupex 本身是 Hot Backup 腳本修改而來(lái),同時(shí)可以備份 MyISAM 和 InnoDB,但是備份 MyISAM 需要加讀鎖。
官網(wǎng):http://www.percona.com/software/percona-xtrabackup
文檔:http://www.percona.com/doc/percona-xtrabackup/2.2/index.html
因?yàn)榭紤]到文件是在太大,必須要做好壓縮,不然 不管scp和rsync都是很痛苦的一件事情。
首先用的是gzip進(jìn)行壓縮,但是發(fā)現(xiàn)速度有些慢,linux 管道的速度沒(méi)啥好質(zhì)疑的…. 那瓶頸應(yīng)該是處在gzip壓縮上…
innobackupex –defaults-file=/etc/mysql/my.cnf –stream=tar /data/7_15 | gzip > /data/7_15.tar.gz
查了下percona關(guān)于壓縮的話題,官方是推薦用上面的方法的。 但是問(wèn)題我剛才也說(shuō)明白了,gzip只是個(gè)單進(jìn)程的應(yīng)用,如何跑滿cpu… 其實(shí)xtrabackup本身也有壓縮的功能參數(shù).
# 壓縮
–compress # 開(kāi)啟壓縮,目前只支持quicklz算法 壓縮級(jí)別在0-9.1快速壓縮,9最佳壓縮,0不壓縮。默認(rèn)為1.
–compress-threads=5 # 并發(fā)壓縮線程,默認(rèn)為1
–compress-chunk-size=64K # 每個(gè)壓縮線程使用的buffer,默認(rèn)64K
percona也是有加密的功能….
# 加密
–encrypt=AES256 # 開(kāi)啟加密,目前支持的算法有AES128, AES192 和 AES256
–encrypt-key=3c0efcea569021b49245e47b5d6a0e28 # 32位密鑰,不過(guò)不推薦這么使用,最好將密鑰存放在文件中,用encrypt-key-file參數(shù)引用
–encrypt-threads=5 # 加密線程數(shù),默認(rèn)為1
使用tar模式
innobackupex –defaults-file=/etc/mysql/my.cnf –stream=tar –compress ./ > 7_15
使用xbstream
innobackupex –defaults-file=/etc/mysql/my.cnf –stream=xbstream /tmp >/backup/bak.xbstream
用了壓縮,也用了多線程,速度貌似不是很給力…
緊接著,在xtrabackup help里面看到了 –parallel這個(gè)參數(shù),他是用來(lái)控制并發(fā)的… 但是經(jīng)過(guò)我的測(cè)試,效果一點(diǎn)都不明顯,說(shuō)白了就是沒(méi)啥效果…. 當(dāng)然也有可能我的場(chǎng)景不對(duì)付? 或者是使用方法不對(duì)?
默認(rèn)情況下 xtrabackup 備份時(shí)只會(huì)開(kāi)啟一個(gè)進(jìn)程進(jìn)行數(shù)據(jù)文件的備份,若配置參數(shù) –parallel=N 可以讓 xtrabackup 開(kāi)啟 N 個(gè)子進(jìn)程對(duì)多個(gè)數(shù)據(jù)文件進(jìn)行并發(fā)備份,這樣可以加快備份的速度。當(dāng)然服務(wù)器的 IO 處理能力以及對(duì)服務(wù)器的影響也是要考慮的,所以另一個(gè)參數(shù) –throttle=IOS 會(huì)與它同時(shí)使用,這個(gè)參數(shù)用來(lái)限制備份過(guò)程中每秒讀寫的 IO 次數(shù),對(duì)服務(wù)器的 IO 是一個(gè)保護(hù)。
innobackupex –defaults-file=/etc/mysql/my.cnf –parallel=10 –stream=tar /data/7_15 | gzip > /data/7_15.tar.gz
這是最后的方式…. 既然是gzip速度慢,那么就用pigz這種gzip的多線程加強(qiáng)版來(lái)改造他,加強(qiáng)他…. 個(gè)人覺(jué)得 –parallel –compress-threads 看起來(lái)不錯(cuò),但是性能提升不是很滿意..
innobackupex –defaults-file=/etc/mysql/my.cnf –stream=tar /data/7_15 | pigz -9 -p 32 > /data/7_15.tar.gz
這是pigz的安裝方式.
- wget http://zlib.net/pigz/pigz-2.3.3.tar.gz
- tar zxvf pigz-2.3.3.tar.gz
- cd pigz-2.3.3/
- make
遇到一個(gè)make問(wèn)題。
[root@bj-buzz-db01 pigz-2.3.3]# make
cc -O3 -Wall -Wextra -c -o pigz.o pigz.c
pigz.c:365:73: error: zlib.h: No such file or directory
pigz.c:372:4: error: #error Need zlib version 1.2.3 or later
pigz.c: In function ‘put_header’:
pigz.c:1000: error: ‘Z_DEFAULT_COMPRESSION’ undeclared (first use in this function)
pigz.c:1000: error: (Each undeclared identifier is reported only once
pigz.c:1000: error: for each function it appears in.)
pigz.c: At top level:
pigz.c:1459: error: expected ‘)’ before ‘*’ token
pigz.c: In function ‘compress_thread’:
pigz.c:1495: error: ‘z_stream’ undeclared (first use in this function)
pigz.c:1495: error: expected ‘;’ before ‘strm’
pigz.c:1502: error: ‘strm’ undeclared (first use in this function)
pigz.c:1502: error: ‘Z_NULL’ undeclared (first use in this function)
pigz.c:1505: warning: implicit declaration of function ‘deflateInit2’
pigz.c:1505: error: ‘Z_DEFLATED’ undeclared (first use in this function)
pigz.c:1505: error: ‘Z_DEFAULT_STRATEGY’ undeclared (first use in this function)
pigz.c:1506: error: ‘Z_MEM_ERROR’ undeclared (first use in this function)
pigz.c:1508: error: ‘Z_OK’ undeclared (first use in this function)
pigz.c:1530: warning: implicit declaration of function ‘deflateReset’
pigz.c:1531: warning: implicit declaration of function ‘deflateParams’
pigz.c:1546: warning: implicit declaration of function ‘deflateSetDictionary’
pigz.c:1595: warning: implicit declaration of function ‘deflate_engine’
pigz.c:1595: error: ‘Z_NO_FLUSH’ undeclared (first use in this function)
pigz.c:1620: error: ‘Z_SYNC_FLUSH’ undeclared (first use in this function)
pigz.c:1624: error: ‘Z_FINISH’ undeclared (first use in this function)
pigz.c:1687: warning: implicit declaration of function ‘adler32’
pigz.c:1687: warning: implicit declaration of function ‘crc32’
pigz.c:1706: warning: implicit declaration of function ‘deflateEnd’
pigz.c: In function ‘write_thread’:
pigz.c:1737: error: ‘Z_NULL’ undeclared (first use in this function)
pigz.c: In function ‘single_compress’:
pigz.c:2039: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
pigz.c:2039: error: ‘strm’ undeclared (first use in this function)
pigz.c:2062: error: ‘z_stream’ undeclared (first use in this function)
pigz.c:2063: error: ‘Z_NULL’ undeclared (first use in this function)
pigz.c:2066: error: ‘Z_DEFLATED’ undeclared (first use in this function)
pigz.c:2066: error: ‘Z_DEFAULT_STRATEGY’ undeclared (first use in this function)
pigz.c:2067: error: ‘Z_MEM_ERROR’ undeclared (first use in this function)
pigz.c:2069: error: ‘Z_OK’ undeclared (first use in this function)
pigz.c:2173: warning: implicit declaration of function ‘deflate’
pigz.c:2173: error: ‘Z_NO_FLUSH’ undeclared (first use in this function)
pigz.c:2198: error: ‘Z_SYNC_FLUSH’ undeclared (first use in this function)
pigz.c:2202: error: ‘Z_FINISH’ undeclared (first use in this function)
pigz.c: In function ‘infchk’:
pigz.c:3044: error: ‘z_stream’ undeclared (first use in this function)
pigz.c:3044: error: expected ‘;’ before ‘strm’
pigz.c:3054: error: ‘Z_NULL’ undeclared (first use in this function)
pigz.c:3055: error: ‘strm’ undeclared (first use in this function)
pigz.c:3058: warning: implicit declaration of function ‘inflateBackInit’
pigz.c:3059: error: ‘Z_MEM_ERROR’ undeclared (first use in this function)
pigz.c:3061: error: ‘Z_OK’ undeclared (first use in this function)
pigz.c:3067: warning: implicit declaration of function ‘inflateBack’
pigz.c:3068: warning: implicit declaration of function ‘inflateBackEnd’
pigz.c:3069: error: ‘Z_DATA_ERROR’ undeclared (first use in this function)
pigz.c:3072: error: ‘Z_BUF_ERROR’ undeclared (first use in this function)
pigz.c:3074: error: ‘Z_STREAM_END’ undeclared (first use in this function)
pigz.c: In function ‘defaults’:
pigz.c:3828: error: ‘Z_DEFAULT_COMPRESSION’ undeclared (first use in this function)
make: *** [pigz.o] Error 1
提示錯(cuò)誤,是因?yàn)闆](méi)有zlib開(kāi)發(fā)包原因….
yum -y install zlib-devel
然后我們?cè)俅斡胮igz進(jìn)行壓縮,下面是cpu的使用率。
Tasks: 446 total, 1 running, 444 sleeping, 0 stopped, 1 zombie
Cpu0 : 69.2%us, 2.6%sy, 0.0%ni, 23.5%id, 4.6%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 45.4%us, 1.0%sy, 0.0%ni, 53.6%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 51.0%us, 5.0%sy, 0.0%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 0.0%si, 0.0%st
Cpu18 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu19 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu20 : 99.7%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st
Cpu21 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu22 : 98.7%us, 0.0%sy, 0.0%ni, 0.3%id, 0.0%wa, 0.0%hi, 1.0%si, 0.0%st
Cpu23 :100.0%us, 0.0%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
我們?cè)賮?lái)看看磁盤,毫無(wú)壓力…. 我這邊就不貼圖了,剛要想起把iostat -x 1的結(jié)果貼出來(lái),發(fā)現(xiàn)備份已經(jīng)完成。。。 在下午xtrabackup備份壓縮的過(guò)程中,其他的訪問(wèn)沒(méi)感受到有性能的影響…..

我們?cè)賮?lái)看看大小…. 965G…… 壓縮的效果還是比較的滿意…. 消耗的時(shí)間是,7:38 – 1:18 ,將近6個(gè)半小時(shí)。。。。。

那么我又在讀取mysql數(shù)據(jù)文件的時(shí)候,增加了并發(fā),使用了–parallel參數(shù)讓 xtrabackup 開(kāi)啟 N 個(gè)子進(jìn)程對(duì)多個(gè)數(shù)據(jù)文件進(jìn)行并發(fā)備份。
innobackupex –defaults-file=/etc/mysql/my.cnf –user=root –password=xxx –parallel=5 –stream=tar /data/7_15_p | pigz -8 -p 15 > /data/7_15_p.tar.gz
首先看下他的io情況. 我們會(huì)發(fā)現(xiàn),數(shù)據(jù)盤無(wú)壓力,但是備份的存儲(chǔ)盤io已經(jīng)是報(bào)了…. 這說(shuō)明啥? 說(shuō)明xtrabackup在增加了并發(fā)讀取文件的時(shí)候,不僅考慮mysql數(shù)據(jù)盤的io,也要考慮目標(biāo)存儲(chǔ)盤的io是否能抗住。。。。
- Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm %util
- sda 0.00 35.00 0.00 7.00 0.00 328.00 46.86 0.01 1.43 1.43 1.00
- sdb 0.00 31.00 460.00 15.00 172944.00 368.00 364.87 1.51 3.16 2.08 98.90
- dm-0 0.00 0.00 0.00 41.00 0.00 328.00 8.00 0.23 5.51 0.24 1.00
- dm-1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
- dm-2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
- memdiska 0.00 0.00 63.00 37.00 2672.00 296.00 29.68 0.01 0.07 0.06 0.60