【问题标题】:Mysql2 0.3.15 TypeError: can't convert nil into StringMysql2 0.3.15 TypeError:无法将 nil 转换为字符串
【发布时间】:2014-05-07 02:06:30
【问题描述】:

我正在尝试使用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

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

【问题讨论】:

  • 通常表示驱动程序期望的某处缺少或无效的字符串变量。问题可能不在 database.yml 中,而是在其他驱动程序或环境配置文件之一(即您设置 mySQL 的位置)。不是同一个问题,但相关github.com/datamapper/dm-core/issues/183
  • 有什么理由让它可以与 mysql2 0.3.14 一起使用吗?我查看了标签的差异,但没有什么真正突出github.com/brianmario/mysql2/compare/0.3.14...0.3.15
  • rails 4.1.1的错误仍然存​​在
  • 安装 gem 时,您是否给它提供了数据库的位置,并且您是否验证了新安装的版本是否正确?我想知道这是否真的是一个套接字问题 - 您是否验证过 /tmp/mysql.sock 文件存在?如果您指定的主机名不是 localhost(甚至是 127.0.0.1),它将尝试以 TCP 模式连接,而不是使用本地套接字。设置主机可能会覆盖对套接字的需求并解决您的问题。注意:如果正确拾取套接字,则不会使用端口号
  • 也不知道为什么你的密码有引号 - 没有它们也能工作吗?如果这些都不起作用并且您仍然有兴趣找到问题,您可以在更新中提供新版本的 database.yml 吗?

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


【解决方案1】:

我发现我已经硬编码了我的 GEM_PATHGEM_HOME,而不是让 gem 安装在适当的 rbenv 文件夹中。由于我同时安装了 ruby​​ 1.9 和 2.1.1,所以当我安装新的 mysql2 gem 时,C 扩展是针对 2.1.1 编译的,并在我尝试连接到数据库时导致上述类型错误。删除 GEM_PATHGEM_HOME 并在 .rbenv 文件夹下重新安装我的 gem 解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多