MongoDB Database Operations--Backup restore Export Import

Source: Internet
Author: User
Tags mongodump mongorestore

MongoDB data backup and restore is mainly divided into two kinds, one is for the library of Mongodump and Mongorestore, one is for the table in the library mongoexport and Mongoimport.

One, Mongodump backup database

1, Common command grid

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

If there is no user who can remove-u and-P.

If you export the native database, you can remove-H.
If it is the default port, you can remove--port.
If you want to export all databases, you can remove-D.

2. Export all databases

[[email protected] MongoDB] #  connected to:127.0.0.13 06:15:55.4483 06:15:55.449 database:test   to   /home/ zhangy/mongodb/3 06:15:55.449   test.system.indexes to/home/zhangy/mongodb/test/3 06:15:55.450     13 06:15:55.450   test.posts to/home/zhangy/mongodb/test/3 06:15:55.480     0 Objects   

3, export the specified database

[[email protected] MongoDB]#mongodump-h 192.168.1.108-d tank-o/home/zhangy/mongodb/Connected to:192.168.1.108Tue Dec3 06:11:41.618 Database:tank to/home/zhangy/mongodb/Tanktue Dec3 06:11:41.623 tank.system.indexes to/home/zhangy/mongodb/tank/System.indexes.bson Tue Dec3 06:11:41.623 2Objects Tue Dec3 06:11:41.623 tank.contact to/home/zhangy/mongodb/tank/Contact.bson Tue Dec3 06:11:41.669 2Objects Tue Dec3 06:11:41.670 Metadata forTank.contact to/home/zhangy/mongodb/tank/Contact.metadata.json Tue Dec3 06:11:41.670 tank.users to/home/zhangy/mongodb/tank/Users.bson Tue Dec3 06:11:41.685 2Objects Tue Dec3 06:11:41.685 Metadata forTank.users To/home/zhangy/mongodb/tank/users.metadata.json

Third, Mongorestore restore the database

1, Common command format

Mongorestore-h IP--port Port-u user name-p password-d database--drop file exists path

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

2. Restore all databases to MongoDB

[[email protected] MongoDB] # mongorestore/home/zhangy/mongodb/  #这里的路径是所有库的备份路径

3. Restore the specified database

[[email protected] MongoDB] # mongorestore-d tank/home/zhangy/mongodb/tank/     [Root@localhost MongoDB]#  mongorestore-d tank_new/home/zhangy/mongodb/tank/  # The tank also has the Tank_new database

These two commands can be implemented to backup and restore the database, the file format is JSON and Bson. Unable to refer to a table backup or restore.

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

1, Common command format

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

The above parameters are good to understand, focus on:
-F export refers to field, split by font size,-f name,email,age export name,email,age three fields
-Q can export the root query condition,-Q ' {"UID": "100"} ' exports the data with UID 100
--csv means that the exported file format is CSV, which is useful because most of the relational databases are supported by CSV, and there's something in common here.

2, export the whole sheet

[[email protected] MongoDB] #  Connected to:127.0.0.1

3. Export some fields from the table

[[email protected] MongoDB] #  Connected to:127.0.0.1

4. Export data according to criteria

[[email protected] MongoDB] #  Connected to:127.0.0.1

Five, Mongoimport import table, or partial field in table

1, Common command format

1.1. Restore a non-CSV file exported from the whole table

Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsert--drop file name  

Focus on--upsert, the other arguments above have been mentioned,--upsert insert or update existing data

1.2. Restore the export file for some fields

Mongoimport-h IP--port Port-u user name-p password-D database-C table name--upsertfields field--drop file name

--upsertfields Root--upsert like

1.3. Restore the exported CSV file

In the above three cases, there can be other permutations and combinations.

2. Restore the exported table data

[[email protected] MongoDB] #

3. Table data import for some fields

[[email protected] MongoDB] # mongoimport-d tank-c users  --upsertfields uid,name,sex  tank/users.dat  connected to:127.0.0.1   Tue Dec  

4. Restore the CSV file

[[email protected] MongoDB] #  Connected to:127.0.0.1

Overall feeling, MongoDB backup and restore, or quite powerful, though a little troublesome.

MongoDB Database Operations--Backup restore Export Import

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.