在 Heroku 中的 2 个应用程序之间共享数据库

发布于 2024-11-07 04:37:59 字数 47 浏览 1 评论 0 原文

我想从另一个 Heroku 应用程序访问应用程序的数据库。在共享数据库中可以吗?

I want to access the database of an app from another Heroku app. Is that possible in the shared database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安人多梦 2024-11-14 04:37:59

已更新

最初,这个答案指出,尽管可以通过一些技巧实现这一点,但强烈不鼓励这样做。这是基于 Heroku 开发者支持网站上的建议。然而,最近 Heroku 发布了一份通信,专门描述了如何实现这一目标,并淡化了他们在开发者网站上的建议。他们的电子邮件的这一部分的完整文本如下:

您知道 Heroku 应用程序可以
共享一个公共数据库?例如,您可以将分析功能
在与面向用户的代码分开的应用程序中。

只需将多个应用程序的 DATABASE_URL 配置变量设置为相同
价值。首先,获取现有应用程序的 DATABASE_URL:

$ heroku 配置 | grep DATABASE_URL --app 寿司 DATABASE_URL => postgres://lswlmfdsfos:[电子邮件受保护]/ldfoiusfsf

然后,将新应用的 DATABASE_URL 设置为此值:

$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:[电子邮件受保护]/ldfoiusfsf --app寿司分析
添加配置变量:DATABASE_URL => postgres://lswlm...m/ldfoiusfsf 正在重新启动应用程序...完成,v74。就是这样

—现在两个应用程序将共享一个数据库。

作为参考,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:

Did you know that Heroku apps can
share a common database? For example, you can put analytics functions
in a separate application from your user-facing code.

Simply set the DATABASE_URL config var for several apps to the same
value. First, get the DATABASE_URL for your existing app:

$ heroku config | grep DATABASE_URL  --app sushi DATABASE_URL => postgres://lswlmfdsfos:[email protected]/ldfoiusfsf

Then, set the DATABASE_URL for new apps to this value:

$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:[email protected]/ldfoiusfsf --app sushi-analytics
Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf Restarting app... done, v74. That's it

— now both apps will share one database.

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.

是伱的 2024-11-14 04:37:59

相关问题的最新答案!

Heroku 现在通过其插件框架正式支持更好/优雅的解决方案。他们最新的时事通讯中描述了如何在应用程序之间共享插件。以下是文章中提到的片段:

$ heroku addons:attach my-sushi-db -a my-sushi-reporting --as MAIN_SUSHI_DB    
  Adding MAIN_SUSHI_DB to my-sushi-reporting... done
  Setting MAIN_SUSHI_DB vars and restarting my-sushi-app-reporting... done, v3

链接: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:

$ heroku addons:attach my-sushi-db -a my-sushi-reporting --as MAIN_SUSHI_DB    
  Adding MAIN_SUSHI_DB to my-sushi-reporting... done
  Setting MAIN_SUSHI_DB vars and restarting my-sushi-app-reporting... done, v3

Link: expanding_the_power_of_add_ons

雨落□心尘 2024-11-14 04:37:59

据我所知这是可能的 - 您必须使用数据库查看应用程序的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?

┈┾☆殇 2024-11-14 04:37:59

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.

情话墙 2024-11-14 04:37:59

请参阅 Heroku 参考:https://devcenter.heroku.com/categories/heroku-postgres

问:多个 Heroku 应用程序可以连接到单个数据库吗?

是的。您可以配置在 Heroku 上运行的多个应用程序进行连接
到单个数据库,前提是您拥有数据库凭据。
只需覆盖您希望连接的应用程序的 DATABASE_URL
heroku config:add DATABASE_URL=... 命令。

See Heroku reference: https://devcenter.heroku.com/categories/heroku-postgres

Q: Can multiple Heroku Applications Connect to a single database?

Yes. You can configure multiple applications running on Heroku to connect
to a single database, provided that you have the databases credentials.
Simply override the DATABASE_URL of the apps that you wish to connect using
the heroku config:add DATABASE_URL=... command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文