MongoDB Database Operations--Backup restore Export Import _mongodb

Source: Internet
Author: User
Tags mongodb mongodump mongorestore

One, Mongodump backup database

1, Common command grid

Mongodump-h IP--port Port-u user name-p password-d database-o file existence path 

If there are no users who can remove-u and-P.
If you export the database for this computer, you can remove-H.
If it is the default port, you can remove the--port.
If you want to export all databases, you can remove-D.

2, export all databases

[Root@localhost mongodb]# mongodump-h 127.0.0.1-o/home/zhangy/mongodb/ 
connected to:127.0.0.1 
Tue Dec 3 06:15 : 55.448 all DBS 
Tue Dec 3 06:15:55.449 database:test   to   /home/zhangy/mongodb/test 
Tue Dec 3 06:15:55.449   test.system.indexes to/home/zhangy/mongodb/test/system.indexes.bson 
Tue Dec 3 06:15:55.450     1 Objects 
Tue Dec 3 06:15:55.450   test.posts to/home/zhangy/mongodb/test/posts.bson 
Tue Dec 3 06:15:55.480     0 Objects ............. 
 
Omitted.......................... 

3, export the specified database

[Root@localhost mongodb]# mongodump-h 192.168.1.108-d tank-o/home/zhangy/mongodb/connected To:192.168.1.108
   tue Dec 3 06:11:41.618 database:tank   to   /home/zhangy/mongodb/tank 
Tue Dec 3 06:11:41.623   Tank.system.indexes To/home/zhangy/mongodb/tank/system.indexes.bson 
Tue Dec 3 06:11:41.623     2 objects 
Tue Dec 3 06:11:41.623   tank.contact to/home/zhangy/mongodb/tank/contact.bson 
Tue Dec 3 06:11:41.669     2 Objects 
Tue Dec 3 06:11:41.670   Metadata for Tank.contact To/home/zhangy/mongodb/tank/contact.metadata.json 
Tue Dec 3 06:11:41.670   tank.users to/home/zhangy/mongodb/tank/users.bson 
Tue Dec 3 06:11:41.685     2 Objects 
Tue Dec 3 06:11:41.685   Metadata for Tank.users To/home/zhangy/mongodb/tank/users.metadata.json 

Three, Mongorestore restore the database

1, commonly used command format

 
 


--drop means to delete all the records first and then restore.

2, restore all databases to MongoDB

[Root@localhost mongodb]# mongorestore/home/zhangy/mongodb/  


3, restore the specified database

[Root@localhost mongodb]# mongorestore-d tank/home/zhangy/mongodb/tank/  #tank这个数据库的备份路径 
 
[root@localhost mongodb]# mongorestore-d tank_new/home/zhangy/mongodb/tank/  #将tank还有tank_new数据库中


These two commands enable the database to be backed up and restored, and the file format is JSON and Bson. Cannot point to write to table backup or restore.

Four, Mongoexport export tables, or some of the fields in the table

1, commonly used command format

Mongoexport-h IP--port Port-u username-p password-D database-C table name-F field-Q condition export--csv-o file name 

The above parameter is good to understand, the key point says:
-F export refers to fields, separated by font size,-f name,email,age export name,email,age three fields
-Q can be exported by root query condition,-Q ' {' uid ': ' 100 '} ' export UID 100 data
--csv indicates that the exported file format is CSV, which is useful because most relational databases are supported by CSV, where there is common

2, export the entire table

[Root@localhost mongodb]# mongoexport-d tank-c users-o/home/zhangy/mongodb/tank/users.dat to 
: 127.0.0.1 
exported 4 records 

3, export some of the fields in the table

[Root@localhost mongodb]# mongoexport-d tank-c users--csv-f uid,name,sex-o tank/users.csv connected to:127.0.0.1 
exported 4 Records 

4, according to the conditions to dare to data

[Root@localhost mongodb]# mongoexport-d tank-c users-q ' {uid:{$gt: 1}} '-O Tank/users.json connected 
10/>exported 3 Records 

Five, Mongoimport Import the table, or some fields in the table

1, commonly used command format

1.1, restore the entire table exported non-CSV file
Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop filename
Focus on--upsert, other parameters above the command already mentioned,--upsert insert or update existing data
1.2, restore the exported file of some fields
Mongoimport-h IP--port Port-u username-p password-D database-C table name--upsertfields field--drop filename
--upsertfields Root--upsert.
1.3, restore the exported CSV file
Mongoimport-h IP--port Port-u username-p password-D database-C table name--type type--headerline--upsert--drop filename
In the above three cases, there can be other permutations and combinations.

2, restore the exported table data

[Root@localhost mongodb]# mongoimport-d tank-c users--upsert tank/users.dat connected to:127.0.0.1 Tue 
Dec 3 0 8:26:52.852 Imported 4 objects

3, partial field of table data import

[Root@localhost mongodb]# mongoimport-d tank-c users--upsertfields uid,name,sex tank/users.dat
Connected to:127.0.0.1
Tue DEC 3 08:31:15.179 imported 4 objects

4, restore CSV file

[Root@localhost mongodb]# mongoimport-d tank-c users--type csv--headerline--file tank/users.csv connected 
.0.0.1 
Tue Dec 3 08:37:21.961 imported 4 objects 

Overall feel, MongoDB backup and restore, still quite powerful, although a bit troublesome.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.