How to migrate data from DocumentDB cluster to MongoDB
Amazon DocumentDB is a fully managed document database service. It is compatible with the open-source MongoDB 3.6 API and enables you to easily migrate from a self-managed database. Amazon DocumentDB handles cluster scaling, instance failover, data backups, and software updates. Rely on the efficiencies of the AWS Cloud to use a faster, cheaper, and more reliable database option.
Prerequisites :-
1. Create EC2 instance
2. Assign inbound and outbound rules
3. Install Mongo dB in EC2 Instance
4. Create documentDB cluster
5. Make ssh connection between documentdb cluster and EC2 instance.
6. Create database in cluster and add to it.
7. Export documentdb data to EC2 instance.
8. Import data from EC2 Instance to mongodb.
STEP 1 : Create EC2 Instance :-
- Login and access to AWS services.
- Choose AMI.
- Choose EC2 Instance Types.
- Configure Instance.
- Add Storage.
- Tag Instance.
- Configure Security Groups.
- Review Instances.
- To create key pair - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/get-set-up-for-amazon-ec2.html
STEP 2 : Assign inbound and outbound rules :-
1. Add port 27017 (i.e. MongoDB port) in inbound round.
2. Allow all traffic in outbound port.
STEP 3 : Install Mongo dB in EC2 Instance : -
1. # curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add –
2. # apt-key list
3. # echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
4.
# apt
install mongodb-org
5. #systemctl start mongod.service
6.
#systemctl status mongod
7.
#systemctl
enable mongod
8. #mongo --eval 'db.runCommand({ connectionStatus: 1 })'
STEP 4 : Create documentDB cluster :-
1. Create Amazon DocumentDB database cluster :-
To get started, navigate to the DocumentDB console. On the Clusters page, choose Create to create a new cluster.
2.This launches the cluster creation wizard. In the Configuration section, give your cluster a name.
Then choose the instance class and number of instances to create for your cluster.
An Amazon DocumentDB cluster requires at least one instance, which serves as the primary instance.
3.3. In the Authentication section, enter the master username and password for your Amazon DocumentDB cluster. Make sure you save this information so that you can authenticate to your database cluster.
4. 4. You need to configure a few more settings for your database cluster, so choose Show advanced settings near the bottom of the wizard.
5. 5. In the advanced settings, there is a Network settings section. Choose the Amazon Virtual Private Cloud (Amazon VPC) subnet group and security group for your Amazon DocumentDB cluster.
6. 6. After you have configured the network settings for your Amazon DocumentDB database cluster, choose Create cluster to create your Amazon DocumentDB database cluster.
7. 7. Amazon DocumentDB begins provisioning your database cluster. Your cluster should now show a Status of creating.
STEP 5 : Make ssh connection between documentdb cluster and EC2 instance :-
1.Hostname: sample-cluster.cluster-cu52jq5kfddg.us-east-1.docdb.amazonaws.com
2.Port: 27017
3.Authentication: Username/Password
4.Username: YourDocDBUsername
5.Password: YourDocDBUserPassword
6.SSL: Server validation
7.Certificate Authority: (select downloaded rds-combined-ca-bundle.pem)
8.SSH Tunnel: Use identity file
9.SSH Hostname: ec2-34-229-221-164.compute-1.amazonaws.com
10.SSH Tunnel Port: 22
11.SSH Username: ubuntu
12.SSH Identity File: ec2Access.pem
STEP 6 : Create database in cluster and add to it :-
1. Now install the mongo shell in DocumentDB cluster.
2. Now add single json document for testing purpose in DocumentDB cluster .
For e.g;
db.example.insertOne(
{
"Item": "Ruler",
"Colors": ["Red","Green","Blue","Clear","Yellow"],
"Inventory": {
"OnHand": 47,
"MinOnHand": 40
},
"UnitPrice": 0.89
}
)
3. Retrieve the document in a collection : -
db.example.find( {} ).pretty()
show collection
STEP 6 : Export documentdb data to EC2 instance :-
1. Run the following command in EC2 Instance.
# mongoexport --db DB_NAME --collection COLLECTION_name --fields
Field_name(s) --type=[csv or JSON} --out=Name-Path-Output-File
STEP 7 : Import data from EC2 Instance to mongodb :-
1. After successfully exporting data from doucmentdb cluster to EC2 instance import json file to mongodb.
2. Command is :-
# mongoimport –collection=’collection_name’
3. Now type the below command to check it.
#mongo
>show collection
It will display the imported json file .