Skip to main content

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:

  1. Export the database from MySQL and MongoDB to JSON or CSV format
  2. Create a CloudBase environment
  3. Go to the CloudBase database and create a new collection
  4. 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

img

Select csv format in the type

img

Note: In step 4, we need to check the option to include column headers.

img

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:

img

Export to JSON Format

Similarly, we will export the selected table(s) in json format:

img

For the remaining steps, simply accept all default settings.

What does the exported result look like?

img

What does the final result look like after removing the array?

img

Exporting MongoDB Data

First, start the mongod service:

img

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:

img

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:

img

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:

img

After creating the environment, please wait patiently for the 2-minute initialization process.

Database Import

Click Add Collection to create a collection:

img

After creating it, we click to enter and perform the import operation:

img

Select the previously exported CSV or JSON format file.

Note

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:

img

After the import process is complete, the imported data can be viewed in the database:

img