解析config / database.yml时发生YAML语法错误

时间:2015-07-02 17:20:55

标签: ruby-on-rails ruby yaml

  

请注意,YAML必须始终使用空格缩进。标签   是不允许的。

     

错误:():找到无法启动任何令牌的字符   扫描第17行第13行的下一个标记

     

Ruby版本是:ruby 2.2.1p85(2015-02-26修订版49769)   [x86_64-linux] Rails版本是:Rails 4.2.3

我正在使用Ubuntu 14.04 LTS 64位

完整database.yml档案:

# MySQL.  Versions 5.0+ are recommended.
#
# Install the MYSQL driver
#   gem install mysql2
#
# Ensure the MySQL gem is defined in your Gemfile
#   gem 'mysql2'
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
#
default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: 
  socket: /var/run/mysqld/mysqld.sock

development:
 <<: *default
 database: readit_development

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: readit_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# 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: readit_production
  username: readit
  password: <%= ENV['READIT_DATABASE_PASSWORD'] %>

3 个答案:

答案 0 :(得分:0)

这看起来像是你正在使用的解析器中的错误,或者正如菲利普建议的那样,是一个隐藏的角色。

使用

yaml round-trip database.yml

你没有得到任何解析器错误,输出文件与输入文件相同,除了空行(已删除)和development的映射值,它在1个空格处不一致缩进(但不是YAML错误,因为它只需要在同一映射/序列中进行一致的缩进。)

答案 1 :(得分:0)

在密码字段中给出单个空格。

例如:

db_yml = Rails.root.join('config/database.yml')
db_config = YAML.load(db_yml.read)[Rails.env]

答案 2 :(得分:-1)

了解您的 YAML 是否正确的一种解决方案

http://www.yamllint.com/

粘贴您的 YAML 并单击“开始” - 我们会告诉您它是否有效,并为您提供一个干净的 UTF-8 版本。针对 Ruby 进行了优化。