Ruby on Rails - 无法加载这样的文件 - mysql2 / 2.2 / mysql2(LoadError)

时间:2015-03-08 10:59:24

标签: mysql ruby-on-rails sqlite heroku

我在我的Rails项目中使用SQLite但是当我听说Heroku不支持SQLite时,我切换到了MYSQL。

切换我的数据库后,我启动了我的rails服务器“rails s”,但它给了我以下错误:

    C:\Sites\simple_cms>rails server
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2:in `require'
: cannot load such file -- mysql2/2.2/mysql2 (LoadError)
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2
:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `r
equire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `<
top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `bloc
k (2 levels) in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `bloc
k in require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `each
'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `requ
ire'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler.rb:134:in `require'
        from C:/Sites/simple_cms/config/application.rb:7:in `<top (required)>'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `require'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:78:in `block in server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `tap'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:75:in `server'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks
.rb:39:in `run_command!'
        from C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top
 (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

这是我的宝石文件:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.0'
# Use mysql as the database for Active Record
gem 'mysql2'

我彻底搜查,找到了一些答案,但没有用。

我将libmysql.dll文件从Program Files/MySQL Server 5.6粘贴到Ruby/bin,但仍然没有运气。我执行了以下命令:

gem uninstall mysql2

gem install mysql2 --platform=ruby

我按照本教程仍然遇到了同样的错误:Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

如果有人帮助我,我将非常感激,以便我可以将它部署到Heroku。

感谢。

3 个答案:

答案 0 :(得分:9)

问题是mysql2 gem没有安装其原生依赖项。在以前的Ruby版本中,这会触发unable to build native gem extension错误:

enter image description here

Ruby 2.2中的某些内容已发生变化,因此您可以安装gem,而不会构建原生扩展。这并不意味着问题得到解决 - 因此您必须确保使用适当的依赖项安装gem。

Tutorial

  1. Download C-Connector from MYSQL(请注意您的Ruby版本 - 如果您使用的是64位,请下载64位版本)

  2. 将MYSQL C-Connector文件夹解压缩/安装到硬盘上(路径中没有任何空格)

  3. 运行以下命令:gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'

  4. 应该使用所需的文件安装gem

  5. 然后您可以从libmysql.dll - &gt;复制C:/your-mysql-connector-path C:/你的旁注安装/ bin`

  6. 此安装与前一个安装之间的区别在于此提及正在安装本机gem扩展(This could take a while),这表明Ruby正在查看该库

    这一次,如果安装正确,它应该适合你。

    -

    如果您仍然发现错误,则必须卸载系统中mysql2 gem的所有现有版本:

    enter image description here

    如果您收到错误,只需输入gem unstally mysql2,然后选择all,然后重复上述步骤。

答案 1 :(得分:2)

从Ruby 2.1.5更新到2.2.1后,我遇到了同样的问题。问题似乎是Rails用作mysql2绑定的libmysql gem。显然它不支持Ruby 2.2分支,但只支持2.0和2.1。

答案 2 :(得分:1)

我遇到了同样的问题所以我转而使用RubyInstaller。我不再有问题了。