简体   繁体   English

初始化程序或环境代码中的 Rails 数据库连接

[英]Rails database connection in initializer or environment code

I've been trying to get an app hosted on EngineYard, and have run into a problem connecting to an an external database (not hosted on engineyard).我一直在尝试在 EngineYard 上托管一个应用程序,但在连接到外部数据库(未托管在 engineyard 上)时遇到了问题。

I just got a response from engineyard that they re-create the database.yml file on their end, and as a result, the remote database connection details are lost.我刚收到engineyard的回复,他们重新创建了database.yml文件,结果远程数据库连接详细信息丢失了。

Response from EngineYard EngineYard 的回应

Part of our deploy process will write out a database.yml file so that you can connect to the database we configure for you.我们部署过程的一部分将写出一个 database.yml 文件,以便您可以连接到我们为您配置的数据库。 If you SSH into your instance, you can see the one we write for you at /data/mls_data_norm/current/config/database.yml.如果您将 SSH 插入您的实例,您可以在 /data/mls_data_norm/current/config/database.yml 中看到我们为您编写的那个。

If you want to configure this, you will need to do one of two things.如果你想配置它,你需要做两件事之一。 One is to set up a different initializer or environment code that will connect to this other DB.一种是设置一个不同的初始化程序或环境代码来连接到另一个数据库。 The other (and probably preferred) is to use a custom Chef recipe to do this.另一个(可能是首选)是使用自定义厨师食谱来做到这一点。 You can read more about doing that here: http://docs.engineyard.com/custom-chef-recipes.html你可以在这里阅读更多关于这样做的信息: http://docs.engineyard.com/custom-chef-recipes.html

I don't know how long I'll be hosting on engineyard, and I don't like the idea of running different code locally than in production.我不知道我会在engineyard 上托管多久,而且我不喜欢在本地运行与在生产环境中不同的代码的想法。 The whole 'recipes' thing, makes me think that down the line I'll be rewriting this connection.整个“食谱”的东西,让我觉得我将重写这个连接。

I'm sure there is a way to define a database connection outside the database.yml file, but I don't know how, and my searches so far have turned up nothing.我确信有一种方法可以在 database.yml 文件之外定义数据库连接,但我不知道如何,而且到目前为止我的搜索一无所获。

The sole purpose of database.yml is to keep connection parameters, and because EngineYard does some silly stuff, does not mean that you need to move your connection parameters to some place else. database.yml 的唯一目的是保留连接参数,因为 EngineYard 做了一些愚蠢的事情,并不意味着您需要将连接参数移动到其他地方。

The best solution would be to take care of your deployment yourself.最好的解决方案是自己处理您的部署。 Spend a couple of hours reading capistrano documentation and you would be ready to go.花几个小时阅读capistrano文档,你就会准备好 go。 It is simple, elegant and much more powerful.它简单、优雅且功能更强大。 You would soon be doing a lot more deployment related stuff using capistrano than what your host provides.你很快就会使用 capistrano 做比你的主机提供的更多的部署相关的东西。

https://github.com/capistrano/capistrano/wiki https://github.com/capistrano/capistrano/wiki

Other options include running a script post deployment that again overwrites the database.yml and does a server restart.其他选项包括运行脚本部署后再次覆盖 database.yml 并重新启动服务器。

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

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