Skip to content

Instantly share code, notes, and snippets.

@maxmilian
Last active July 13, 2020 06:48
Show Gist options
  • Select an option

  • Save maxmilian/f229c6f4736b66c78be512a14a7e2802 to your computer and use it in GitHub Desktop.

Select an option

Save maxmilian/f229c6f4736b66c78be512a14a7e2802 to your computer and use it in GitHub Desktop.

參考 Mongodb Back Up and Restore Deployments

Mongo 本地端啟動

# list services
brew services list

# start
brew services start mongodb

# stop
brew services stop mongodb

# restart
brew services restart mongodb

Mongo 設定

# vim /usr/local/etc/mongod.conf

storage:
  dbPath: /usr/local/var/mongodb
setParameter:
  failIndexKeyTooLong: false

Mongo備份還原

# backup
mongodump -h host --db databaseName --out path

# restore all collection in  database
mongorestore -h host --db databaseName --drop path

# restore a collection in database
mongorestore -h host --db databaseName --collection collectionName --drop bsonPath

NOTE: 若需要drop collection,才需要加入 drop 參數

Mongodb resync

刪除 mongo data 目錄,重啟後會自動重新同步資料

cd /var/lib/mongodb
rm -fr *
service mongo start

強制設定為 Master

方法一 修改 priority

cfg = rs.conf()
cfg.members[0].priority = 0.5
cfg.members[1].priority = 0.5
cfg.members[2].priority = 1
rs.reconfig(cfg)

Operation

db.currentOp()

db.currentOp({"secs_running": {$gte: 10}}) 

db.killOp(opid)

關機

db.adminCommand( { shutdown: 1 } )

use admin
db.shutdownServer();
``

# Mongo狀態
https://docs.mongodb.com/manual/reference/replica-states/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment