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.
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.
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
Run the command line interface with
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.
ApsaraDB for MongoDB allows you to import database files. Dump your local database to a backup file with
mongodump in a new terminal.
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 Alibaba Cloud ECS instanc installed with Ubuntu 16.04 in your favorite region. If you haven't done this before, head over to our ECS instance tutorials.
Let's install everything we need on the ECS instance. Log on 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
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.
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 the terms and click Activate.
You will see the MongoDB instance being created.
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
127.0.0.1 so for the tutorial's purposes we will manually modify the default to
0.0.0.0/0. Remember, in the real world you will want to whitelist just a section of a VPC CIDR block.
Then, 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.
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
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.
rinetd on your ECS instance. Note that the details given here are for an ECS instance installed with Ubuntu. These details are 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 0.0.0.0 3717 dds-7go5d2c4cb32e4f41.mongodb.rds.aliyuncs.com 3717 logfile /var/log/rinetd.log
Then, start the
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
0.0.0.0/0 for the tutorial's purposes.
Click OK and restart the instance.
Log on to your 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.
Copy over your dump file with
scp to your ECS instance.
scp dump.rdb email@example.com:/home
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.
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.
Alibaba Clouder - July 30, 2019
Alibaba Clouder - February 14, 2020
Alibaba Clouder - February 21, 2020
Alibaba Clouder - August 2, 2019
Alibaba Clouder - February 25, 2021
Alibaba Clouder - November 3, 2020
A secure, reliable, and elastically scalable cloud database service for automatic monitoring, backup, and recovery by time pointLearn More
An online computing service that offers elastic and secure virtual cloud servers to cater all your cloud hosting needs.Learn More
An on-demand database hosting service for MySQL with automated monitoring, backup and disaster recovery capabilitiesLearn More
More Posts by Alibaba Clouder