A command line tool to migrate MongoDB collections to MySQL tables dynamically
Project description
mongo-to-mysql
mongo-to-mysql is a high-performance Python command-line utility (CLI) designed to automate the migration of unstructured MongoDB databases to structured MySQL relational databases.
It dynamically infers table schemas from NoSQL document structures, normalizes complex hierarchies (arrays, nested dictionaries) into relational schemas without redundant junction tables, supports schema evolution across batch boundaries, and operates with full transactional reliability.
🚀 Key Features
- Automated Schema Inference & Evolution: Automatically creates tables and columns based on document attributes. If new fields are introduced dynamically during migration, the tool alters the MySQL tables on the fly without interrupting the transaction.
- Recursive Normalization (No Redundant Mappings): Converts embedded hierarchies directly into relational layouts:
- Lists of Scalars (e.g.,
["friend", "colleague"]) are mapped to a dedicated child table. - Nested Dictionaries (One-to-One) (e.g.,
{"city": "Pune"}) are normalized into a sub-table linked directly via a foreign key (e.g.,subtable_parent_id). - Lists of Dictionaries (One-to-Many) are normalized into a sub-table with direct parent foreign key references, completely avoiding redundant
*_mappingjunction tables.
- Lists of Scalars (e.g.,
- BSON Sanitization: Recursively flattens MongoDB-specific data types (
ObjectId,Decimal128,datetime) to Python-native scalars to avoid unwanted schema nesting. - Session-backed Batch Streaming: Streams documents in chunks using PyMongo cursors inside an explicit client session, preventing cursor timeout errors on large datasets.
- Transactional Batch Control: Performs all insertions within batch-controlled database transactions. If a single document parsing or insertion fails in a batch, the entire batch transaction is rolled back.
📦 Installation
Install the package directly from PyPI:
pip install mongo-to-mysql
(Note: Ensure your Python script installation directory is added to your system's PATH to run the command globally).
📖 End-to-End Migration Example
Here is a visual example of how a MongoDB document is transformed into structured MySQL tables.
1. MongoDB Source Document (Collection: companies)
{
"_id": {"$oid": "64b0f92b7c4e2b001c8a1234"},
"name": "TechNova Solutions",
"headquarters": {
"city": "Pune",
"country": "India"
},
"industries": ["Software", "AI"],
"projects": [
{ "title": "Astra", "budget": 12000 },
{ "title": "Horizon", "budget": 45000 }
]
}
2. Resulting MySQL Relational Tables
Table: companies
| companies_id | companies_name |
|---|---|
| 1 | TechNova Solutions |
Table: headquarters (Nested Object -> One-to-One FK)
| headquarters_id | headquarters_city | headquarters_country | headquarters_companies_id (FK) |
|---|---|---|---|
| 1 | Pune | India | 1 |
Table: industries (List of Scalars -> Lookup FK)
| industries_id | industries_companies_id (FK) | industries_industries |
|---|---|---|
| 1 | 1 | Software |
| 2 | 1 | AI |
Table: projects (List of Objects -> One-to-Many FK)
| projects_id | projects_title | projects_budget | projects_companies_id (FK) |
|---|---|---|---|
| 1 | Astra | 12000 | 1 |
| 2 | Horizon | 45000 | 1 |
🛠️ Usage Guide
The tool supports both direct CLI arguments and an interactive prompt mode.
1. Non-Interactive CLI Command
Provide arguments directly to run the migration:
mongo-to-mysql \
--mongo-db sample_mongo_db \
--mysql-db sample_mysql_db \
--username root \
--password rootpwd \
--host localhost \
--batch-size 50
2. Interactive Mode
If arguments are omitted, the tool will guide you with prompts:
mongo-to-mysql
3. Remote Connections & Custom Ports
To connect to remote databases running on custom ports or with MongoDB auth:
mongo-to-mysql \
--mongo-db secure_db \
--mongo-host 192.168.1.50 \
--mongo-port 27017 \
--mongo-user admin \
--mongo-pass adminpwd \
--mysql-db target_db \
--host mysql-server.internal \
--mysql-port 3306 \
--username migrate_user \
--password migratepwd
⚙️ CLI Reference Options
| Argument | Short Flag | Description | Required / Optional |
|---|---|---|---|
--mongo-db |
- | Name of the source MongoDB database | Required (or prompted) |
--mysql-db |
- | Name of the target MySQL database | Required (or prompted) |
--username |
- | MySQL database connection username | Required (or prompted) |
--password |
- | MySQL database connection password | Required (or prompted) |
--host |
- | MySQL database connection host/IP | Required (or prompted) |
--mysql-port |
- | MySQL database connection port (default 3306) |
Optional |
--mongo-host |
- | MongoDB host IP/hostname (default localhost) |
Optional |
--mongo-port |
- | MongoDB port (default 27017) |
Optional |
--mongo-user |
- | MongoDB authentication username | Optional |
--mongo-pass |
- | MongoDB authentication password | Optional |
--batch-size |
- | Batch size for chunked streaming (default 1000) |
Optional |
--help |
-h |
Show help guidelines and options | Optional |
--version |
-v |
Show version number (0.1.0) |
Optional |
🔍 Relational Mapping Mechanics
- Table Creation: Every collection in MongoDB is represented by a table in MySQL named after the collection.
- Column Naming: All columns in MySQL are prepended with
{tablename}_(e.g.,users_namein theuserstable) and declared asVARCHAR(1000) NULLto handle polymorphic schemas cleanly. - Primary Keys: Tables utilize auto-incremented integer keys named
{tablename}_id. - Sub-document Normalization: Nested JSON dicts and list items are normalized into dedicated sub-tables, utilizing direct parent foreign key references (e.g.,
address_users_idinsideaddresstable) rather than redundant junction mapping tables.
📄 License
This project is licensed under the MIT License. See LICENSE for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongo_to_mysql-0.1.1.tar.gz.
File metadata
- Download URL: mongo_to_mysql-0.1.1.tar.gz
- Upload date:
- Size: 14.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
060fbe2832a49703a22d52d6134e6e55b7e3298ff3eb5a25c0bb48f80b1bce85
|
|
| MD5 |
aff9d0f6eef6e2bd9c297dba46ab7163
|
|
| BLAKE2b-256 |
a6724a7946929028c7109662ffb308e8e9c6c2d49d75c4f0839180a659ea87ca
|
File details
Details for the file mongo_to_mysql-0.1.1-py3-none-any.whl.
File metadata
- Download URL: mongo_to_mysql-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d44a9f91a2fc534d6ffe833367d2f64e039184d463ae9d496435dc19b733318
|
|
| MD5 |
36064f592980c92e5f8ae608eb7877f0
|
|
| BLAKE2b-256 |
7363f621434443f8b9b23aaeae17d35eb5b9a59a3600d52ec18992fd73bdef3c
|