mongodb 更换mysql
时间: 2024-01-11 12:22:44 浏览: 92
以下是将MongoDB数据迁移到MySQL的示例代码:
```python
from pymongo import MongoClient
import mysql.connector
# 连接MongoDB数据库
mongo_client = MongoClient('mongodb://localhost:27017')
mongo_db = mongo_client['mydatabase']
mongo_collection = mongo_db['mycollection']
# 连接MySQL数据库
mysql_connection = mysql.connector.connect(
host='localhost',
user='your_username',
password='your_password',
database='mydatabase'
)
mysql_cursor = mysql_connection.cursor()
# 从MongoDB读取数据
mongo_documents = mongo_collection.find()
# 将数据插入到MySQL中
for document in mongo_documents:
field1_value = document['field1']
field2_value = document['field2']
field3_value = document['field3']
mysql_cursor.execute("INSERT INTO mycollection (field1, field2, field3) VALUES (%s, %s, %s)", (field1_value, field2_value, field3_value))
mysql_connection.commit()
```