本文介绍: MongdoDB一共提供了4个命令行工具给我们对数据进行备份与恢复以及导入与导出数据。(2)准备测试数据,mongoDB终端下进行以下操作,插入200000条数据。(4)删除orders表中的多个文档 ,目前显示剩余190078条文档。(1)查询demo数据表的orders数据表文件(默认每次20个)(3)修改返回结果数据的单页显示数量为30条,默认20条。导入与导出数据的数据文件格式是json格式,文本格式。(8)查看导出文件,一共200000条。(10)导入数据进行恢复。(11)查看恢复情况。
一、实验
1.MongoDB 数据库备份与恢复
root@node1:~# mongo --version
(2)准备测试数据,mongoDB终端下进行以下操作,插入200000条数据
use demo
fotmatnumber = (start, end)=>{
num = Math.round(Math.random() * (end-start)) + start
if(num<10){
return "0"+num;
}else{
return num;
}
}
rand_title = (i)=>{
num = Math.round( Math.random() * 10 );
num1 = Math.round( Math.random() * 10 );
return [
"赠送礼品-"+num,
"购物狂欢-"+num,
"随便买买-"+num,
"愉快购物-"+num,
"赠送礼物-"+num,
"商品购买-"+num,
"买多送多-"+num,
"买年货-"+num,
"买买买买-"+num,
"充值会员-"+num
][num1];
}
for(var i=0; i<200000; i++){
db.orders.insert({
"onumber": ( "0000000000000000" + i ).substr( String(i).length ),
"date": "20"+fotmatnumber(0,21)+"-"+fotmatnumber(1,12)+"-"+fotmatnumber(1,31),
"title": rand_title(i),
"user_id": parseInt(i/200)+1,
"items" :[{
"goods_id" : parseInt(i/200)+1,
"goods_attr" : i,
"price" : 100.0
},{
"goods_id" : parseInt(i/200)+2,
"goods_attr" : i+1,
"price" : 80.0
}]
})
if(i%10000==0){
print("已经添加了"+parseInt(i/10000)+"万条数据!");
}
}
root@node1:~# mongo
……
> show dbs;
admin 0.000GB
config 0.000GB
demo 0.010GB
local 0.000GB
> exit
bye
(3) 数据备份
root@node1:~# mongodump -h 127.0.0.1:27017 -d demo -o backup
root@node1:~# mongo
……
> show dbs;
admin 0.000GB
config 0.000GB
demo 0.010GB
local 0.000GB
> use demo;
switched to db demo
> db.dropDatabase()
{ "dropped" : "demo", "ok" : 1 }
> show dbs;
admin 0.000GB
config 0.000GB
local 0.000GB
> exit
bye
(5)数据恢复
root@node1:~# mongorestore -h 127.0.0.1:27017 -d demo --dir /root/backup/demo
(6)查看数据恢复情况
root@node1:~# mongo
……
> use dbs;
switched to db dbs
> show dbs;
admin 0.000GB
config 0.000GB
demo 0.009GB
local 0.000GB
2.MongoDB 数据表备份与恢复
(1)查询demo数据表的orders数据表文件(默认每次20个)
……
> use demo;
switched to db demo
> show tables;
orders
> db.orders.find()
Type "it" for more
> it
> DBQuery.shellBatchSize=30
30
(4)删除orders表中的多个文档 ,目前显示剩余190078条文档
> db.orders.deleteMany({"title":"买买买买-1"})
{ "acknowledged" : true, "deletedCount" : 2037 }
> db.orders.deleteMany({"title":"买买买买-3"})
{ "acknowledged" : true, "deletedCount" : 1988 }
> db.orders.deleteMany({"title":"买买买买-4"})
{ "acknowledged" : true, "deletedCount" : 2062 }
> db.orders.deleteMany({"title":"买买买买-5"})
{ "acknowledged" : true, "deletedCount" : 1902 }
> db.orders.deleteMany({"title":"买买买买-8"})
{ "acknowledged" : true, "deletedCount" : 1933 }
> db.order.count()
0
> db.orders.count()
190078
> exit
bye
(5)数据进行恢复
root@node1:~# mongorestore -h 127.0.0.1:27017 -d demo --dir /root/backup/demo --drop
2023-11-26T21:21:23.636-0800 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2023-11-26T21:21:23.637-0800 building a list of collections to restore from /root/backup/demo dir
2023-11-26T21:21:23.637-0800 reading metadata for demo.orders from /root/backup/demo/orders.metadata.json
2023-11-26T21:21:23.638-0800 dropping collection demo.orders before restoring
2023-11-26T21:21:23.667-0800 restoring demo.orders from /root/backup/demo/orders.bson
2023-11-26T21:21:25.993-0800 finished restoring demo.orders (200000 documents, 0 failures)
2023-11-26T21:21:25.993-0800 no indexes to restore for collection demo.orders
2023-11-26T21:21:25.993-0800 200000 document(s) restored successfully. 0 document(s) failed to restore.
(6)查看恢复情况
root@node1:~# mongo
……
> use demo;
switched to db demo
> db.orders.count()
200000
> exit
bye
(7)数据导出
root@node1:~# mongoexport -d demo -c orders -o /root/backup/orders.json --type json
2023-11-26T21:24:52.113-0800 connected to: mongodb://localhost/
2023-11-26T21:24:53.117-0800 [##......................] demo.orders 24000/200000 (12.0%)
2023-11-26T21:24:54.117-0800 [######..................] demo.orders 56000/200000 (28.0%)
2023-11-26T21:24:55.116-0800 [########................] demo.orders 72000/200000 (36.0%)
2023-11-26T21:24:56.116-0800 [############............] demo.orders 104000/200000 (52.0%)
2023-11-26T21:24:57.117-0800 [################........] demo.orders 136000/200000 (68.0%)
2023-11-26T21:24:58.117-0800 [####################....] demo.orders 168000/200000 (84.0%)
2023-11-26T21:24:59.056-0800 [########################] demo.orders 200000/200000 (100.0%)
2023-11-26T21:24:59.056-0800 exported 200000 records
(8)查看导出文件,一共200000条
(9)删除数据表orders
root@node1:~/backup# mongo
……
> use demo;
switched to db demo
> show tables;
orders
> db.orders.drop()
true
> show tables;
> exit
bye
(10)导入数据进行恢复
root@node1:~/backup# mongoimport -d demo -c orders --file /root/backup/orders.json --type json
2023-11-26T21:29:06.494-0800 connected to: mongodb://localhost/
2023-11-26T21:29:09.495-0800 [########................] demo.orders 16.7MB/48.0MB (34.8%)
2023-11-26T21:29:12.495-0800 [################........] demo.orders 32.0MB/48.0MB (66.8%)
2023-11-26T21:29:15.311-0800 [########################] demo.orders 48.0MB/48.0MB (100.0%)
2023-11-26T21:29:15.311-0800 200000 document(s) imported successfully. 0 document(s) failed to import.
(11)查看恢复情况
root@node1:~/backup# mongo
……
> use demo
switched to db demo
> show tables;
orders
> db.orders.count()
200000
> exit
bye
二、问题
1.MongoDB有哪些命令行工具实现数据备份与恢复
MongdoDB一共提供了4个命令行工具给我们对数据进行备份与恢复以及导入与导出数据。
(2)备份与恢复
1)数据备份
mongodump -h dbhost -d dbname -o dbdirectory
# 例如:备份一个demo的数据库
mongodump -h 127.0.0.1:27017 -d demo -o /root/backup
2)数据恢复
mongorestore -h dbhost -d dbname --dir dbdirectory --drop
# 例如:恢复上面备份的demo数据库
mongorestore -h 127.0.0.1:27017 -d demo --dir /root/backup/demo
(3) 导入与导出
1) 数据导出
mongoexport -d dbname -c collectionname -o file --type json/csv -f field
# 例如:导出demo数据库的orders集合数据
mongoexport -d demo -c orders -o /root/backup/orders.json --type json
2) 数据导入
mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
# 例如:把上面导出的json中的数据,导入到demo数据库的orders集合中
mongoimport -d demo -c orders --file /order/backup/orders.json --type json
原文地址:https://blog.csdn.net/cronaldo91/article/details/134643205
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_17793.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。