100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > MongoDB 数据库备份 与 恢复

MongoDB 数据库备份 与 恢复

时间:2022-06-28 16:50:54

相关推荐

MongoDB  数据库备份 与 恢复

目录

MongoDB 数据备份

MongoDB 数据恢复

本文只讲 MongoDB 数据库的备份与恢复,其余内容不清楚的可以参考《MongoDB》其它章节。

MongoDB 数据备份

MongoDB提供了数据备份和恢复的功能,分别是MongoDB下载目录 bin 目录下的mongodump.exe和mongorestore.exe文件 备份数据命令:mongodump-hdbhost-ddbname-odbdirectory

-h:MongDB所在的服务器地址,例如:127.0.0.1,当然也可以同时指定端口号:127.0.0.1:27017

-d:需要备份的数据库实例,例如:test

-o:备份的数据存放位置,例如:d:\data\dump,目录不存在时会自动新建,备份完成后,dump目录下会建立一个test目录,里面存放该数据库实例的备份数据。

不同的 mongoDB 版本,参数选项可能不一致,可以在 cmd 命令行中输入mongodump --help进行查看

C:\Users\Administrator.SC-07281232>mongodump --helpUsage:mongodump <options>Export the content of a running server into .bson files.Specify a database with -d and a collection with -c to only dump that database or collection.See /manual/reference/program/mongodump/ for more information.general options:/help print usage/versionprint the toolversion and exitverbosity options:/v, /verbose:<level> more detailed logoutput (include........

因为是直接调用 mongodump.exe 与 mongorestore.exe 程序进行操作,所以直接在 cmd 命令行中使用即可,不需要登录 MongoDB 数据库如下所示,mongoDB 中现在有 5 个数据库实例,其中 mydb1 数据库中有 100 条数据,现在以备份 mydb1 数据库为例。

> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb1 0.000GBmydb2 0.186GB> dbmydb1> db.c1.find().count()100> db.c1.find(){ "_id" : ObjectId("5b98b375c03ec6172729490d"), "name" : "zhangSan", "age" : 1 }{ "_id" : ObjectId("5b98b375c03ec6172729490e"), "name" : "zhangSan", "age" : 2 }{ "_id" : ObjectId("5b98b375c03ec6172729490f"), "name" : "zhangSan", "age" : 3 }{ "_id" : ObjectId("5b98b375c03ec61727294910"), "name" : "zhangSan", "age" : 4 }{ "_id" : ObjectId("5b98b375c03ec61727294911"), "name" : "zhangSan", "age" : 5 }{ "_id" : ObjectId("5b98b375c03ec61727294912"), "name" : "zhangSan", "age" : 6 }{ "_id" : ObjectId("5b98b375c03ec61727294913"), "name" : "zhangSan", "age" : 7 }{ "_id" : ObjectId("5b98b375c03ec61727294914"), "name" : "zhangSan", "age" : 8 }{ "_id" : ObjectId("5b98b375c03ec61727294915"), "name" : "zhangSan", "age" : 9 }{ "_id" : ObjectId("5b98b375c03ec61727294916"), "name" : "zhangSan", "age" : 10 }{ "_id" : ObjectId("5b98b375c03ec61727294917"), "name" : "zhangSan", "age" : 11 }{ "_id" : ObjectId("5b98b375c03ec61727294918"), "name" : "zhangSan", "age" : 12 }{ "_id" : ObjectId("5b98b375c03ec61727294919"), "name" : "zhangSan", "age" : 13 }{ "_id" : ObjectId("5b98b375c03ec6172729491a"), "name" : "zhangSan", "age" : 14 }{ "_id" : ObjectId("5b98b375c03ec6172729491b"), "name" : "zhangSan", "age" : 15 }{ "_id" : ObjectId("5b98b375c03ec6172729491c"), "name" : "zhangSan", "age" : 16 }{ "_id" : ObjectId("5b98b375c03ec6172729491d"), "name" : "zhangSan", "age" : 17 }{ "_id" : ObjectId("5b98b375c03ec6172729491e"), "name" : "zhangSan", "age" : 18 }{ "_id" : ObjectId("5b98b375c03ec6172729491f"), "name" : "zhangSan", "age" : 19 }{ "_id" : ObjectId("5b98b375c03ec61727294920"), "name" : "zhangSan", "age" : 20 }Type "it" for more>

如下所示 mongodump -h 127.0.0.1:27017 -d mydb1 -o E:/wmx/mongoDump 进行备份 mydb1 数据库

-h :指定 mongoDB 服务端 IP 地址 与 端口,端口不指定时,默认为 27017

-d:需要备份的 MongoDB 数据库实例

-o:备份的数据存放位置,目录不存在时,会自动新建,此目录下会再有一个与 数据库实例同名的子目录用来正式存放数据文件

注意备份、恢复 命令直接在 cmd 命令行中操作,不需要登录 MongoDB ,所以如果之前登录了,则 exit 退出。

{ "_id" : ObjectId("5b98b375c03ec6172729491f"), "name" : "zhangSan", "age" : 19 }{ "_id" : ObjectId("5b98b375c03ec61727294920"), "name" : "zhangSan", "age" : 20 }Type "it" for more> exitbyeC:\Users\Administrator.SC-07281232>mongodump -h 127.0.0.1:27017 -d mydb1 -o E:/wmx/mongoDump-09-12T14:40:57.719+0800 writing mydb1.c1 to-09-12T14:40:57.805+0800 done dumping mydb1.c1 (100 documents)C:\Users\Administrator.SC-07281232>

接着再次登录 MongoDB 数据库删除原来的 mydb1 数据库,为后来的数据库恢复做准备,如下所示 mydb1 数据库删除成功

> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb1 0.000GBmydb2 0.186GB> use mydb1switched to db mydb1> db.dropDatabase(){ "dropped" : "mydb1", "ok" : 1 }> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb2 0.186GB>

MongoDB 数据恢复

恢复数据命令:mongorestore-hdbhost-ddbname/dirdbdirectory

-h:MongoDB所在服务器 IP 地址

-d:需要恢复的数据库实例,例如:test,这个名称也可以和备份时候的不一样,比如test2、test3 ,自定义即可

/dir:备份数据所在位置,例如:c:\data\dump\test ,路径要指定到数据库目录

不同的 mongoDB 版本,参数选项可能不一致,可以在 cmd 命令行中输入mongorestore --help 进行查看

C:\Users\Administrator.SC-07281232>mongorestore --helpUsage:mongorestore <options> <directory or file to restore>Restore backups generated with mongodump to a running server.Specify a database with -d to restore a single database from the target directory,or use -d and -c to restore a single collection from a single .bson file.See /manual/reference/program/mongorestore/ for more information.general options:/help print usage/versionprint the toolversion and exitverbosity options:/v, /verbose:<level> more detailed logoutput (includemultiple times formore verbosity,e.g. -vvvvv, or...........

上面已经手动删除了 MongoDB 中的 mydb1 数据库实例,现在开始恢复上面备份好的E:/wmx/mongoDump/mydb1 数据库同理在 cmd 命令行中直接操作,不用登陆 MongoDB如下所示,mongorestore -h 127.0.0.1:27017 -d mydb1 /dir E:/wmx/mongoDump/mydb1 进行数据恢复

/h :MongoDB 服务器所在的 IP 地址,端口不写时默认为 27017

-d:需要恢复的数据库实例,名称自定义即可

/dir:备份数据所在位置,指定到数据库目录

C:\Users\Administrator.SC-07281232>mongorestore -h 127.0.0.1:27017 -d mydb1 /dir E:/wmx/mongoDump/mydb1-09-12T15:38:02.707+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead-09-12T15:38:02.738+0800 building a list of collections to restore from E:\wmx\mongoDump\mydb1 dir-09-12T15:38:02.739+0800 reading metadata for mydb1.c1 from E:\wmx\mongoDump\mydb1\c1.metadata.json-09-12T15:38:03.129+0800 restoring mydb1.c1 from E:\wmx\mongoDump\mydb1\c1.bson-09-12T15:38:03.131+0800 no indexes to restore-09-12T15:38:03.131+0800 finished restoring mydb1.c1 (100 documents)-09-12T15:38:03.131+0800 doneC:\Users\Administrator.SC-07281232>

登陆 MongoDB 进行查看,如下所示,数据恢复成功

> show dbsadmin 0.000GBconfig 0.000GBlocal 0.000GBmydb1 0.000GBmydb2 0.186GB> use mydb1switched to db mydb1> show tablesc1> db.c1.find().count()100> db.c1.find(){ "_id" : ObjectId("5b98bc379253fbe383c9f04e"), "name" : "zhangSan1", "age" : 1 }{ "_id" : ObjectId("5b98bc379253fbe383c9f04f"), "name" : "zhangSan2", "age" : 2 }{ "_id" : ObjectId("5b98bc379253fbe383c9f050"), "name" : "zhangSan3", "age" : 3 }{ "_id" : ObjectId("5b98bc379253fbe383c9f051"), "name" : "zhangSan4", "age" : 4 }{ "_id" : ObjectId("5b98bc379253fbe383c9f052"), "name" : "zhangSan5", "age" : 5 }{ "_id" : ObjectId("5b98bc379253fbe383c9f053"), "name" : "zhangSan6", "age" : 6 }{ "_id" : ObjectId("5b98bc379253fbe383c9f054"), "name" : "zhangSan7", "age" : 7 }{ "_id" : ObjectId("5b98bc379253fbe383c9f055"), "name" : "zhangSan8", "age" : 8 }{ "_id" : ObjectId("5b98bc379253fbe383c9f056"), "name" : "zhangSan9", "age" : 9 }{ "_id" : ObjectId("5b98bc379253fbe383c9f057"), "name" : "zhangSan10", "age" : 10 }{ "_id" : ObjectId("5b98bc379253fbe383c9f058"), "name" : "zhangSan11", "age" : 11 }{ "_id" : ObjectId("5b98bc379253fbe383c9f059"), "name" : "zhangSan12", "age" : 12 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05a"), "name" : "zhangSan13", "age" : 13 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05b"), "name" : "zhangSan14", "age" : 14 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05c"), "name" : "zhangSan15", "age" : 15 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05d"), "name" : "zhangSan16", "age" : 16 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05e"), "name" : "zhangSan17", "age" : 17 }{ "_id" : ObjectId("5b98bc379253fbe383c9f05f"), "name" : "zhangSan18", "age" : 18 }{ "_id" : ObjectId("5b98bc379253fbe383c9f060"), "name" : "zhangSan19", "age" : 19 }{ "_id" : ObjectId("5b98bc379253fbe383c9f061"), "name" : "zhangSan20", "age" : 20 }Type "it" for more>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。