One-liner to migrate data from MongoDB to MySQL from your shell

moboudra

One-liner to migrate data from MongoDB to MySQL from your shell

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

Also, don’t forget that you can always do this without creating an account on our convert JSON to SQL tool!

Step by step version

Export MongoDB data from your collection as JSON using mongoexport, remember to replace database and data with your database and collection names.

$ mongoexport --db database --collection data > data.json

Call the convert to sql step to convert the JSON to runnable SQL, configure what flavour of SQL you need, the table name and wether you would like us to create the table.

Check out the API reference for the convert endpoint for more advanced options here.

$ curl https://sqlify.io/api/pipelines/sqlify/convert/run \
   -u "{API_KEY}:" \
   -F "file=@data.json" \
   -F "return_output=true" \
   -F "options[to]=sql" \
   -F "options[output][flavour]=mysql" \
   -F "options[output][table_name]=table" \
   -F "options[output][create_table]=true" > data.sql

Then pipe the result into your MySQL server to import the data

$ mysql -u username -p database < data.sql

One-liner

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

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

Happy migrating!