简体   繁体   English

Flask / Heroku ClearDB无法更新

[英]Flask/Heroku ClearDB not updating

I've deployed my Python app on Heroku and have connected it to a clearDB database. 我已经在Heroku上部署了我的Python应用程序,并将其连接到clearDB数据库。 This works perfectly when run locally, however when running on Heroku my INSERT statements don't seem to be committing properly (sometimes they do, sometimes they don't). 这在本地运行时效果很好,但是在Heroku上运行时,我的INSERT语句似乎未正确提交(有时可以,有时不能)。 It's running through gunicorn. 它正在穿过金枪鱼。

This is my MySQL setup: 这是我的MySQL设置:

APP_ROOT = os.path.join(os.path.dirname(__file__), '..')
dotenv_path = os.path.join(APP_ROOT, '.env')
load_dotenv(dotenv_path)

db_user = os.getenv('DB_USER2')
db_pass = os.getenv('DB_PASSWORD2')
db_database = os.getenv('DB_DATABASE2')
db_host = os.getenv('DB_HOST2')

app.config['MYSQL_DATABASE_USER'] = db_user
app.config['MYSQL_DATABASE_PASSWORD'] = db_pass
app.config['MYSQL_DATABASE_DB'] = db_database
app.config['MYSQL_DATABASE_HOST'] = db_host
mysql.init_app(app)

These variables are coming from a .env file. 这些变量来自.env文件。 But they are also added as environmental variables on Heroku. 但是它们也作为环境变量添加到Heroku中。 So connection to the server is same both locally and on Heroku. 因此,本地和Heroku上与服务器的连接都是相同的。

It's completely random whether it works or not. 是否有效完全是随机的。 Fetch statements work perfectly fine, but inserting or updating seems to cause me a lot of issues. 提取语句工作得很好,但是插入或更新似乎给我带来了很多问题。 Again, this works perfectly with no issues locally, but doesn't on Heroku. 同样,这在本地没有问题的情况下也可以完美运行,但在Heroku上则没有。 (When run locally, it seems to take a bit of time to save the data, which is fine. But it doesn't take much time at all on Heroku so I'm assuming it's somehow skipping the commit). (在本地运行时,保存数据似乎需要一点时间,这很好。但是在Heroku上根本不需要花费很多时间,因此我假设它以某种方式跳过了提交)。

There are no errors in the logs either. 日志中也没有错误。 It just seems to bypass the statement sometimes and not commit. 似乎只是有时绕过该语句而不提交。 Doing a hard refresh sometimes brings up the data, but then it disappears again. 进行硬刷新有时会弹出数据,但是随后又消失了。 Most of the times the data isn't saved at all. 大多数情况下,根本不保存数据。

I have one cursor open throughout the app and I'm using conn.ping(True) before every SQL statement, and conn.commit() after fetching the data. 我在整个应用程序中都打开了一个游标,并且在每个SQL语句之前都使用conn.ping(True) ,在获取数据后使用conn.commit()

Any help would be appreciated, thank you! 任何帮助,将不胜感激,谢谢!

Heroku uses two masters. Heroku使用两个大师。 so sometimes your credential are available and sometimes not, in your procfile, you must have 因此有时您的凭据可用,有时则不可用,在procfile中,您必须具有

web: gunicorn --workers=1 app:app 网址:gunicorn --workers = 1 app:app

workers=1 means your data will be on one master, app=app is just my entry point, yours can be what you have. worker = 1意味着您的数据将集中在一个主数据库上,app = app只是我的切入点,您的可以是您拥有的。

Below is from Heroku Dual Masters, One Database Connection URL The ClearDB Heroku Add-On provisions your database on two "multi-master" database servers in two different Amazon EC2 regions to ensure absolute data availability. 以下是来自Heroku Dual Masters的一个数据库连接URL ClearDB Heroku Add-On在两个不同的Amazon EC2区域中的两个“多主服务器”数据库服务器上配置了您的数据库,以确保绝对的数据可用性。 In our 1.0 add-on, this meant that you received two database connections. 在我们的1.0附加组件中,这意味着您收到了两个数据库连接。 Now, with our custom SQL high availability routing technology, you get a single connection that automatically fails over to the secondary endpoint in the event of a failure, and will switch back to the primary master once service has been restored and the databases have re-synchronized. 现在,借助我们的自定义SQL高可用性路由技术,您将获得一个连接,该连接在发生故障时会自动故障转移到辅助端点,并在恢复服务并重新建立数据库后将切换回主要主机。同步。

The primary master node is located right next to Heroku, in Amazon's EC2 "US-East" region. 主主节点位于Amazon EC2“美国东部”区域的Heroku旁边。 The secondary master node is located in Amazon's EC2 "US-West" region for high availability purposes. 辅助主节点位于Amazon的EC2“美国西部”区域中,以实现高可用性。

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

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