欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MongoDB副本集遷移實(shí)操案例詳解

 更新時(shí)間:2023年11月29日 09:52:34   作者:愛(ài)可生開(kāi)源社區(qū)  
文中詳細(xì)闡述了通過(guò)全量?+?增量?Oplog?的遷移方式,完成一套副本集?MongoDB?遷移的全過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

背景介紹

客戶(hù)要將生產(chǎn)環(huán)境上一套副本集架構(gòu)的 MongoDB 進(jìn)行遷移,數(shù)據(jù)量 240GB 左右。經(jīng)過(guò)測(cè)試,全量備份耗時(shí) 3.5 小時(shí),恢復(fù)耗時(shí) 4.5小時(shí)。

為了減少割接時(shí)間,采取全量 + 增量 Oplog 的遷移方式。提前一天進(jìn)行全備,割接當(dāng)天只需備份增量的 Oplog 恢復(fù)即可,可大幅減少割接窗口。

實(shí)操過(guò)程

查看 Oplog 信息

檢查并評(píng)估生產(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 用來(lái)增量還原。

#!/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"

提取增量備份開(kāi)始的時(shí)間點(diǎn)

全備備份出來(lái)的 Oplog,可以利用 bsondump 工具將 bson 轉(zhuǎn)換為 json 格式,查看備份時(shí)間產(chǎn)生的最后的 Oplog 的時(shí)間戳,根據(jù)此時(shí)間戳來(lái)進(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 文件,找出增量備份開(kāi)始的時(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ù)量對(duì)比

分別在源端和目標(biāo)端運(yùn)行腳本,檢查遷移完成后業(yè)務(wù)數(shù)據(jù)庫(kù)下文檔數(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ù)后,增量備份前,首先檢查下從庫(kù)與主庫(kù)的延時(shí),確保主從沒(méi)有延時(shí),防止備份出的數(shù)據(jù)和主庫(kù)不一致。
  • 如果全備時(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í)操案例的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MongoDB 中Limit與Skip的使用方法詳解

    MongoDB 中Limit與Skip的使用方法詳解

    這篇文章主要介紹了MongoDB 中Limit與Skip的使用方法詳解的相關(guān)資料,這里對(duì)這兩種方法進(jìn)行了詳細(xì)介紹并附示例代碼,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下
    2017-07-07
  • MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn)

    MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn)

    本文主要介紹了MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • mongodb實(shí)現(xiàn)同庫(kù)聯(lián)表查詢(xún)方法示例

    mongodb實(shí)現(xiàn)同庫(kù)聯(lián)表查詢(xún)方法示例

    在關(guān)系型數(shù)據(jù)庫(kù)中,通過(guò)連接運(yùn)算符可以實(shí)現(xiàn)多個(gè)表聯(lián)合查詢(xún)。而非關(guān)系型數(shù)據(jù)庫(kù)的特點(diǎn)是表之間屬于弱關(guān)聯(lián),下面這篇文章主要給大家介紹了關(guān)于mongodb實(shí)現(xiàn)同庫(kù)聯(lián)表查詢(xún)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • Ubuntu中安裝MongoDB及執(zhí)行一些簡(jiǎn)單操作筆記

    Ubuntu中安裝MongoDB及執(zhí)行一些簡(jiǎn)單操作筆記

    這篇文章主要介紹了Ubuntu中安裝MongoDB及執(zhí)行一些簡(jiǎn)單操作筆記,本文同時(shí)給出了查看已有數(shù)據(jù)庫(kù)、刪除數(shù)據(jù)庫(kù)、創(chuàng)建數(shù)據(jù)庫(kù)等操作命令實(shí)例,需要的朋友可以參考下
    2014-09-09
  • MongoDB Remove函數(shù)的3個(gè)常見(jiàn)用法

    MongoDB Remove函數(shù)的3個(gè)常見(jiàn)用法

    這篇文章主要介紹了MongoDB Remove函數(shù)的3個(gè)常見(jiàn)用法,需要的朋友可以參考下
    2014-05-05
  • mongodb在建立一個(gè)T級(jí)別的數(shù)據(jù)庫(kù)時(shí),進(jìn)程掛掉的解決方法

    mongodb在建立一個(gè)T級(jí)別的數(shù)據(jù)庫(kù)時(shí),進(jìn)程掛掉的解決方法

    這篇文章主要介紹了mongodb在建立一個(gè)T級(jí)別的數(shù)據(jù)庫(kù)時(shí),進(jìn)程掛掉,需要的朋友可以參考下
    2017-03-03
  • ubuntu mongodb安裝在哪個(gè)文件夾路徑詳解

    ubuntu mongodb安裝在哪個(gè)文件夾路徑詳解

    這篇文章主要為大家介紹了ubuntu mongodb安裝在哪個(gè)文件夾的安裝路徑詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集

    mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集

    這篇文章主要給大家介紹了關(guān)于mongodb3.4集群搭建實(shí)戰(zhàn)之高可用的分片+副本集的相關(guān)資料,文中通過(guò)示例代碼將實(shí)現(xiàn)的步驟一步步的介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 一文搞懂Scrapy與MongoDB交互過(guò)程

    一文搞懂Scrapy與MongoDB交互過(guò)程

    這篇文章主要介紹了Scrapy與MongoDB交互過(guò)程,文末給大家介紹了類(lèi)方法@classmethod的相關(guān)知識(shí),需要的朋友可以參考下
    2022-07-07
  • MongoDB中的push操作詳解(將文檔插入到數(shù)組)

    MongoDB中的push操作詳解(將文檔插入到數(shù)組)

    $push操作符添加指定的值到數(shù)組中,下面這篇文章主要給大家介紹了關(guān)于MongoDB中push操作(將文檔插入到數(shù)組)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11

最新評(píng)論