This Python script allows you to convert data from a MySQL database to a MongoDB database.
-
Clone the project repository:
git clone https://github.com/Laviru-Dilshan/mysql-to-mongodb-python.git cd mysql-to-mongodb-python
-
Install the required Python packages:
pip install -r requirements.txt
-
Create a
.env
file in the project directory and add your environment variables:SQL_HOST=your_sql_host SQL_USER=your_sql_user SQL_PASSWORD=your_sql_password SQL_DATABASE=your_sql_database MONGO_URI=your_mongo_uri MONGO_DATABASE=your_mongo_database
(important: after add mongo uri add '/', example - "www.mongodb.com/")
Using This You Can Convert Your Full MYSQL Database To MongoDB
-
Open
mysql_to_mongo.py
and add your sql config ,mongo uri and call functions:# Create MYSQL Config sql_config = { "host": os.getenv('SQL_HOST'), "user": os.getenv('SQL_USER'), "password": os.getenv('SQL_PASSWORD'), "database": os.getenv('SQL_DATABASE'), } #add mondo db uri mongo_uri = os.getenv('MONGO_URI') # call functions converter = MySQLToMongoConverter(sql_config, mongo_uri) converter.convert()
-
Run the script:
python mysql_to_mongo.py
It retrieves data from a specified table in MySQL and inserts it into a specified collection in MongoDB, based on a mapping of fields between the two databases.
-
Open
table_to_collection.py
and add your data mapping and collection name:# Define your field mappings for each table province_mapping = { "id": 0, "name_en": 1, "name_si": 2, "name_ta": 3, } # Example usage converter = MySQLToMongoConverter( { "host": os.getenv('SQL_HOST'), "user": os.getenv('SQL_USER'), "password": os.getenv('SQL_PASSWORD'), "database": os.getenv('SQL_DATABASE'), }, os.getenv('MONGO_URI'), os.getenv('MONGO_DATABASE'), 'provinces', province_mapping ) converter.convert()
-
Run the script:
python table_to_collection.py
All rights reserved by Laviru Dilshan Jr. 2024
Connect with me:
- GitHub: https://github.com/Laviru-Dilshan
- Twitter: https://x.com/laviru_dilshan
- LinkedIn: https://www.linkedin.com/in/laviru-dilshan
- Facebook: https://www.facebook.com/LaviruD
- Instagram: https://www.instagram.com/lavirudilshan