Migrate a self-managed MongoDB database from an on-premises server or an ECS instance to an ApsaraDB for MongoDB standalone instance by using mongodump and mongorestore.
For zero-downtime migrations, use Data Transmission Service (DTS) instead. For more information, see Full migration from ApsaraDB for MongoDB (standalone architecture) to ApsaraDB for MongoDB (all architectures).
For other migration or synchronization scenarios, see Data migration and synchronization.
Prerequisites
-
The versions of the
mongodumpandmongorestoretools must match the version of your self-managed MongoDB database. For installation instructions, see the official Install MongoDB documentation.NoteAlternatively, you can run the
mongodumpandmongorestorecommands on the server that hosts your self-managed database without installing them separately. -
Your self-managed MongoDB database must be version 3.0 or later.
NoteFor information about supported storage engines, see Versions and storage engines. If you are migrating across different versions or storage engines, verify compatibility before you start.
-
The destination standalone instance must have sufficient storage capacity for the self-managed database. If the storage capacity is insufficient, you can increase it. For more information, see Modify instance configuration.
Usage notes
-
This is a full data migration. To ensure data consistency, stop all related services and write operations on your self-managed database before you start.
-
If you have previously run
mongodump, move the existing backup files out of the dump folder before you start. Otherwise, the existing backup files will be overwritten. -
Run
mongodumpandmongorestoreon the server that hosts your self-managed MongoDB database, not from within themongo shell.
Step 1: Back up the self-managed database
-
On the server that hosts your self-managed database, run the following command to back up all databases.
mongodump --host <mongodb_host> --port <port> -u <username> --authenticationDatabase <database>Note-
<mongodb_host>: The server address of your self-managed MongoDB database. You can use127.0.0.1if you are running the command on the database server. -
<port>: The port number of the database service. The default value is27017. -
<username>: The account for the self-managed database, such astest. -
<database>: The authentication database to which the user account belongs.
Example:
mongodump --host 127.0.0.1 --port 27017 -u test --authenticationDatabase admin -
-
When the CLI prompts
Enter password:, enter the database account password and press Enter to begin the backup.NotePassword characters are hidden when you enter them.
After the backup is complete, the data from your self-managed database is saved to the dump folder in the current directory.
Step 2: Migrate data to Alibaba Cloud
-
Obtain the primary node endpoint of the ApsaraDB for MongoDB instance.
-
Log on to the ApsaraDB for MongoDB console.
-
In the upper-left corner of the page, select the region where the instance is located.
-
In the left-side navigation pane, click Replica Set Instances.
-
Find the target instance and click the instance ID.
-
In the left-side navigation pane, click Database Connections to view connection details.
On the connection information page, the VPC and public connection sections display the endpoint for the primary node (for example,
dds-<instanceID>.mongodb.rds.aliyuncs.com:3717, where public endpoints include the-pubsuffix) and a ConnectionStringURI (for example,mongodb://root:****@<endpoint>/admin). Use the primary node endpoint for the migration.Table 1. Connection endpoints Endpoint type
Description
Scenario
VPC endpoint
An isolated network environment that provides higher security and performance than the classic network.
Use this when your self-managed MongoDB database is on an ECS instance in the same region and VPC as the ApsaraDB for MongoDB instance.
NoteThe ECS instance and the ApsaraDB for MongoDB instance must be in the same region and VPC.
public endpoint
Not enabled by default. You must apply for one before use. For more information, see Apply for a public endpoint.
Use this when your self-managed MongoDB database is on an on-premises server.
-
-
Add the IP address of your self-managed database's server to the whitelist of the ApsaraDB for MongoDB instance. For more information, see Configure a whitelist.
Note-
If you connect to the ApsaraDB for MongoDB instance over a VPC, add the private IP address of the ECS instance that hosts your self-managed database to the whitelist.
-
If you connect to the ApsaraDB for MongoDB instance using a public endpoint, add the public IP address of the on-premises server that hosts your self-managed database to the whitelist.
-
-
On the server that hosts your self-managed database, run the following command to restore the data to the ApsaraDB for MongoDB instance.
mongorestore --host <Primary_host> -u <username> --authenticationDatabase <database> <Backup directory>Note-
<Primary_host>: The primary node endpoint for your ApsaraDB for MongoDB instance. -
<username>: The database account for the ApsaraDB for MongoDB instance. The initial account is root. -
<database>: The authentication database. If you use therootaccount, this database isadmin. -
<Backup directory>: The directory where the backup files are stored. The default directory is dump.
Example:
The database account is
test, and the authentication database isadmin.mongorestore --host dds-bp19f409d7512****-pub.mongodb.rds.aliyuncs.com:3717 -u test --authenticationDatabase admin dump -
-
When the CLI prompts
Enter password:, enter the password for the ApsaraDB for MongoDB database account and press Enter. The data restoration process starts.Note-
Password characters are hidden when you enter them.
-
If you have forgotten the password for the
rootaccount, you can reset it. For more information, see Reset a password.
-
After the migration is complete, switch your services to the ApsaraDB for MongoDB instance when ready.
Next steps
After the migration, connect to the ApsaraDB for MongoDB instance to manage databases and accounts.