Import and Export

Last updated: 2019-08-12 11:59:41

PDF

The local database store stores metadata of a replica set, including configuration information and oplog. The admin database manages information about users and roles. To prevent data corruption, authentication failure and other issues, TencentDB for MongoDB does not allow to import local and admin databases into instances.

In CVM, you can use the MongoDB shell client to connect to TencentDB for MongoDB service for data import and export. Please use the latest MongoDB client suite. For more information, see Operation Guide -> Connection Example.

Import Commands

mongodump and mongorestore

MongoDB provides two sets of data import and export tools. If you need to import and export the entire database, we recommend mongodump and mongorestore. The data should be formatted in BSON, which facilitates massive data "dump" and "restore".
The mongodump import command is as follows:

mongodump --host 10.66.187.127:27017 -u mongouser -p thepasswordA1 --authenticationDatabase=admin --db=testdb -o /data/dump_testdb


The mongorestore import command is as follows:

mongorestore --host 10.66.187.127:27017 -u mongouser -p thepasswordA1 --authenticationDatabase=admin --dir=/data/dump_testdb

Export Commands

mongoexport and mongoimport

When you import and export a single collection, we recommend mongoexport and mongoimport . The data should be in JSON format for higher readability.

The mongoexport export command is as follows:

mongoexport --host 10.66.187.127:27017 -u mongouser -p thepasswordA1 --authenticationDatabase=admin --db=testdb --collection=testcollection -o /data/export_testdb_testcollection.json

In addition, you can add the "-f" into the parameter to specify a desired field, and "-q" to specify a query condition that applies restriction on data export.
The mongoimport export command is as follows:

mongoimport --host 10.66.187.127:27017 -u mongouser -p thepasswordA1 --authenticationDatabase=admin --db=testdb --collection=testcollection2 --file=/data/export_testdb_testcollection.json

Parameters of Authentication Methods

As described in the Connection Example, TencentDB for MongoDB provides two user names "rwuser" and "mongouser" by default to support the "MONGODB-CR" and "SCRAM-SHA-1" authentication respectively.

  • For "mongouser" and all new users created in the console, they can simply follow the example above to use the import and export tools.
  • For "rwuser", the parameter "--authenticationMechanism=MONGODB-CR" should be included in each command.

Example of mongodump:

mongodump --host 10.66.187.127:27017 -u rwuser -p thepasswordA1 --authenticationDatabase=admin --authenticationMechanism=MONGODB-CR --db=testdb -o /data/dump_testdb