简体   繁体   English

我可以将完整的MySQL DB从基于PHP的站点移动到Django应用程序吗?

[英]Can I move complete MySQL DB from PHP-based site to a Django application?

We have a php based site with MySQL database. 我们有一个基于PHP的站点与MySQL数据库。 We have lots of data there users, orders, invoices and etc. Now we wan to redesign our site application using Django and MySQL or even MongoDB. 我们有很多用户,订单,发票等数据。现在我们想用Django和MySQL甚至MongoDB重新设计我们的网站应用程序。

It's really important for us to move all tables exactly as they used to be with no loss. 对我们来说,完全按照以前的方式移动所有表格并且没有损失是非常重要的。 Is there any way to do this? 有没有办法做到这一点? Can we define new models in Django and redirect them to existing data tables with same names and then import previous database? 我们可以在Django中定义新模型并将它们重定向到具有相同名称的现有数据表,然后导入以前的数据库吗?

If there's really a way to do it, what is it? 如果真的有办法做到这一点,它是什么? I appreciate it if anyone knows a practical way. 如果有人知道一种实用的方法我很感激。

Yikes! 哎呀! Migrations are hard. 迁移很难。

First off, make a backup of your database that you can test with (this is probably a given, but it needs to be stated.) You can in fact use mysqldump to dump straight to a new MySQL database . 首先, 备份您可以测试的数据库 (这可能是给定的,但需要说明。)实际上,您可以使用mysqldump直接转储到新的MySQL数据库

Most of what will get you started is in Django's documentation for working with "legacy databases" . 大多数可以帮助您入门的是Django用于处理“遗留数据库”的文档

Roughly speaking, set up a Django application with your database connection parameters and the name of the database. 粗略地说,使用数据库连接参数和数据库名称设置Django应用程序。 You'll need to edit the DATABASES setting and assign values to these keys: 您需要编辑DATABASES设置并为这些键分配值:

NAME
ENGINE
USER
PASSWORD
HOST
PORT

To get started, there is a built in utility called inspectdb that can generate models based on your database. 首先,有一个名为inspectdb的内置实用程序,可以根据您的数据库生成模型。

python manage.py inspectdb

Keep in mind that this is only a first pass, and you'll likely have to fix some of it by hand. 请记住,这只是第一次通过,您可能需要手动修复其中一些。 Once you get the output cleaned up, save it as models.py and add it to your INSTALLED_APPS setting. 清除输出后,将其另存为models.py并将其添加到INSTALLED_APPS设置中。 You'll also need to set managed=True in each of your model's internal class Meta . 您还需要在每个模型的内部类Meta设置managed=True

After all this is done, run 完成所有这些后,运行

python manage.py migrate

You will probably have to tweak this over and over to get it right. 你可能不得不一遍又一遍地调整它以使其正确。 I highly recommend having backups and a good (maybe automated) setup to keep testing and tweaking to get it right. 我强烈建议备份和良好(可能是自动化)设置,以保持测试和调整,以使其正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM