Skip to main content

Migrate data from MySQL/MongoDB to CloudBase Database

Migration Notes

This article covers migrating from MySQL and MongoDB to CloudBase Database. Migrations from other databases follow a similar approach.

The migration process can be broken down into the following steps:

  1. Export databases from MySQL and MongoDB to JSON or CSV format.
  2. Create a Cloud Development environment.
  3. Create a new collection in the Cloud Development database.
  4. Import JSON or CSV format files in the collection.

Export

Export MySQL Data

In the following process, we use Navicat for MySQL to export data. You can also use other MySQL export tools.

Export to CSV Format

Select the table and export it.

img

Choose csv format from the type options.

img

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

img

The exported csv file contains the key names in the first line, with each subsequent line representing key-value records corresponding to the header. As shown below:

img

Export to JSON Format

Similarly, we export the selected table to json format:

img

Export

What does the exported data look like?

img

After removing the array, is this the final result?

img

Export MongoDB Data

Start the mongod service first:

img

Do not close this terminal after startup.

Export to CSV Format

Open a new terminal and enter the following command:

mongoexport -db <database> --collection <collection name> --type csv -f <field name1[,field name2]> -o <output file path>

For more detailed parameter descriptions, please refer to the MongoDB documentation.

Note: When exporting to csv format, the field names must be specified; otherwise, an error will occur:

csv mode requires a field list

The exported data looks like:

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.

The exported data looks like:

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 a new environment, please wait patiently for the 2-minute environment initialization process.

Database Import

Click Add Collection to create a collection:

img

After creating, click to access and perform the import operation:

img

Select the CSV or JSON format file we previously exported.

Note

There are two conflict resolution modes: Insert and Upsert.

  • Insert mode will always insert new records during import. The same file cannot contain duplicate _id fields or have _id fields that are the same as existing records in the database. If you want to ensure that existing data is not overwritten, you should use Insert mode.
  • Upsert mode determines whether a record exists. If it exists, it updates the record; otherwise, it inserts a new record. If you want to avoid redundant or duplicate data, you should use Upsert mode. :::

Here we select the Upsert mode:

img

After the import process is completed, you can view the imported data in the database:

img