MongoDB备份恢复命令说明
一、全库备份恢复
站在用户的角度思考问题,与客户深入沟通,找到芒市网站设计与芒市网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都网站制作、网站设计、企业官网、英文网站、手机端网站、网站推广、主机域名、雅安服务器托管、企业邮箱。业务覆盖芒市地区。
#mongodump -d my_MongoDB -o my_mongodb_dump
#mongorestore -d my_mongodb my_mongodb_dump/*
二、JSON Collection备份恢复
$ mongoexport -d test -c book -o book.json
connected to: 10.77.20.xx
exported 2 records
$ mongoimport -d test -c book book.json
connected to: 10.77.20.xx
imported 2 objects
三、CSV Collection数据导出导入
# more students.txt
{ "classid" : 1, "age" : 20, "name" : "kobe" }
{ "classid" : 1, "age" : 23, "name" : "nash" }
{ "classid" : 2, "age" : 18, "name" : "james" }
{ "classid" : 2, "age" : 19, "name" : "wade" }
{ "classid" : 2, "age" : 19, "name" : "bosh" }
{ "classid" : 2, "age" : 25, "name" : "allen" }
{ "classid" : 1, "age" : 19, "name" : "howard" }
{ "classid" : 1, "age" : 22, "name" : "paul" }
{ "classid" : 2, "age" : 24, "name" : "shane" }
#
1.导入数据
# mongoimport -d test -c students students.txt
connected to: 127.0.0.1
Tue Apr 22 16:02:32 imported 9 objects
> db.students.find()db.students.find()
{ "_id" : ObjectId("53562218bc98e02b24984175"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("53562218bc98e02b24984176"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("53562218bc98e02b24984177"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("53562218bc98e02b24984178"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("53562218bc98e02b24984179"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("53562218bc98e02b2498417a"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("53562218bc98e02b2498417b"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("53562218bc98e02b2498417c"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("53562218bc98e02b2498417d"), "classid" : 2, "age" : 24, "name" : "shane" }
>
2.导出CSV
# mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
connected to: 127.0.0.1
exported 9 records
#
导出只有数据,不包括_id
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
[root@jsmartserv hbk]# more students_csv.dat
classid,name,age
1,"kobe",20
1,"nash",23
2,"james",18
2,"wade",19
2,"bosh",19
2,"allen",25
1,"howard",19
1,"paul",22
2,"shane",24
[root@jsmartserv hbk]#
3.删除数据
db.students.remove()
4.导入CSV
# mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
Tue Apr 22 16:07:57 imported 9 objects
参数说明:
-type:指明要导入的文件格式
-headerline:指明第一行是列名,不需要导入
-file:指明要导入的文件
5.验证
db.students.find()db.students.find()
{ "_id" : ObjectId("5356235dbc98e02b2498417e"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("5356235dbc98e02b2498417f"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("5356235dbc98e02b24984180"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("5356235dbc98e02b24984181"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984182"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984183"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("5356235dbc98e02b24984184"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("5356235dbc98e02b24984185"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("5356235dbc98e02b24984186"), "classid" : 2, "name" : "shane", "age" : 24 }
>
查询students集合,验证导入是否成功:
分享名称:MongoDB备份恢复命令说明
标题路径:http://azwzsj.com/article/gshjeh.html