【问题标题】:Django cannot read data from MySQL database after moving it to another VPS将 MySQL 数据库移至另一个 VPS 后,Django 无法从 MySQL 数据库中读取数据
【发布时间】:2021-02-05 14:05:54
【问题描述】:

我正在写作,因为我正忙于一项让我忙碌了很长时间的任务。基本上我有一个托管在 Pythonanywhere 的 Django 应用程序,我想将其移至 DigitalOcean VPS。

我已经在Digital Ocean上设置了APP,它可以正常工作,加载静态文件,如果我访问新IP就可以看到它。

当我需要将 MySQL 中的数据从 Pythonanywhere 移动到 DigitalOcean 时,我遇到了困难。 Django 没有读取数据,我的应用程序是空的。

这是我所做的:

1. 在 Pythonanywhere 上的数据库上做了一个 mysqldump

2. 使用备份在 DigitalOcean 上的新数据库中创建相同的表和相同的数据

3. 检查表是否正确存在于新 VPS 的 MySQL 中。这是 DigitalOcean 上的表格:

| Tables_in_mysitedb      |

| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| blog_category              |
| blog_post                  |
| blog_post_productos        |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_redirect            |
| django_session             |
| django_site                |
| pages_homepage             |
| productos_product          |
| productos_product_tags     |
| productos_tag              |
| staticpages_about          |
| staticpages_staticpage     |

它们与通过 Django 模型创建的旧名称完全相同。

4.在DigitalOcean上Django的settings.py中添加数据库:

  DATABASES = { 'default': { 
        'ENGINE': 'django.db.backends.mysql', 
        'OPTIONS': {
                'read_default_file': '/etc/mysql/my.cnf',
            },
        } 
    }

...这是 my.cnf(显然我已删除凭据)。

[client]
database = mydb
user = username
password = password
host = localhost
default-character-set = utf8

如果我这样做python manage.py shell,我可以正确检索帖子。

>>> from blog.models import Post
>>> posts = Post.objects.all()
>>> for post in posts:
...     print(post.title)
... 
Post title 1
post title 2
etc.

在我看来一切都已就绪,那么为什么 Django 没有从数据库中看到任何内容而我的应用程序是空的?

如果数据库和表完全相同,这是否足以使其正常工作?我读到了 inspectdb 命令,它应该根据表重新创建模型,但关键是我已经有了这些表的模型,因为它是完全相同的数据。

非常感谢任何帮助,我被卡住了,无法理解如何解决这个问题。

谢谢

【问题讨论】:

    标签: python mysql django


    【解决方案1】:

    我解决了这个问题,添加了答案以帮助其他人,以防他们遇到同样的问题。

    我只需要重新启动 gunicorn:

    sudo systemctl daemon-reload
    sudo systemctl restart gunicorn
    sudo systemctl status gunicorn
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多