Ruby-on-rails 如何在 Rails 中设置 database.yml 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7304576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:55:36  来源:igfitidea点击:

How do I set up the database.yml file in Rails?

ruby-on-railsrubydatabaseyaml

提问by Zack Shapiro

I'm doing this tutorial (http://dmix.ca/2008/09/how-to-scrape-websites-in-ruby-on-rails-using-scrubyt/) and step 4 before I begin is to set up the database.yml file. Not sure what that means. Could someone please explain?

我正在做这个教程(http://dmix.ca/2008/09/how-to-scrape-websites-in-ruby-on-rails-using-scrubyt/),在我开始之前的第 4 步是设置database.yml 文件。不确定那是什么意思。有人可以解释一下吗?

采纳答案by S?awosz

At first I would use http://ruby.railstutorial.org/.

起初我会使用http://ruby.railstutorial.org/

And database.yml is place where you put setup for database your application use - username, password, host - for each database. With new application you dont need to change anything - simply use default sqlite setup.

database.yml 是您为应用程序使用的数据库设置的地方 - 用户名、密码、主机 - 用于每个数据库。使用新应用程序,您无需更改任何内容 - 只需使用默认的 sqlite 设置即可。

回答by marcgg

The database.yml is the file where you set up all the information to connect to the database. It differs depending on the kind of DB you use. You can find more information about this in the Rails Guideor any tutorial explaining how to setup a rails project.

database.yml 是您设置所有信息以连接到数据库的文件。它因您使用的数据库类型而异。您可以在Rails 指南或任何解释如何设置 rails 项目的教程中找到更多信息。

The information in the database.yml file is scoped by environment, allowing you to get a different setting for testing, development or production. It is important that you keep those distinct if you don't want the data you use for development deleted by mistake while running your test suite.

database.yml 文件中的信息受环境限制,允许您获得不同的测试、开发或生产设置。如果您不希望在运行测试套件时错误地删除用于开发的数据,那么保持这些区别很重要。

Regarding source control, you should not commit this file but instead create a template file for other developers (called database.yml.template). When deploying, the convention is to create this database.yml file in /shared/configdirectly on the server.

关于源代码控制,您不应提交此文件,而应为其他开发人员创建一个模板文件(称为database.yml.template)。部署时,约定是/shared/config直接在服务器上创建这个 database.yml 文件。

With SVN: svn propset svn:ignore config "database.yml"

使用 SVN: svn propset svn:ignore config "database.yml"

With Git: Add config/database.ymlto the .gitignore file or with git-extragit ignore config/database.yml

使用 Git:添加config/database.yml到 .gitignore 文件或使用git-extragit ignore config/database.yml



... and now, some examples:

......现在,一些例子:

SQLite

SQLite

adapter: sqlite3
database: db/db_dev_db.sqlite3
pool: 5
timeout: 5000

MYSQL

MYSQL

adapter: mysql
database: my_db
hostname: 127.0.0.1
username: root
password: 
socket: /tmp/mysql.sock
pool: 5
timeout: 5000

MongoDB with MongoID (called mongoid.yml, but basically the same thing)

带有 MongoID 的 MongoDB(称为 mongoid.yml,但基本相同)

host: <%= ENV['MONGOID_HOST'] %>
port: <%= ENV['MONGOID_PORT'] %>
username: <%= ENV['MONGOID_USERNAME'] %>
password: <%= ENV['MONGOID_PASSWORD'] %>
database: <%= ENV['MONGOID_DATABASE'] %>
# slaves:
#   - host: slave1.local
#     port: 27018
#   - host: slave2.local
#     port: 27019

回答by cmpolis

The database.ymlis a file that is created with new rails applications in /configand defines the database configurations that your application will use in different environments. Read this for details.

database.yml是一个使用新 Rails 应用程序创建的文件,它/config定义了您的应用程序将在不同环境中使用的数据库配置。阅读本文了解详情。

Example database.yml:

示例数据库.yml:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: mysql
  encoding: utf8
  database: your_db
  username: root
  password: your_pass
  socket: /tmp/mysql.sock
  host: your_db_ip     #defaults to 127.0.0.1
  port: 3306