This topic describes how to migrate data from Amazon DynamoDB to ApsaraDB for MongoDB by using mongoimport, which is built in MongoDB for data restoration. You can install a MongoDB database on an on-premises server or in an ECS instance as an intermediary, and then use mongoimport to migrate data from an Amazon DynamoDB instance to an ApsaraDB for MongoDB instance.
- This is full data migration. Incremental data migration is not supported. To ensure data consistency, we recommend that you do not write data to the source database before you migrate data.
- Run the mongoimport command on the server. Do not run this command in the mongo shell.
|Term in Amazon DynamoDB||Term in ApsaraDB for MongoDB|
Install a MongoDB database on the on-premises server. Make sure that the version of the database is the same as the database version of the ApsaraDB for MongoDB instance. This server plays an intermediary role during data migration. In this example, MongoDB is installed on Ubuntu.
- Install the MongoDB database to your server. For more information, visit Install MongoDB Community Edition.
Note This server plays an intermediary role in both data backup and restoration. The server is no longer required after the migration is complete.
- Add the public IP address of the server to a whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist or an ECS security group for an ApsaraDB for MongoDB instance.
- Log on to the Amazon DynamoDB console.
- In the left-side navigation pane, click Tables.
- Select the table you want to migrate. For this example, select customer.
- Select the data you want to migrate and export it to a .csv file.
- Click the Items tab.
- Select the items you want to migrate.
- Choose .
- Copy the exported .csv file to the server described in Preparation.
- Log on to the ApsaraDB for MongoDB console to obtain the public endpoint of the ApsaraDB for MongoDB instance.
Note You must manually apply for a public endpoint. For more information, see Apply for a public endpoint for an ApsaraDB for MongoDB instance.
- If you migrate data to a replica set instance of ApsaraDB for MongoDB, obtain the public endpoint of the primary node. For more information, see Overview of replica set instance connections.
- If you migrate data to a sharded cluster instance of ApsaraDB for MongoDB, obtain the public endpoint of a mongos. For more information, see Overview of sharded cluster instance connections.
- On the server, run the following command to import the backup files to an ApsaraDB for MongoDB database:
mongoimport --host <mongodb_host:port> --authenticationDatabase admin -u <username> --db <database> --collection <collection> --file <filename> --type csv --headerlineNote
The following command is used to import data from the customer.csv file to the customer collection in the mongodbtest database.
- <mongodb_host:port>: the public endpoint of the primary node in the replica set instance of ApsaraDB for MongoDB, or the public endpoint of a mongos in the sharded cluster instance of ApsaraDB for MongoDB.
- <username>: the username of the destination database in the ApsaraDB for MongoDB instance. The initial username is root. This user must have read and write permissions on the destination database.
- <database>: the name of the authentication database. It is the database where the database user is created. If the username is root, enter admin.
- <collection>: the name of the collection to which you want to import data.
- <filename>: the path and name of the .csv file.
mongoimport --host dds-bpxxxxxxxx-pub.mongodb.rds.aliyuncs.com:3717 --authenticationDatabase admin -u root --db mongodbtest --collection customer --file ~/download/customer.csv --type csv --headerline
Enter password:is displayed, enter the password of the ApsaraDB for MongoDB database user and press Enter.Note If you want to migrate data of other tables, repeat Steps 3 to 8.
After data import is complete, the data of the source Amazon DynamoDB instance is migrated to the destination ApsaraDB for MongoDB instance.
What to do next
To improve the performance of data operations, we recommend that you create indexes after you import data. For more information, visit db.collection.createIndex.