簡體   English   中英

Ruby on Rails - 無法加載此類文件 — mysql2/2.2/mysql2 (LoadError)

[英]Ruby on Rails - cannot load such file — mysql2/2.2/mysql2 (LoadError)

我在 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>'

這是我的 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'

我徹底搜索,找到了一些答案,但沒有用。

我將我的libmysql.dll文件從Program Files/MySQL Server 5.6粘貼到Ruby/bin但仍然沒有運氣。 我執行了以下命令:

gem uninstall mysql2

gem install mysql2 --platform=ruby

我按照本教程進行操作,但仍然遇到相同的錯誤: 錯誤“...無法加載此類文件 -- mysql2/2.0/mysql2 (LoadError)”。 在帶有 Ruby 2.0.0 的 Windows XP 上

如果有人幫助我,我將不勝感激,以便我可以將其部署到 Heroku。

謝謝。

問題是mysql2 gem 沒有安裝其本機依賴項。 在以前的 Ruby 版本中,這會觸發unable to build native gem extension錯誤:

在此處輸入圖片說明

Ruby 2.2中發生了一些變化,因此您可以在不構建本機擴展的情況下安裝 gem。 然而,這並不意味着問題已解決 - 因此您必須確保安裝具有適當依賴項的 gem。

教程

  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 -> c:/your-ruby-install/bin` 復制libmysql.dll

此安裝與前一個安裝之間的區別在於,這應該提及正在安裝本機 gem 擴展( This could take a while ),這表明 Ruby 正在查看該庫。

這一次,如果它安裝正確,它應該適合你。

——

如果您仍然看到錯誤,則必須卸載系統上任何現有版本的mysql2 gem:

如何安裝mysql2。

如果您收到一個錯誤,你只需要輸入gem uninstall mysql2 ,選擇all ,然后重復上述步驟。

在上面的第 3 步中,我不得不使用一對額外的“-”,並且路徑周圍沒有引號。

gem install mysql2 -- --with-mysql-dir=C:\mysql-connector-dir

我正在使用 Win 7

從 Ruby 2.1.5 更新到 2.2.1 后,我剛剛遇到了同樣的問題。 問題似乎是 Rails 使用的mysql2 gem 作為libmysql的綁定。 顯然它不支持 Ruby 2.2 分支,而只支持 2.0 和 2.1。

我遇到了同樣的問題,所以我改用 RubyInstaller。 我不再有這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM