我尝试使用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
文件中指定host
,port
和database.yml
,则此错误会消失。现在出于某些原因需要这些吗?
答案 0 :(得分:3)
我发现我已经对GEM_PATH
和GEM_HOME
进行了硬编码,而不是让宝石安装在相应的rbenv
文件夹中。由于我安装了ruby 1.9和2.1.1,当我安装新的mysql2
gem时,C扩展是针对2.1.1编译的,当我尝试连接到DB时导致上述类型错误。删除GEM_PATH
和GEM_HOME
并在.rbenv
文件夹下重新安装我的宝石可解决此问题。