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

查看:82
本文介绍了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天全站免登陆