温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB 备份恢复命令说明

发布时间:2020-06-20 10:11:01 来源:网络 阅读:556 作者:pgmia 栏目: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集合,验证导入是否成功:


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI