Ruby on Rails - 无法加载此类文件 - mysql2/2.2/mysql2 (LoadError)

Ruby on Rails - cannot load such file -- mysql2/2.2/mysql2 (LoadError)(Ruby on Rails - 无法加载此类文件 - mysql2/2.2/mysql2 (LoadError))
本文介绍了Ruby on Rails - 无法加载此类文件 - mysql2/2.2/mysql2 (LoadError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rails 项目中使用 SQLite,但当我听说 Heroku 不支持 SQLite 时,我改用了 MYSQL.

切换数据库后,我启动了 Rails 服务器rails s",但出现以下错误:

 C:Sitessimple_cms>rails 服务器C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2:in `require': 无法加载此类文件 -- mysql2/2.2/mysql2 (LoadError)来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2/mysql2.rb:2:in `'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `r等于'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/mysql2-0.3.18-x64-mingw32/lib/mysql2.rb:31:in `<顶部(必填)>'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `requ愤怒来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:76:in `bloc要求中的 k(2 个级别)'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `each'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:72:in `block 需要'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `each'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler/runtime.rb:61:in `requ愤怒来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/bundler-1.8.4/lib/bundler.rb:134:in `require'来自 C:/Sites/simple_cms/config/application.rb:7:in `'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:78:在`需要'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:78:在`服务器中的块'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:在`tap'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:在`服务器'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:在`run_command!'来自 C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top(必填)>'来自 bin/rails:4:in `require'来自 bin/rails:4:in `<main>'

这是我的 gemfile:

# Bundle edge Rails 代替: gem 'rails', github: 'rails/rails'宝石导轨",4.2.0"# 使用mysql作为Active Record的数据库宝石'mysql2'

我进行了彻底的搜索,找到了一些答案,但没有用.

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

gem 卸载mysql2gem install mysql2 --platform=ruby

我遵循了本教程,但仍然出现相同的错误::

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

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

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

  4. 应该安装 gem 和它需要的文件

  5. 然后你可以从C:/your-mysql-connector-path复制libmysql.dll ->c:/your-ruby-install/bin`

这次安装和上一次的区别在于,这应该提到正在安装本机 gem 扩展(这可能需要一段时间),这表明Ruby 正在查看库.

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

--

如果您仍然看到错误,则必须卸载系统上任何现有版本的 mysql2 gem:

如果出现错误,只需输入gem uninstall mysql2,选择all,然后重复上述步骤.

I was using SQLite for my Rails project but when I heard that Heroku doesn't support SQLite, I switched to MYSQL.

After switching my database, I started my rails server "rails s" but it gives me the following error:

    C:Sitessimple_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>'

This is my gemfile:

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

I searched thoroughly, found some answers but it didn't work.

I pasted my libmysql.dll file from Program Files/MySQL Server 5.6 to Ruby/bin but still no luck. I performed the following commands:

gem uninstall mysql2

gem install mysql2 --platform=ruby

I followed this tutorial and still got the same error: Error "...cannot load such file -- mysql2/2.0/mysql2 (LoadError)". On Windows XP with Ruby 2.0.0

I will highly appreciate it if someone will help me out, so that I can deploy it to Heroku.

Thanks.

解决方案

The problem is that the mysql2 gem is not installing with its native dependencies. In previous versions of Ruby, this would have triggered the unable to build native gem extension error:

Something has changed in Ruby 2.2 so that you can install the gem without it building the native extension. This does not mean the problem is resolved however - and so you have to ensure you install the gem with the appropriate dependencies.

Tutorial:

  1. Download C-Connector from MYSQL (note your version of Ruby -- if you're using 64 bit, download the 64 bit version)

  2. Unzip / Install the MYSQL C-Connector folder onto your HDD (don't have any spaces in the path)

  3. Run the following command: gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'

  4. This should install the gem with the files it needs

  5. Then you can copy libmysql.dll from C:/your-mysql-connector-path -> c:/your-ruby-install/bin`

The difference between this install and the previous one will be that this should mention that the native gem extensions are being installed (This could take a while), which suggests that Ruby is looking at the library.

This time, if it installs correctly, it should work for you.

--

If you're still seeing an error, you'll have to uninstall any existing versions of the mysql2 gem on your system:

If you're getting an error, you just need to type gem uninstall mysql2, select all and then repeat the steps above.

这篇关于Ruby on Rails - 无法加载此类文件 - mysql2/2.2/mysql2 (LoadError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)