Migrating Data from MySQL/MongoDB to CloudBase Cloud Database
Migration Instructions
This article covers migration from MySQL and MongoDB to CloudBase Cloud Database, and other database migrations are largely similar.
Migration is roughly divided into the following steps:
- Export the database from MySQL and MongoDB to JSON or CSV format
- Create a CloudBase environment
- Go to the CloudBase database and create a new collection
- Import JSON or CSV format files into the collection
Export
Exporting MySQL Data
In the following steps, we use Navicat for MySQL for exporting. You can also use other MySQL export tools.
Export to CSV Format
Select the table(s) and export
Select csv format in the type
Note: In step 4, we need to check the option to include column headers.
The exported csv file contains the first row with all key names, and each subsequent row contains corresponding key-value records matching the key names in the first row. For example:
Export to JSON Format
Similarly, we will export the selected table(s) in json format:
For the remaining steps, simply accept all default settings.
What does the exported result look like?
What does the final result look like after removing the array?
Exporting MongoDB Data
First, start the mongod service:
Do not close this terminal after starting.
Export to CSV Format
Open a new terminal and enter the following command
mongoexport -db <database> --collection <collection name> --type csv -f <field1[,field2]> -o <output file path>
For more detailed parameter descriptions, please refer to the MongoDB documentation.
Note: When exporting to csv format, you must specify the field names to export; otherwise, an error will occur:
csv mode requires a field list
Exported result:
Export to JSON Format
Open a new terminal and enter the following command:
mongoexport -db <database> --collection <collection name> -o <output file path>
For more detailed parameter descriptions, please refer to the MongoDB documentation.
Exported result:
Import
Create a New Cloud Environment
If you already have a cloud environment, you can skip this step and directly open the Cloud Development Console to create a new cloud environment:
After creating the environment, please wait patiently for the 2-minute initialization process.
Database Import
Click Add Collection to create a collection:
After creating it, we click to enter and perform the import operation:
Select the previously exported CSV or JSON format file.
There are two conflict resolution modes: Insert and Upsert
- Insert mode always inserts new records during import. Duplicate _id fields are not allowed in the same file or matching existing _id values in the database. Use Insert mode if you want to prevent existing data from being overwritten.
- Upsert mode checks whether a record exists. If it exists, the record will be updated; otherwise, a new record will be inserted. Use Upsert mode if you want to avoid redundant or duplicate data. :::
Here we select Upsert mode:
After the import process is complete, the imported data can be viewed in the database: