Mysql2 0.3.15 TypeError:无法将nil转换为String

时间:2014-05-07 02:06:31

标签: ruby-on-rails mysql2 ruby-on-rails-4.1

我尝试使用mysql2 0.3.15创建新的Rails 4.1项目,并获得以下错误和堆栈跟踪。当我强制mysql2 0.3.14时,没有错误。

$ rake db:migrate
rake aborted!
TypeError: can't convert nil into String
/usr/local/Cellar/Gems/1.9/gems/mysql2-0.3.15/lib/mysql2/client.rb:67:in `connect'
/usr/local/Cellar/Gems/1.9/gems/mysql2-0.3.15/lib/mysql2/client.rb:67:in `initialize'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `new'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/mysql2_adapter.rb:18:in `mysql2_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:416:in `acquire_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:351:in `block in checkout'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:350:in `checkout'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:264:in `connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:541:in `retrieve_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/connection_handling.rb:87:in `connection'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/migration.rb:910:in `initialize'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/migration.rb:807:in `new'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/migration.rb:807:in `up'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/migration.rb:785:in `migrate'
/usr/local/Cellar/Gems/1.9/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'

这是我的database.yml文件

default: &default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: "asdf"
  timeout: 5000

development:
  <<: *default
  database: dev
  socket: /tmp/mysql.sock 

production:
  <<: *default
  database: prod
  socket: /var/lib/mysql/mysql.sock

我的MySQL客户端库在OSX 10.9.2上是5.6.16

更新 如果我在password文件中指定hostportdatabase.yml,则此错误会消失。现在出于某些原因需要这些吗?

1 个答案:

答案 0 :(得分:3)

我发现我已经对GEM_PATHGEM_HOME进行了硬编码,而不是让宝石安装在相应的rbenv文件夹中。由于我安装了ruby 1.9和2.1.1,当我安装新的mysql2 gem时,C扩展是针对2.1.1编译的,当我尝试连接到DB时导致上述类型错误。删除GEM_PATHGEM_HOME并在.rbenv文件夹下重新安装我的宝石可解决此问题。