将 Django DB 从 SQLite 迁移到 MySQL 的最佳方法是什么?

发布于 2024-09-06 04:45:02 字数 266 浏览 2 评论 0原文

我需要将我的数据库从 sqlite 迁移到 mysql,而各种工具/脚本对我来说太多了,无法轻松找到最安全、最优雅的解决方案。

这对我来说似乎很好 http://djangosnippets.org/snippets/14/ 但似乎是 3自从获得令人担忧的更新以来已经有很多年了。

您能推荐一个已知对 Django 1.1.1 可靠的解决方案吗?

I need to migrate my db from sqlite to mysql, and the various tools/scripts out there are too many for me to easily spot the safest and most elegant solution.

This seemed to me nice http://djangosnippets.org/snippets/14/ but appears to be 3 years since getting an update which is worrying..

Can you recommend a solution that is known to be reliable with Django 1.1.1 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆光下的微笑 2024-09-13 04:45:02

执行:

python manage.py dumpdata > datadump.json

接下来,将settings.py更改为mysql数据库。

最后:

python manage.py loaddata datadump.json

Execute:

python manage.py dumpdata > datadump.json

Next, change your settings.py to the mysql database.

Finally:

python manage.py loaddata datadump.json
酒解孤独 2024-09-13 04:45:02

经过一番努力搜索后,我遇到了几个问题,我希望未来寻找答案的人会发现这些问题有用。

我的公式是

  1. python manage.py dumpdata > datadump.json
  2. 将 settings.py 更改为您的 mysql
  3. 确保您可以连接 mysql(权限等)
  4. python manage.py migrate --run-syncdb
  5. 排除 contentype 数据在 shell 中使用此代码段

    python manage.py shell

    从 django.contrib.contenttypes.models 导入 ContentType
    ContentType.objects.all().delete()
    quit()

  6. python manage.py loaddata datadump.json

希望对您有所帮助!

After some hard searching I got several problems that I hope future answer looking people will find useful.

my formula is

  1. python manage.py dumpdata > datadump.json
  2. Change settings.py to your mysql
  3. Make sure you can connect on your mysql (permissions,etc)
  4. python manage.py migrate --run-syncdb
  5. Exclude contentype data with this snippet in shell

    python manage.py shell

    from django.contrib.contenttypes.models import ContentType
    ContentType.objects.all().delete()
    quit()

  6. python manage.py loaddata datadump.json

Hope that will help you!

命硬 2024-09-13 04:45:02

这是避免其他地方描述的 ContentType 问题的更简洁的方法:

./manage.py dumpdata --exclude contenttypes --exclude auth.permission --exclude sessions --indent 2 > dump.json

然后:

./manage.py loaddata dump.json

This is a neater way to avoid the ContentType issues described elsewhere:

./manage.py dumpdata --exclude contenttypes --exclude auth.permission --exclude sessions --indent 2 > dump.json

Then:

./manage.py loaddata dump.json
负佳期 2024-09-13 04:45:02

从 sqlite 迁移到 MySQL 所需的(更完整的)步骤列表,YMMV:

  1. python manage.py dumpdata > datadump.json
  2. 确保您可以连接到您的 mysql(权限等)
  3. 确保您有权限修改 FOREIGN_KEY_CHECKS (为此我必须安装并运行我自己的 mysql 私有实例)
  4. 确保不使用 InnoDB 引擎(使用 MyISAM在每个表中)或者下一步将不起作用(默默地失败)!
  5. 通过此命令放宽验证(这在 InnoDB 中不会生效):
    SET GLOBAL FOREIGN_KEY_CHECKS = 0;
  6. 单独加载 django_site.sql 表(如果使用 contrib.sites)
  7. 将 settings.py 更改为新的 mysql
  8. python manage.py migrate --run-syncdb
  9. 通过修补修复sync​​db错误根据需要使用 Django 应用程序的 /migrations 目录中的代码和数据库表
  10. 使用此代码段排除 contentype 数据(可以将其放在主 urls.py 模块中):
    从 django.contrib.contenttypes.models 导入 ContentType
    ContentType.objects.all().delete()
    quit()
  11. 如果需要编辑json数据,必须先美化它:
    cat datadump.json | cat datadump.json | python -m json.tool > datadump_pretty.json
  12. python manage.py loaddata datadump.json
  13. 修复任何数据截断问题
  14. 将时区数据添加到数据库:
    mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql_tzinfo_to_sql mysql -D mysql -P 1234 -u root -p --protocol=tcp
    mysql -P 1234 -u root -p -e "flushtables" --protocol=tcp
  15. 测试站点是否正常工作,无需提交任何数据
  16. SET GLOBAL FOREIGN_KEY_CHECKS = 1;
  17. 测试其余部分

A (fuller) list of the steps I needed for moving from sqlite to MySQL, YMMV:

  1. python manage.py dumpdata > datadump.json
  2. Make sure you can connect on your mysql (permissions, etc)
  3. Make sure you HAVE PRIVILEGES to modify FOREIGN_KEY_CHECKS (I had to install and run my own private instance of mysql for that)
  4. Make sure InnoDB engine is NOT used (use MyISAM in every table) or the next step won't work (failing silently)!
  5. Relax validation by this command (this won't take effect in InnoDB):
    SET GLOBAL FOREIGN_KEY_CHECKS = 0;
  6. Load django_site.sql table separately (if using contrib.sites)
  7. Change settings.py to your new mysql
  8. python manage.py migrate --run-syncdb
  9. Fix syncdb errors errors by tinkering with the code in /migrations directories of your Django apps and the DB tables as necessary
  10. Exclude contentype data with this snippet (can put it in the main urls.py module):
    from django.contrib.contenttypes.models import ContentType
    ContentType.objects.all().delete()
    quit()
  11. If need editing json data have to prettify it first:
    cat datadump.json | python -m json.tool > datadump_pretty.json
  12. python manage.py loaddata datadump.json
  13. Fix any data truncation issues
  14. Add timezone data to the database:
    mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -D mysql -P 1234 -u root -p --protocol=tcp
    mysql -P 1234 -u root -p -e "flush tables" --protocol=tcp
  15. Test the site is working without submitting any data
  16. SET GLOBAL FOREIGN_KEY_CHECKS = 1;
  17. Test the rest
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文