麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

mongodb如何創(chuàng)建備份,以及如何恢復(fù)數(shù)據(jù)?

2024-09-07 00:22:35
字體:
供稿:網(wǎng)友
  了解數(shù)據(jù)的備份與恢復(fù)是很有必要的,因此我們很難保證數(shù)據(jù)不會出現(xiàn)意外,做好備份與恢復(fù)能夠讓數(shù)據(jù)損失降到最低。那么在mongodb如何創(chuàng)建備份,以及如何恢復(fù)數(shù)據(jù)呢?下面我們一起來學(xué)習(xí)一下。
 
  為什么要備份?
 
  備份的目的是對數(shù)據(jù)做冗余的一種方式,它能夠讓我們在某種情況下保證最少數(shù)據(jù)的丟失;之前我們對mongodb做副本集也是對數(shù)據(jù)做冗余,但是這種在副本集上做數(shù)據(jù)冗余僅僅是針對系統(tǒng)故障或服務(wù)異常等一些非人為的故障發(fā)生時,保證數(shù)據(jù)服務(wù)的可用性;它不能夠避免人為的誤操作;為了使得數(shù)據(jù)的安全,將數(shù)據(jù)損失降低到最小,我們必須對數(shù)據(jù)庫周期性的做備份;
 
 
  mongodb邏輯備份工具
 
  在mongodb中使用邏輯備份的工具有兩組,第一組是mongodump/mongorestore,使用mongodump/mongorestore這組工具來邏輯的備份數(shù)據(jù),它備份出來的數(shù)據(jù)是BSON格式,BSON是一種二進(jìn)制格式,通常無法使用文本編輯器直接打開查看其內(nèi)容,對人類的可讀性較差,但它的優(yōu)點(diǎn)是保存的文件體積要小;使用這組命令導(dǎo)出的數(shù)據(jù),在恢復(fù)是依賴mongodb版本,不同版本導(dǎo)出的BSON格式略有不同,所以恢復(fù)時,可能存在版本不同而導(dǎo)致恢復(fù)數(shù)據(jù)失敗的情況;另外一組是mongoexport/mongoimport,這組工具導(dǎo)出的數(shù)據(jù)是json格式的數(shù)據(jù),通常我們可以使用文本編輯器打開直接查看,對人類的可讀性較好,但體積相對BSON格式的數(shù)據(jù)要大,恢復(fù)時不依賴版本;所以跨版本備份要先查看下對應(yīng)版本的兼容性,如果兼容使用mongodump/mongorestore,不兼容的話建議使用mongoexport/mongoimport;這里需要注意一點(diǎn),JSON格式雖然可讀性很好,也很通用,但是它只是保留了數(shù)據(jù)部分,而沒有保留索引,賬戶等基礎(chǔ)信息,在使用是應(yīng)該注意;
 
  使用mongodump備份數(shù)據(jù)
 
  插入數(shù)據(jù)
 
> use testdb
switched to db testdb
> for(i=1;i<=1000;i++) db.test.insert({id:i,name:"test"+i,age:(i%120),classes:(i%25)})
WriteResult({ "nInserted" : 1 })
> show tables
test
> db.test.findOne()
{
 "_id" : ObjectId("5fb130da012870b3c8e3c4ad"),
 "id" : 1,
 "name" : "test1",
 "age" : 1,
 "classes" : 1
}
> db.test.count()
1000
>
  備份所有數(shù)據(jù)庫
 
