如何正确地设置一个database.yml文件在Rails 4 [英] How to correctly setup a database.yml file in Rails 4

查看:1681
本文介绍了如何正确地设置一个database.yml文件在Rails 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇如何在Rails 4应用程序中正确设置我的 database.yml 文件。

I'm curious how to correctly set up my database.yml file in a Rails 4 application.

这不是我真正看到非常详细的,因为它似乎只是工作时部署到Heroku,但我想了解它现在,并注意到格式已经改变了一点从Rails 4.0到4.1。例如

It's not something I have really looked into in great detail as it all seems to just work when deploying to Heroku, but I want to understand it now and have noticed that the format has changed a little from Rails 4.0 to 4.1. For example

4.0.2

development:
 adapter: mysql2
 encoding: utf8
 database: my_app_development
 pool: 5
 username: root
 password:

test:
 adapter: mysql2
 encoding: utf8
 database: my_app_test
 pool: 5
 username: root
 password:


production:
 adapter: mysql2
 encoding: utf8
 database: ymca_gym_production
 pool: 5
 username: root
 password:

4.1.0

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password:
  socket: /var/run/mysqld/mysqld.sock

development:
  <<: *default
    database: my_app_development


test:
  <<: *default
   database: my_app_test


 # On Heroku and other platform providers, you may have a full connection URL
 # available as an environment variable. For example:
 #
 #   DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase"
 #
 # You can use this database configuration with:
 #
 #   production:
 #      url: <%= ENV['DATABASE_URL'] %>
 #

 production:
   <<: *default
     database: my_app_production
     username: my_app
     password: <%= ENV['MY_APP_DATABASE_PASSWORD'] %>

我的问题是


  1. 我应该在所有环境中设置用户名和密码吗?

  2. 我使用Heroku上的ClearDB作为我的数据库。是否应该在评论建议时使用 url:<%= ENV ['DATABASE_URL']%> 进行生产?

  1. Should I be setting usernames and passwords in ALL environments?
  2. I'm using ClearDB on Heroku as my database. Should I be using url: <%= ENV['DATABASE_URL'] %> for production as the comments suggest?


推荐答案

您发布的第二个database.yml实际上等同于第一个,它只是从开发块复制值。

The second database.yml you posted is actually equivalent to the first, it just copies values from the development block.

回答您的其他问题:

1)我应该在所有环境中设置用户名和密码

1) Should I be setting usernames and passwords in ALL environments

你可以,如果你愿意,或者你可以离开它,因为你有一个块的所有凭据。

you can if you wish, or you can leave it as you have above where it takes the credentials all from one block.

2)如果我使用清晰的数据库与Heroku作为我的数据库,那么我应该取消注释

2)If I'm using clear DB with Heroku as my database then should I be uncommenting

heroku实际上完全忽视这个文件(不应该检查到源控件第一位)。 Heroku有自己的机制来处理数据库,您可以在这里阅读更多: https://devcenter.heroku .com / articles / heroku-postgresql

heroku actually completely disregards this file (which shouldn't be checked into source control in the first place anyway). Heroku has its own mechanism to handle databases which you can read more about here: https://devcenter.heroku.com/articles/heroku-postgresql

基本上,将heroku的数据库和您在此文件中定义的本地数据库完全不同。

Essentially, treat "heroku's databases" and local databases that you define in this file completely different.

这篇关于如何正确地设置一个database.yml文件在Rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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