Ruby on Rails 中文 Wiki
Tutorial Step Two

<— TutorialStepOne | TutorialStepThree (创建一个Controller) —>

配置数据库
通过编辑 config/database.yml 来配置数据库。 以下展示了各种特定适配器的例子。

注意:在database.yml中不能用tab缩进,请确保只使用空格缩进。当然可是用能自动把tab转换为空格的编辑器来进行配置

MySQL

如果使用MySQL,配置文件类似下面这样:

development:
  adapter: mysql
  database: rails_development
  host: localhost
  username: rails_user
  password: [your password here]

test:
  adapter: mysql
  database: rails_test
  host: localhost
  username: rails_user
  password: [your password here]

production:
  adapter: mysql
  database: rails_production
  host: localhost
  username: rails_user
  password: [your password here]

一般在开发时,Rails是运行在development模式的。所以不一定需要production设置,但最好还是一次过把全部都填写完整。

还有要注意一下,rails_production就是将会创建的数据库的实际名字。这个名字只是表示而已,并不是说数据库的名称是_production,Rails就会运行于production模式。

MAC用户:确保是基于MAMP包来安装MySQL的,还应该在上面配置中每个数据库配置上加上下面这么一行:

  socket: /Applications/MAMP/tmp/mysql/mysql.sock

SQLite

如果使用SQLite,配置文件会类似下面这样:

production:
  adapter: sqlite3       # Use sqlite (no 3) for 1/2.x
  dbfile: db/rails-production.db

test:
  adapter: sqlite3       # Use sqlite (no 3) for 1/2.x
  dbfile: db/rails-test.db

development:
  adapter: sqlite3       # Use sqlite (no 3) for 1/2.x
  dbfile: db/rails-development.db

注意要使用到SQLite适配器,应该预先将sqlite.dll文件所在的目录配置到系统的Path下(win32平台下的情况),并且还需要安装适当的gem包(所有平台)

gem install sqlite3-ruby # for SQLite3
or
gem install sqlite-ruby # for an earlier 2.x release

在安装gem包时,Win32 用户应该选择最高版本的“-win“条目, 而Linux用户应该选择最高版本的”-ruby“条目。 (最新的rubygems不需要选择,会自动处理这些。)

PostgreSQL

如果使用PostgreSQL,配置文件会类似下面这样:

production:
  adapter: postgresql
  database: rails_production
  username: postgres
  password:

test:
  adapter: postgresql
  database: rails_test
  username: postgres
  password: 

development:
  adapter: postgresql
  database: rails_development
  username: postgres
  password: 

<— TutorialStepOne | TutorialStepThree (创建一个控制器) —>

本条目被以下条目链接: