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

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

问题描述

我想从另一个Heroku应用程序访问应用程序的数据库。这是可能的共享数据库?

解决方案

更新



最初,这个答案表明,虽然这可能有一些技巧,但它被强烈阻​​止。这是基于Heroku开发者支持网站的建议。然而,最近Heroku发布了一个专门描述如何实现这一目的的沟通,并在开发者网站上淡化了他们的建议。这部分电子邮件的完整内容包含在下面:


您知道Heroku应用程序可以
共享a通用数据库?例如,您可以将分析函数
放置在面向用户的代码的单独应用程序中。



只需将多个应用程序的DATABASE_URL config var设置为相同即可
值。首先,为您的现有应用程序获取DATABASE_URL:

  $ heroku config | grep DATABASE_URL --app sushi DATABASE_URL => postgres:// lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute1.amazonaws.com/ldfoiusfsf 

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

  $ heroku config:add DATABASE_URL = postgres:/ /lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics 
添加config vars:DATABASE_URL => postgres://lswlm...m/ldfoiusfsf重新启动应用程序...完成,v74。这就是它

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


blockquote>

作为一个参考点,Heroku最初的建议是创建并使用API​​远程访问数据。我个人的观点是,总的来说,对于很多情况来说,这是一个好建议,(即比将多个应用程序连接到同一个数据库更好),尽管我可以看到情况会比其值得更麻烦。

更新



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


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

解决方案

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

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.

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

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