在 Heroku 中的 2 个应用程序之间共享数据库 [英] Share database between 2 apps in Heroku

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

问题描述

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

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

推荐答案

UPDATED

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

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:

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

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.

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

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:5FSLVUSLLT123@ec2-123-456-78-90.compute1.amazonaws.com/ldfoiusfsf

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

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

$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/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.

作为参考,Heroku 最初的建议是创建和使用 API 来远程访问数据.我个人的观点是,总的来说,在许多情况下,这是好的建议,(即比仅仅将多个应用程序连接到同一个数据库更好),尽管我可以看到这样的情况会带来更多的麻烦而不是值得.

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.

更新

根据对此答案的评论,值得注意的是,Heroku 确实保留根据需要更改数据库 URL 的权利.如果发生这种情况,将导致您的辅助连接失败,您需要相应地更新网址.

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 中的 2 个应用程序之间共享数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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