国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(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是一種二進制格式,通常無法使用文本編輯器直接打開查看其內(nèi)容,對人類的可讀性較差,但它的優(yōu)點是保存的文件體積要小;使用這組命令導(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;這里需要注意一點,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ā)表
主站蜘蛛池模板: 清苑县| 手游| 鹤壁市| 洪洞县| 奉化市| 思茅市| 镇平县| 陵川县| 博野县| 三门县| 千阳县| 泊头市| 德兴市| 尉氏县| 新津县| 喀喇| 集贤县| 宝应县| 长乐市| 林甸县| 北川| 铁力市| 南投市| 阆中市| 武冈市| 洮南市| 泾川县| 太和县| 洪雅县| 太白县| 临潭县| 兰西县| 宿迁市| 黄浦区| 休宁县| 格尔木市| 民丰县| 舞钢市| 内丘县| 罗江县| 沧州市|