MongoDB副本集遷移實(shí)操案例詳解
背景介紹
客戶要將生產(chǎn)環(huán)境上一套副本集架構(gòu)的 MongoDB 進(jìn)行遷移,數(shù)據(jù)量 240GB 左右。經(jīng)過測試,全量備份耗時(shí) 3.5 小時(shí),恢復(fù)耗時(shí) 4.5小時(shí)。
為了減少割接時(shí)間,采取全量 + 增量 Oplog 的遷移方式。提前一天進(jìn)行全備,割接當(dāng)天只需備份增量的 Oplog 恢復(fù)即可,可大幅減少割接窗口。
實(shí)操過程
查看 Oplog 信息
檢查并評估生產(chǎn)環(huán)境 Oplog 的產(chǎn)生信息,以防全量和增量備份期間產(chǎn)生的 Oplog 被覆蓋掉。
mongo> db.getReplicationInfo()
{
"logSizeMB" : 20480,
"usedMB" : 20374.38,
"timeDiff" : 7074665,
"timeDiffHours" : 1965.18,
"tFirst" : "Fri Feb 24 2023 18:36:32 GMT+0800 (CST)",
"tLast" : "Wed May 17 2023 15:47:37 GMT+0800 (CST)",
"now" : "Wed May 17 2023 15:47:43 GMT+0800 (CST)"
}可以看出在 1965.18h 的運(yùn)行中,產(chǎn)生了 10374.38MB 大小的 Oplog。
全量備份
全量備份并拷貝備份期間產(chǎn)生的 Oplog 用來增量還原。
#!/bin/bash user=admin password=123 host=127.0.0.1 port=27017 outputdir=/data/mongobak_`date +%F` authenticationdatabase=admin start_time=`date +%s` mongodump -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplog --gzip -o $outputdir stop_time=`date +%s` duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
全量恢復(fù)
利用全備進(jìn)行數(shù)據(jù)恢復(fù)。
#!/bin/bash start_time=`date +%s` user=admin password=123 host=127.0.0.1 port=27017 authenticationdatabase=admin mongorestore -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplogReplay --gzip /data/mongobak_2023-07-17 stop_time=`date +%s` duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
提取增量備份開始的時(shí)間點(diǎn)
全備備份出來的 Oplog,可以利用 bsondump 工具將 bson 轉(zhuǎn)換為 json 格式,查看備份時(shí)間產(chǎn)生的最后的 Oplog 的時(shí)間戳,根據(jù)此時(shí)間戳來進(jìn)行增量的 Oplog 備份。
shell> cd /data/ mongobak_2023-07-17 shell> mv oplog.bson oplog.bson.gz shell> gzip -d oplog.bson.gz shell> bsondump --pretty oplog.bson > op.json
查看 op.json 文件,找出增量備份開始的時(shí)間點(diǎn)。
"ts": {
"$timestamp": {
"t": 1686669429,
"i": 4
}
},增量備份
備份 Oplog(時(shí)間戳大于上一次全備結(jié)束時(shí)的時(shí)間)。
#!/bin/bash
user=admin
password=123
host=127.0.0.1
port=27017
outputdir=/tmp/oplog_`date +%F`
authenticationdatabase=admin
start_time=`date +%s`
mongodump -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase -d local -c oplog.rs -q '{"ts":{"$gt": {"$timestamp":{"t":1686669429, "i":4}}}}' -o $outputdir
stop_time=`date +%s`
duration=$((stop_time-start_time))
echo "Spend times: $duration seconds"增量恢復(fù)
#!/bin/bash user=admin password=123 host=127.0.0.1 port=27017 authenticationdatabase=admin start_time=`date +%s` mongorestore -u$user --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --oplogReplay /data/oplog_2023-07-17 stop_time=`date +%s` duration=$((stop_time-start_time)) echo "Spend times: $duration seconds"
增量遷移后業(yè)務(wù)文檔數(shù)量對比
分別在源端和目標(biāo)端運(yùn)行腳本,檢查遷移完成后業(yè)務(wù)數(shù)據(jù)庫下文檔數(shù)量是否一致。
#!/bin/bash
user=admin
password=123
host=127.0.0.1
port=27017
authenticationdatabase=admin
mpid=`pidof mongod`
tooldir=`dirname $(ls -l /proc/$mpid/exe | awk '{print $11}')`
database=$(echo "show dbs" | $tooldir/mongo -uadmin --host $host --port $port -p$password --authenticationDatabase $authenticationdatabase --quiet |awk '{print $1}'| sed -E '/^admin$|^config$|^local$/d')
for db in $database
do
collections=$(echo -e "use $db\n show collections" | $tooldir/mongo -u $user --host $host --port $port -p $password $authenticationdatabase --quiet | sed '/switched to db/d')
for table in $collections
do
count=$(echo -e "use $db\n db.$table.count()" | $tooldir/mongo -u $user --host $host --port $port -p $password --authenticationDatabase $authenticationdatabase --quiet | sed '/switched to db/d')
echo "$db.$table have $count documents"
done
done源端運(yùn)行結(jié)果:

