在 Heroku 中的 2 个应用程序之间共享数据库
我想从另一个 Heroku 应用程序访问应用程序的数据库。在共享数据库中可以吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想从另一个 Heroku 应用程序访问应用程序的数据库。在共享数据库中可以吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
已更新
最初,这个答案指出,尽管可以通过一些技巧实现这一点,但强烈不鼓励这样做。这是基于 Heroku 开发者支持网站上的建议。然而,最近 Heroku 发布了一份通信,专门描述了如何实现这一目标,并淡化了他们在开发者网站上的建议。他们的电子邮件的这一部分的完整文本如下:
作为参考,Heroku 最初的建议是创建并使用 API 来远程访问数据。我个人的观点是,总体而言,对于许多情况来说,这是好建议(即比仅将多个应用程序连接到同一个数据库更好),尽管我可以看到这样做会带来更多麻烦而不是值得的情况。
更新
根据对此答案的评论,值得注意的是,Heroku 保留根据需要更改数据库 URL 的权利。如果发生这种情况,将导致您的辅助连接失败,您需要相应地更新 URL。
UPDATED
Originally, this answer stated that although this was possible with a few tricks, it was strongly discouraged. This was based on advice on the Heroku developer support website. However, recently Heroku issued a communication specifically describing how to achieve this, and have watered down their advice on the developer site. The complete text of this section of their e-mail is included below:
Just as a point of reference, Heroku's original advice was to create and use an API to access data remotely. My personal view is that overall, for many situations this is good advice, (i.e. better than just connecting multiple application to the same DB) although I can see situations where that'd be more trouble than it's worth.
UPDATE
As per comments on this answer, it's worth noting that Heroku do reserve the right to change database URLs as required. If this occurs, it will cause your secondary connections to fail, and you'll need to update the URLs accordingly.
相关问题的最新答案!
Heroku 现在通过其插件框架正式支持更好/优雅的解决方案。他们最新的时事通讯中描述了如何在应用程序之间共享插件。以下是文章中提到的片段:
链接:expanding_the_power_of_add_ons
Late-breaking answer to the pertinent question!
Heroku officially supports a better/graceful solution by the way of their addon framework now. It is described in their latest newsletter on how to share addons between apps. Below are the snippets mentioned in the writeup:
Link: expanding_the_power_of_add_ons
据我所知这是可能的 - 您必须使用数据库查看应用程序的heroku配置变量,然后将想要共享数据库的应用程序上的database_url设置为相同的值。虽然有点偏离轨道,但我不知道它是如何支持的。
编辑为了让我安心,我在 Heroku 上开发了两个应用程序 - 一个带有标题的简单支架“帖子”。
http://evening-spring-734.heroku.com/posts 是大师
< a href="http://electric-galaxy-230.heroku.com/posts" rel="noreferrer">http://electric-galaxy-230.heroku.com/posts - 是奴隶
所以帖子创建于两者都将写入 Evening-spring-734 的数据库 URL。
我所做的就是使用 heroku config 获取 Evening-spring-734 的 DATABASE_URL,然后将相同的值设置到 electric-galaxy-230 的 DATABASE_URL 中。
您最终可能会遇到一些果断的 DB 竞争条件,但这绝对是可能的。
魔法吧?
as far as I know it is possible - you'll have to look at the heroku config variables for your app with the DB and then set the database_url on the app that wants to share the database to the same value. Kinda off track though and as to how supported it is I don't know.
EDIT Just to set my mind at rest I've spun up two apps on Heroku - a simple scaffold 'post' with a title.
http://evening-spring-734.heroku.com/posts is the master
http://electric-galaxy-230.heroku.com/posts - is the slave
So posts created on either will be written to the database URL of evening-spring-734.
All I've done is use heroku config to get the DATABASE_URL of evening-spring-734 and then set the same value into the DATABASE_URL of electric-galaxy-230.
You could end up with some fruity DB race conditions but it is definitely possible to do.
Magic huh?
Amazon RDS 插件非常适合此目的。多个应用程序可以共享同一个RDS实例。
您可以灵活地让它们共享表或使用 active_record.table_name_prefix 避免表名冲突。
The Amazon RDS plugin works well for this. Multiple apps can share the same RDS instance.
You have the flexibility to have them share tables or avoid table name collision using the active_record.table_name_prefix.
请参阅 Heroku 参考:https://devcenter.heroku.com/categories/heroku-postgres
See Heroku reference: https://devcenter.heroku.com/categories/heroku-postgres