我在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我遵循了本教程,仍然得到了相同的错误:错误"...cannot加载这样的文件-- mysql2 2/2.0/mysql2 2 (LoadError)“。在Windows上使用Ruby 2.0.0
如果有人帮我,我会非常感激的,这样我就可以把它部署到Heroku。
谢谢。
发布于 2015-03-22 16:50:50
问题是,mysql2 gem没有安装它的本机依赖项。在早期版本的Ruby中,这将触发unable to build native gem extension错误:

Ruby 2.2中发生了一些变化,这样您就可以安装gem,而无需构建本机扩展。但是,这并不意味着问题得到了解决,因此您必须确保安装具有适当依赖关系的gem。
gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:/your-mysql-connector-path"'C:/your-mysql-connector-path -> c:/your/bin`复制->这次安装和上一次安装的不同之处在于,这应该提到本地gem扩展正在安装(This could take a while),这意味着Ruby正在查看库。
这一次,如果它安装正确,它应该为您工作。
--
如果仍然看到错误,则必须在系统上卸载mysql2 gem的任何现有版本:

如果遇到错误,只需键入gem uninstall mysql2,选择all,然后重复上面的步骤。
发布于 2015-04-13 13:15:44
在上面的步骤3中,我不得不在路径周围使用一个额外的'-‘和没有引号。
gem install mysql2 -- --with-mysql-dir=C:\mysql-connector-dir我用的是Win 7
发布于 2015-03-13 12:44:00
在从Ruby2.1.5升级到2.2.1之后,我刚刚遇到了同样的问题。问题似乎在于Rails将其用作绑定到mysql2的gem。显然,它不支持Ruby2.2分支,而只支持2.0和2.1。
https://stackoverflow.com/questions/28925628
复制相似问题