Migrating and Backing up Databases with Alibaba Cloud ApsaraDB for MongoDB

In this tutorial, we’ll show you how to get started with Alibaba Cloud ApsaraDB for MongoDB. In particular, we will be exploring how to create, migrate, and back up database files with Alibaba Cloud ApsaraDB for MongoDB.

We will demo the ReplicaSet architecture option for MongoDB.

What Is ApsaraDB for MongoDB?

Alibaba Cloud’s ApsaraDB for MongoDB is a secure, reliable, and elastic cloud database service. It currently supports the ReplicaSet and Sharding architectures and can be quickly deployed in just a few steps.

ApsaraDB for MongoDB’s high-availability service includes automatic monitoring, backup, and disaster tolerance capabilities. This saves you from time-consuming database management and system administration tasks leaving you free to focus on your applications and core business.

The ApsaraDB stack is part of Alibaba Cloud’s wider suite of database services. Alibaba Cloud’s ApsaraDB also includes support for RDS, Memcache, and Redis.

Already Have a MongoDB Setup?

You may already have a MongoDB database setup locally. If so, and you wish to migrate to Alibaba Cloud’s ApsaraDB for MongoDB, first check that your MongoDB installation is working.

Start MongoDB with mongod

Run the command line interface with mongo

db.insert() a test username/password pair we can use for the tutorial.

You can also connect to a MongoDB using client software such as with the mongo-java-driver Maven library for coding to the database with Java, for example.

Backup File

ApsaraDB for MongoDB allows you to import database files. Dump your local database to a backup file with mongodump in a new terminal.

Create an Alibaba Cloud ECS Instance

Before you can build a MongoDB instance on Alibaba Cloud, you need an Alibaba Cloud ECS instance. We’ve done these steps before so spin up an Ubuntu 16.04 Pay As You Go ECS instance on Alibaba Cloud in your favorite region. If you haven’t done this before, head over to our ECS instance tutorials.

Setup Ubuntu OS and MongoDB on the ECS

Let’s install everything we need on the Ubuntu 16.04 ECS instance. Logon to the instance and do the requisite Ubuntu update steps:

apt-get update
apt-get upgrade
apt-get dist-upgrade

Then install MongoDB on the ECS instance:

apt-get install -y mongodb

Make sure you have minimum version 3. You can check what version you have by running mongod then on the Mongo console run db.version()

Remember that for all other operating systems, these instructions will vary. Make sure you follow the correct instructions for the operating system of your choice.

Create an Alibaba Cloud ApsaraDB for MongoDB Instance

Now you can create an ApsaraDB for MongoDB instance that is connected to the ECS instance we just created. Go to the Alibaba Cloud Product page and click through to ApsaraDB for MongoDB.

Click Create Instance.

Click the Pay-As-You-Go (Replica Set) tab. Check the details, insert the password for the ECS instance, and click Buy Now.

Agree to Terms and Activate.

You will see the MongoDB instance being created.

Connect to ApsaraDB for MongoDB

Now let’s connect to the ApsaraDB for MongoDB instance on the ECS instance we have just created. First, we have to whitelist a network CIDR block in order to access the MongoDB instance.

Go to the ApsaraDB for MongoDB instance and click the Instance ID link which will take you through to the configuration details.

Click through to Whitelist Setting.

Click Add a Whitelist Group (or under Operation click Manually Modify).

The default whitelist blocks all access with so for the tutorial’s purposes we will manually modify the default to Remember, in the real world you will want to whitelist just a section of a VPC CIDR block.

When you’re ready click OK.

After the whitelist has been set, the Connection Info appears on the ApsaraDB for MongoDB Specification Information tab. Make a note of it.

Now, ssh into the ECS from a new terminal window.

ssh root@

Access the ApsaraDB for MongoDB instance with the following command using the primary address.

mongo --host dds-7go5d2c4cb32e4f41.mongodb.rds.aliyuncs.com:3717 -u root -p Password888 --authenticationDatabase admin

Java Client

You can also connect to your Alibaba Cloud ApsaraDB for MongoDB instance with Java in Eclipse. Before you can access the MongoDB instance in the cloud you have to open some ports with network configuration changes on the ECS instance.

First, install rinetd on the Ubuntu instance (the details given here will be different for other operating systems).

wget http://www.boutell.com/rinetd/http/rinetd.tar.gz&&tar -xvf rinetd.tar.gz&&cd rinetd
sed -i 's/65536/65535/g' rinetd.c
mkdir /usr/man&&make&&make install

Next, add the following configuration details to the rinetd configuration file:

nano /etc/rinetd.conf 3717 dds-7go5d2c4cb32e4f41.mongodb.rds.aliyuncs.com 3717
logfile /var/log/rinetd.log

Then, start the rinetd service:


Check whether rinetd is running and working properly.

Also, make sure to open the MongoDB port 3717 in the Security Group details of the ECS instance. Go to the ECS Management Console Security Groups and select your Security Group. Click Configure Rules, then Add Security Group Rules.

Open port range 3717/3717 to the IP range you wish to have access to your MongoDB instance. We have used for the tutorial’s purposes.

Click OK and restart the instance.

Logon to the ECS instance and make sure rinetd is running. Now you can code directly to the MongoDB instance from your local development environment.

For Java, create a new class and add the appropriate configuration details to match your cloud instance.

Migrate MongoDB to Alibaba Cloud

Copy over your dump file with scp to the ECS instance.

scp dump.rdb root@

Check to make sure the files have arrived successfully.

Then, you can import the backup dump file with the mongorestore command either in command line or Java or another client.

Let’s check to see if our local data is now available in the cloud.

Backing Up

As with all the other Alibaba Cloud ApsaraDB services, ApsaraDB for MongoDB has backup configuration settings.


To summarize, we first made sure our local MongoDB database was functioning properly and we created a new database and a user with a password. Then, we made backup files of the local MongoDB.

Next, we created an Alibaba Cloud ECS Instance on which to host our ApsaraDB for MongoDB instance. We configured Ubuntu 16.04 as our operating system and setup MongoDB on the instance.

Then, we created an Alibaba Cloud ApsaraDB for MongoDB instance which was attached to the Ubuntu ECS we had just created. Then we performed some network administration in order to be able to access our Alibaba Cloud ApsaraDB for MongoDB instance from outside the cloud. We chose a Java client and wrote a test class to check that we could connect to the cloud database.

We imported our local database dump to the Alibaba Cloud instance by first copying it over with scp and then checking whether it had migrated successfully.

Don’t forget to check out our other Alibaba Cloud ApsaraDB tutorials for Memcache, RDS, and Redis along with all the other products and services Alibaba Cloud offers.