使用Ruby on Rails连接MySQL时出错

时间:2013-08-03 11:28:01

标签: mysql ruby-on-rails

我刚刚使用Ruby on Rails设置了MySQL。我创建了一个新的rails项目,给出了以下命令

rails new test1 –d mysql

项目已成功创建但我在下面的命令

时收到错误
rake db:create

错误是

C:\Users\HP\test1>rake db:create
rake aborted!
(<unknown>): did not find expected key while parsing a block mapping at line 11
column 1

Tasks: TOP => db:create => db:load_config
(See full trace by running task with --trace)

运行跟踪命令时收到以下消息

C:\Users\HP\test1>rake db:create --trace
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
rake aborted!
(<unknown>): did not find expected key while parsing a block mapping at line 11
column 1
C:/Ruby200/lib/ruby/2.0.0/psych.rb:205:in `parse'
C:/Ruby200/lib/ruby/2.0.0/psych.rb:205:in `parse_stream'
C:/Ruby200/lib/ruby/2.0.0/psych.rb:153:in `parse'
C:/Ruby200/lib/ruby/2.0.0/psych.rb:129:in `load'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-3.1.1/lib/rails/application/configu
ration.rb:106:in `database_configuration'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-3.1.1/lib/active_record/railtie
s/databases.rake:6:in `block (2 levels) in <top (required)>'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:236:in `block i
n execute'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute
'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:175:in `block i
n invoke_with_call_chain'
C:/Ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_
with_call_chain'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:197:in `block i
n invoke_prerequisites'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:195:in `invoke_
prerequisites'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:174:in `block i
n invoke_with_call_chain'
C:/Ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_
with_call_chain'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'

C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:149:in `
invoke_task'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `
block (2 levels) in top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `
each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:106:in `
block in top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:115:in `
run_with_threads'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:100:in `
top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:78:in `b
lock in run'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:165:in `
standard_exception_handling'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/lib/rake/application.rb:75:in `r
un'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.0/bin/rake:33:in `<top (required)>
'
C:/Ruby200/bin/rake:23:in `load'
C:/Ruby200/bin/rake:23:in `<main>'
Tasks: TOP => db:create => db:load_config

不确定要查找错误的文件:

(<unknown>): did not find expected key while parsing a block mapping at line 11
    column 1

我在最后几个小时努力解决这个问题,但找不到解决办法。请指教。感谢。

2 个答案:

答案 0 :(得分:1)

我通过打开普通记事本中的yml文件来提供root密码。所以我相信它无法读取密码。然后我通过notepad ++编辑了yml文件,它运行正常。所以我们似乎需要使用notepad ++或editplus或类似的收费来编辑这些文件。

答案 1 :(得分:0)

看起来您的config\database.yml文件缺少某些数据,它应该如下所示:

development:
  adapter: mysql
  database: my_project_development
  host: localhost
  username: my_user
  password: my_password

你的gemfile中还需要一个mysql gem。