溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MongoDB 備份恢復(fù)命令說明

發(fā)布時間:2020-06-20 10:11:01 來源:網(wǎng)絡(luò) 閱讀:553 作者:pgmia 欄目:MongoDB數(shù)據(jù)庫

一、全庫備份恢復(fù)


#mongodump -d my_mongodb -o my_mongodb_dump


#mongorestore -d my_mongodb my_mongodb_dump/*




二、JSON Collection備份恢復(fù)


$ 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數(shù)據(jù)導(dǎo)出導(dǎo)入


# 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.導(dǎo)入數(shù)據(jù)


# 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.導(dǎo)出CSV


# mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat

connected to: 127.0.0.1

exported 9 records

#

導(dǎo)出只有數(shù)據(jù),不包括_id

參數(shù)說明:

-h:指明數(shù)據(jù)庫宿主機(jī)的IP

-u:指明數(shù)據(jù)庫的用戶名

-p:指明數(shù)據(jù)庫的密碼

-d:指明數(shù)據(jù)庫的名字

-c:指明collection的名字

-f:指明要導(dǎo)入那些列



[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.刪除數(shù)據(jù)


db.students.remove()



4.導(dǎo)入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



參數(shù)說明:

-type:指明要導(dǎo)入的文件格式

-headerline:指明第一行是列名,不需要導(dǎo)入

-file:指明要導(dǎo)入的文件


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集合,驗證導(dǎo)入是否成功:


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI