Migrating data from MongoDB to MySQL in one line of bash


Migrating data from MongoDB to MySQL in one line of bash

Today we’re introducing the return_output option, which means you can use the Sqlify API to convert a file in one HTTP call.

First, sign up to Sqlify to get an API key, you can continue once you’ve got your key.

If your data is less than 100 MB, all you need to do is pass the return_output option to the step runner, like this:

$ curl https://api.sqlify.io/v1/pipelines/sqlify/convert-to-sql/run \
     -u "{API_KEY}:" \
     -F "file=@file.json" \
     -F "return_output=true" > result.sql

The curl command will read file.json and if successful, will return 200 OK with the converted SQL data in the HTTP response.

If it’s not successful, it will return non-OK status code and some JSON detailing the problem.

With this, it means that you could do very interesting one liners like this one:

$ mongoexport --db database --collection data && \
  curl https://sqlify.io/api/pipelines/sqlify/convert-to-sql/run \
     -u "{API_KEY}:" \
     -F "file=@-" \
     -F "return_output=true" \
     -F "table_name=table" \
     -F "create_table=true" && \
  mysql -u username -p database

What this does is:

  • Export MongoDB data from your collection as JSON using mongoexport
  • Call the export-sql step to convert to runnable SQL
  • Pipe the result into your MySQL server to import the data

Happy migrating!