[root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -o ./node12_mongodb_full_backup
2020-11-15T21:47:45.439+0800 writing admin.system.users to node12_mongodb_full_backup/admin/system.users.bson
2020-11-15T21:47:45.442+0800 done dumping admin.system.users (4 documents)
2020-11-15T21:47:45.443+0800 writing admin.system.version to node12_mongodb_full_backup/admin/system.version.bson
2020-11-15T21:47:45.447+0800 done dumping admin.system.version (2 documents)
2020-11-15T21:47:45.448+0800 writing testdb.test to node12_mongodb_full_backup/testdb/test.bson
2020-11-15T21:47:45.454+0800 done dumping testdb.test (1000 documents)
[root@node11 ~]# ls
node12_mongodb_full_backup
[root@node11 ~]# ll node12_mongodb_full_backup/
total 0
drwxr-xr-x 2 root root 128 Nov 15 21:47 admin
drwxr-xr-x 2 root root 49 Nov 15 21:47 testdb
[root@node11 ~]# tree node12_mongodb_full_backup/
node12_mongodb_full_backup/
├── admin
│ ├── system.users.bson
│ ├── system.users.metadata.json
│ ├── system.version.bson
│ └── system.version.metadata.json
└── testdb
 ├── test.bson
 └── test.metadata.json
 
2 directories, 6 files
[root@node11 ~]#
  提示:-u用于指定用戶,-p指定對應(yīng)用戶的密碼,-h指定數(shù)據(jù)庫地址,--authenticationDatabase 指定驗證用戶和密碼對應(yīng)的數(shù)據(jù)庫 -o指定要存放備份文件的目錄名稱;
 
  只備份單個testdb數(shù)據(jù)庫
 
[root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -o ./node12_testdb
2020-11-15T21:53:36.523+0800 writing testdb.test to node12_testdb/testdb/test.bson
2020-11-15T21:53:36.526+0800 done dumping testdb.test (1000 documents)
[root@node11 ~]# tree ./node12_testdb
./node12_testdb
└── testdb
 ├── test.bson
 └── test.metadata.json
 
1 directory, 2 files
[root@node11 ~]#
  提示:-d用戶指定要備份的數(shù)據(jù)庫名稱;
 
  只備份testdb下的test集合
 
[root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -c test -o ./node12_testdb_test-collection
2020-11-15T21:55:48.217+0800 writing testdb.test to node12_testdb_test-collection/testdb/test.bson
2020-11-15T21:55:48.219+0800 done dumping testdb.test (1000 documents)
[root@node11 ~]# tree ./node12_testdb_test-collection
./node12_testdb_test-collection
└── testdb
 ├── test.bson
 └── test.metadata.json
 
1 directory, 2 files
[root@node11 ~]#
  提示:-c用于指定要備份的集合(collection)名稱;
 
  壓縮備份testdb庫
 
[root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb --gzip -o ./node12_mongodb_testdb-gzip
2020-11-15T22:00:52.268+0800 writing testdb.test to node12_mongodb_testdb-gzip/testdb/test.bson.gz
2020-11-15T22:00:52.273+0800 done dumping testdb.test (1000 documents)
[root@node11 ~]# tree ./node12_mongodb_testdb-gzip
./node12_mongodb_testdb-gzip
└── testdb
 ├── test.bson.gz
 └── test.metadata.json.gz
 
1 directory, 2 files
[root@node11 ~]#
  提示:可以看到使用壓縮,只需要加上--gzip選項即可,備份出來的數(shù)據(jù)就是.gz后綴結(jié)尾的壓縮文件;
 
  壓縮備份testdb庫下的test集合
 
[root@node11 ~]# mongodump -utom -p123456 -h 192.168.0.52:27017 --authenticationDatabase admin -d testdb -c test --gzip -o ./node12_mongodb_testdb-test-gzip
2020-11-15T22:01:31.492+0800 writing testdb.test to node12_mongodb_testdb-test-gzip/testdb/test.bson.gz
2020-11-15T22:01:31.500+0800 done dumping testdb.test (1000 documents)
[root@node11 ~]# tree ./node12_mongodb_testdb-test-gzip
./node12_mongodb_testdb-test-gzip
└── testdb
 ├── test.bson.gz
 └── test.metadata.json.gz
 
1 directory, 2 files
[root@node11 ~]#
  使用mongorestore恢復(fù)數(shù)據(jù)
 
  在node12上刪除testdb
 
> db
testdb
> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
 
  驗證:登錄192.168.0.52:27017查看對應(yīng)testdb數(shù)據(jù)庫是否恢復(fù)?
 
[root@node11 ~]# mongo -utom -p123456 192.168.0.52:27017/admin
MongoDB shell version v4.4.1
connecting to: mongodb://192.168.0.52:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("af96cb64-a2a4-4d59-b60a-86ccbbe77e3e") }
MongoDB server version: 4.4.1
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
 https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
 https://community.mongodb.com
---
The server generated these startup warnings when booting:
 2020-11-15T20:42:23.774+08:00: ***** SERVER RESTARTED *****
 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
 2020-11-15T20:42:29.198+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
 Enable MongoDB's free cloud-based monitoring service, which will then receive and display
 metrics about your deployment (disk utilization, CPU, operation statistics, etc).
 
   恢復(fù)單個庫
 
  刪除testdb庫
 
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
testdb 0.000GB
> use testdb
switched to db testdb
> db.dropDatabase()
{ "dropped" : "testdb", "ok" : 1 }
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>

(編輯:武林網(wǎng))

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 偿还的影视高清在线观看 | 欧美一级电影网站 | 男女羞羞视频在线观看免费 | 在线亚洲播放 | 午夜视频在线观看免费视频 | 亚洲人成中文字幕在线观看 | 一级黄色影片在线观看 | 成人精品 | 久草在线新时代视觉 | 一本一本久久a久久精品综合小说 | 久章草影院| 青久草视频 | 中文字幕亚洲情99在线 | 久久综合九色 | 操碰 | 爱操av| 最污网站 | 欧美特黄一级高清免费的香蕉 | 久久久成人动漫 | 国产精品99一区二区 | 海外中文字幕在线观看 | 精品亚洲网站 | 国产一级αv片免费观看 | 九九热精品在线 | 国产精品99久久久久久久 | 国产a级久久 | 亚洲精品一区国产精品丝瓜 | 99精美视频 | 久草在线新时代视觉 | av中文在线观看 | 激情宗合| 可以看逼的视频 | 日日夜av | 国产精品av久久久久久网址 | 国产毛片aaa一区二区三区视频 | 久草在线新时代视觉 | 毛片大全 | 亚洲网站在线播放 | 亚洲精品一区中文字幕 | 性欧美极品xxxx欧美一区二区 | 久久久久久久一区二区三区 |