iT邦幫忙

0

[MongoDB] 工作筆記:利用LVM Snapshot 進行Restore - Replica Set

簡介

目標是將原本的mongodb搬移到新的server。
本篇文章內容僅包含複製完原本mongodb檔案後,mongod的初始設定的步驟。

包含以下幾個步驟:

  1. 啟動standalone mongod
  2. 刪除local data
  3. 重啟mongo成為single-node
  4. Replica set初始化

啟動standalone mongod

將設定檔(mongod.conf)中的replication和security註解。
並更新ip、port、各種檔案路徑(如果需要的話)。

sed -i "/bindIp/c\   bindIp: `hostname -i`, 127.0.0.1" conf/mongod.conf
sed -i '/replication/s/^/#&/' conf/mongod.conf
sed -i '/oplogSizeMB/s/^/#&/' conf/mongod.conf
sed -i '/replSetName/s/^/#&/' conf/mongod.conf
sed -i '/security/s/^/#&/' conf/mongod.conf
sed -i '/authorization/s/^/#&/' conf/mongod.conf
sed -i '/keyFile/s/^/#&/' conf/mongod.conf

mongod.conf

net:
   port: 
   bindIp: `hostname -i`,127.0.0.1
#replication:
#   oplogSizeMB: 
#   replSetName: 
#security:
#   authorization: enabled
#   keyFile:

啟動mongod

mongod --config your_config_path

刪除local data

連線到mongo shell,刪除local的內容。

use local
db.dropDatabase()
use admin
db.shutdownServer()

重啟mongo成為single-node

將設定檔中,前面註解掉的replication和security都拿掉。

sed -i '/replication/s/^#//' conf/mongod.conf
sed -i '/oplogSizeMB/s/^#//' conf/mongod.conf
sed -i '/replSetName/s/^#//' conf/mongod.conf
sed -i '/security/s/^#//' conf/mongod.conf
sed -i '/authorization/s/^#//' conf/mongod.conf
sed -i '/keyFile/s/^#//' conf/mongod.conf

mongod.conf

replication:
   oplogSizeMB: 
   replSetName: 
security:
   authorization: enabled
   keyFile:

啟動mongod

mongod --config your_config_path

Replica set初始化

Primary node

rs.initiate()
rs.status()
exit

Second node

rs.add( { host: "## HOSTNAME ##:## PORT ##" } )
rs.status()
exit

大功告成!


參考

https://docs.mongodb.com/manual/tutorial/restore-replica-set-from-backup/


尚未有邦友留言

立即登入留言