Heroku 应用程序数据库重置 [英] Heroku app database resetting

查看:29
本文介绍了Heroku 应用程序数据库重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Heroku 上运行 Python 入门后,我启动了我的第一个应用程序.一切似乎都运行良好,但过了一会儿(可能是几个小时),数据库会重置.我对根本原因的假设是我的 django 应用程序使用的是默认的 django 数据库(我认为是 SQLite)并且 Heroku 默认支持 postgres.我还没有测试过这个,因为将我的应用程序更改为 postgres 似乎需要做很多工作,如果不需要的话,我现在不想这样做.

After running through Getting Started with Python on Heroku, I launched my first app. Everything seems to be working fine, but after a little while (a few hours maybe), the database resets. My hypothesis of the root cause is that my django app is using the default django database (SQLite I think) and Heroku supports postgres by default. I haven't tested this because it seems like quite a lot of work to change my app to postgres and it's something I'd rather not do now if I don't have to.

总而言之,我的问题是,由于我的应用程序使用 SQLite,我的数据库没有保存吗?如果是这样,为什么我的应用程序可以正常工作?如果没有,我应该首先在哪里解决问题?

In summary, my question is, is my database not saving due to my app using SQLite? And if so, why does my app work at all? if not, where is the first place I should look to solve the issue?

推荐答案

正如@DanielRoseman 所说,你不应该在 heroku 上使用 SQLite,因为 SQLite 在内存中运行,并将其数据存储在磁盘上的文件中.让我引用 heroku doku 的话:

As stated by @DanielRoseman you should not use SQLite on heroku, because SQLite runs in memory, and backs up its data store in files on disk. Let me quote from the heroku doku:

SQLite 在内存中运行,并在磁盘上的文件中备份其数据存储.虽然这种策略对开发很有效,但 Heroku 的 Cedar 堆栈有一个临时文件系统.您可以写入它,也可以从中读取,但内容将被定期清除.如果您要在 Heroku 上使用 SQLite,您将至少每 24 小时丢失一次整个数据库.

SQLite runs in memory, and backs up its data store in files on disk. While this strategy works well for development, Heroku’s Cedar stack has an ephemeral filesystem. You can write to it, and you can read from it, but the contents will be cleared periodically. If you were to use SQLite on Heroku, you would lose your entire database at least once every 24 hours.

即使 Heroku 的磁盘持续运行 SQLite,仍然不适合.由于 SQLite 不作为服务运行,因此每个 dyno 将运行一个单独的运行副本.这些副本中的每一个都需要自己的磁盘支持存储.这意味着每个驱动您的应用程序的 dyno 都会有一组不同的数据,因为磁盘不同步.

Even if Heroku’s disks were persistent running SQLite would still not be a good fit. Since SQLite does not run as a service, each dyno would run a separate running copy. Each of these copies need their own disk backed store. This would mean that each dyno powering your app would have a different set of data since the disks are not synchronized.

您可以将应用配置为在 Postgres 上运行,而不是在 Heroku 上使用 SQLite.

Instead of using SQLite on Heroku you can configure your app to run on Postgres.

在 django 中使用 postgres 真的很容易.您只需在设置文件中更改数据库适配器:

It is really easy to use postgres in django. You only have to change the database adapter in your settings file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',                     
        'USER': 'myuser',
        'PASSWORD': 'password',
        'HOST': 'localhost',                      # Empty for localhost through domain sockets or           '127.0.0.1' for localhost through TCP.
        'PORT': '',                      # Set to empty string for default.
    }
}

在他们的文档部分还有一个关于如何在 herku 上设置 django 应用程序的教程:

There is also a tutorial on how to setup a django application on herku in their docu section:

https://devcenter.heroku.com/articles/django-app-configuration

这篇关于Heroku 应用程序数据库重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