目標(biāo)端運(yùn)行結(jié)果:

注意事項(xiàng)
- 使用 secondary 備份時(shí),在割接停止業(yè)務(wù)后,增量備份前,首先檢查下從庫與主庫的延時(shí),確保主從沒有延時(shí),防止備份出的數(shù)據(jù)和主庫不一致。
- 如果全備時(shí)指定了 gzip,在提取時(shí)間戳?xí)r要重命名
oplog.bson為oplog.bson.gz,然后解壓,再利用bsondump工具解析 bson 文件,否則會(huì)報(bào)錯(cuò)。
以上就是MongoDB 副本集遷移實(shí)操案例的詳細(xì)內(nèi)容,更多關(guān)于MongoDB 副本集遷移實(shí)操案例的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MongoDB數(shù)據(jù)庫授權(quán)認(rèn)證的實(shí)現(xiàn)
本文主要介紹了MongoDB數(shù)據(jù)庫授權(quán)認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
MongoDB使用自帶的命令行工具進(jìn)行備份和恢復(fù)的教程
這篇文章主要介紹了MongoDB使用自帶的命令行工具進(jìn)行備份和恢復(fù)的教程,我們只需要在命令行界面中用簡單的命令操作mongorestore和mongodump工具就可以實(shí)現(xiàn),需要的朋友可以參考下2016-06-06
Windows10安裝MongoDB4.0詳細(xì)步驟及啟動(dòng)配置教程
這篇文章主要介紹了Windows10安裝MongoDB4.0詳細(xì)步驟及啟動(dòng)配置教程 ,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01
mongodb實(shí)現(xiàn)同庫聯(lián)表查詢方法示例
在關(guān)系型數(shù)據(jù)庫中,通過連接運(yùn)算符可以實(shí)現(xiàn)多個(gè)表聯(lián)合查詢。而非關(guān)系型數(shù)據(jù)庫的特點(diǎn)是表之間屬于弱關(guān)聯(lián),下面這篇文章主要給大家介紹了關(guān)于mongodb實(shí)現(xiàn)同庫聯(lián)表查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
MongoDB開啟權(quán)限認(rèn)證的方法步驟詳解
MongoDB已經(jīng)使用很長一段時(shí)間了,基于MongoDB的數(shù)據(jù)存儲(chǔ)也一直沒有使用到權(quán)限訪問(MongoDB默認(rèn)設(shè)置為無權(quán)限訪問限制),最近深入學(xué)習(xí)了下,所以下面這篇文章主要給大家介紹了關(guān)于MongoDB開啟權(quán)限認(rèn)證的相關(guān)資料,需要的朋友可以參考下。2018-02-02

