How to Migrate MongoDB Database to MySQL?

How to Migrate from MongoDB to MySQL database

MongoDB is amongst the most preferred databases for developers today. It is easy to use and works magically on unstructured data. Several data objects are stored as separate documents in the collection, unlike the traditional relational databases. However, with all the pros, there are a few cons too.

MongoDB does not support well-defined relationships, and you may end up having a lot of duplicate data. Duplicate data could also result in data becoming corrupted. Hence, to avoid complications, people prefer to migrate from MongoDB to MYSQL.

Experts at DEV IT have curated this article to help you migrate from MongoDB to the MYSQL database.

While there are multiple ways to archive the MongoDB to MYSQL migration, one of the simplest ways is using Studio 3T.

You may download Studio 3T from here

Prerequisites:
Robomongo Studio 3T
MongoDB Credential
MySQL Credential

Follow the steps mentioned below to migrate from MongoDB to the MYSQL database:

  • Open Robomongo Studio 3T
  • Open Connection (File-> Connect). The connection dialogue box will open, as shown below.
  • Click on New Connection. The following pop-up shall show.
New connection
  • Fill in the Connection Name, Connection Type, Server, and Port, and then double-check your connection by clicking on the Test Connection button.
  • Save your Connection.
  • Click on the Connect button, and your MongoDB connection will be established.

You shall now see Robomongo Studio 3T Dashboard, as shown below. 

  • Create a new Database or New Collection.

You may see that I have already created a new collection in the existing database.

See the county collection (Table) and already inserted documents below.

Studio 3T collection table
Sample Countries Document 
{
		"_id" : ObjectId("5e4e5d1cd4ceeb16b0bb1ba3"),
		"status" : 1,
		"region_id" : ObjectId("5e4e40a0f728f73e0848a5a5"),
		"translations" : [
			{
				"_id" : ObjectId("5e4e5d1cd4ceeb16b0bb1ba6"),
				"language" : "English",
				"language_id" : ObjectId("5d43de669e156d1334c064ff"),
				"name" : "China"
			},
			{
				"_id" : ObjectId("5e4e5d1cd4ceeb16b0bb1ba5"),
				"language" : "Spanish",
				"language_id" : ObjectId("5e294ffd0b5c94593c70ba99"),
				"name" : ""
			},
			{
				"_id" : ObjectId("5e4e5d1cd4ceeb16b0bb1ba4"),
				"language" : "Portuguese",
				"language_id" : ObjectId("5e2ab380e722552de823eab1"),
				"name" : ""
			}
		],
		"country_code" : "CH",
		"createdAt" : ISODate("2020-02-20T10:19:08.980Z"),
		"updatedAt" : ISODate("2020-02-20T10:19:08.980Z"),
		"__v" : 0
}
  • Now this (country) collection will convert to MySQL with the help of Studio 3T.
  • Select SQL Migration
  • Select MongoDB – SQL Migration
Select MongoDB

You will see an ‘open Migration to SQL Tab’ option as shown below.

open Migration to SQL Tab
  • Select the SQL target connection/directory. You will see two options: Directory or Server. Select the Server option.
  • Then click on the “click here to connect to a server” link.
  • Open SQL Connection Manager
  • To create a new MySQL connection, click on New Connection.
  • Enter MySQL Host, Username, Password, Port, Database.
  • Click on the OK button, and a connection has been established.
SQL Connection manager

The MySQL server will be connected, and the following screen will show.

All collections that you wish to migrate into MYSQL will appear here.

  • Click all the checkboxes and then click on the Mapping tab.

All the mapping-related details will show here, along with the MySQL table.

MYSQL table

See that we have one document with a nested object.

The SQL will create three tables, namely countries, countries_translations, countries_translations_object.

  • Now click on the run migration button. And your migration process will create several MySQL tables.
  • When it’s run successfully, you may open the MySQL database and see your MongoDB countries collections migrated to the MySQL database.

Here is when your migration of MongoDB to MySQL database will complete. I hope you are now transparent with the migration process. In case of any queries, do not hesitate to get in touch with an expert.