ruby2.4でrailsでmysqlを使いたい。
解決済
回答 2
投稿
- 評価
- クリップ 0
- VIEW 7,937
railsでmysqlを使おうとしたらエラーが出ました。
発生している問題・エラーメッセージ
>rails db:create
LoadError: cannot load such file -- mysql2/2.4/mysql2
.../config/application.rb:7:in `<top (required)>'
.../Rakefile:4:in `require_relative'
.../Rakefile:4:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
コマンド
>gem install mysql2
======================================================================================================
You've installed the binary version of mysql2.
It was built using MySQL Connector/C version 6.1.11.
It's recommended to use the exact same version to avoid potential issues.
At the time of building this gem, the necessary DLL files were retrieved from:
http://cdn.mysql.com/Downloads/Connector-C/mysql-connector-c-6.1.11-win32.zip
This gem *includes* vendor/libmysql.dll with redistribution notice in vendor/README.
======================================================================================================
Successfully installed mysql2-0.4.10-x64-mingw32
Parsing documentation for mysql2-0.4.10-x64-mingw32
Installing ri documentation for mysql2-0.4.10-x64-mingw32
Done installing documentation for mysql2 after 0 seconds
1 gem installed
ソース
database.yml
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
development:
adapter: mysql2
encoding: utf8
database: rails_tutorial
pool: 5
username: root
password:
host: localhost
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Gemfile
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use mysql2 as the database for Active Record
gem 'mysql2'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 2.13'
gem 'selenium-webdriver'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
試したこと
rubyを2.3にダウングレード
補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Windows [Version 10.0.16299.98]
ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]
Rails 5.1.4
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
調べたところ,mysql2-0.4.10-x64-mingw32 は Ruby 2.4 用のバイナリーを持っていないようです。
mysql2-0.4.10-x64-mingw32 がインストールされたディレクトリーの lib/mysql2 というディレクトリーを見てみてください。
gem のインストールディレクトリーは Ruby のインストールディレクトリーの中の lib/ruby/gems/2.4.0/gems の下にあるかと思います。(いま Windows マシンが手元にないので記憶で書いています)
2.0, 2.1, 2.2, 2.3 というディレクトリーがありますが,これが各 Ruby バージョン用の mysql2.so というファイルを格納するところです。Ruby 2.3 用までしか無いんですね。
なので,gem をいったんアンインストールしてください。
Windows 用のバイナリーを含んだバージョンでなく,インストール時にコンパイルする方法を試してみましょう。
gem install mysql2 --platform=ruby
とします。
MSYS2 が適切にインストール,セットアップされているという前提です。(Ruby は RubyInstaller for Windows でインストールされたんですよね?)
ただ,私は mysql2 でこれを試したことはないので,これでうまくいくかどうかは全く分かりません。
ところで,「rubyを2.3にダウングレード」を試されたとのことですが,こちらはどのようにうまくいかなかったのでしょうか。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
+1
まず Ruby 2.4 のほうについて。
mysql2 が「インストールはできているが正常にインストールできていない」のを確かめるのは
ruby -r mysql2 -e puts
とでもすれば分かります。
-r mysql2
は require "mysql2"
する,ということです。
-e puts
は puts
を実行する,ということです。したがって,puts
じゃなくても exit
でも p
でも何でも OK です。
Windows 環境で,
gem install --platform=ruby
を試してみました。Alberta_Fagni さんと同じ
ruby 2.4.2p198 (2017-09-14 revision 59899) [x64-mingw32]
で。すると,
ERROR: While executing gem ... (Gem::UnsatisfiableDependencyError)
Unable to resolve dependency: No match for 'psych (= 3.0.1)' on this platform. Found: x64-mingw32
が出てダメでした。
Windows環境でのmysql2 gemのインストール - Qiita の手順ではどうでしょうか。
(私もやってみようとしたのですが,Connector/C 6.1.11 のダウンロードに Oracle アカウントを作る必要があり,住所・電話まで入れさせられるのでやめました)
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/12/08 16:11 編集
#<Mysql2::Error: Can't connect to MySQL server on 'localhost' (10061)>
Couldn't create database for {"adapter"=>"mysql2", "encoding"=>"utf8", "database"=>"rails_tutorial", "pool"=>5, "username"=>"root", "password"=>nil, "host"=>"localhost"}, {:charset=>"utf8"}
(If you set the charset manually, make sure you have a matching collation)
Created database 'rails_tutorial'
Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
Couldn't create database for {"adapter"=>"sqlite3", "pool"=>5, "timeout"=>5000, "database"=>"db/test.sqlite3"}
rails aborted!
Gem::LoadError: Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
Gem::LoadError: sqlite3 is not part of the bundle. Add it to your Gemfile.
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:create
2017/12/08 17:48
Can't connect to MySQL server on 'localhost'
とあるので,データベース接続ができないようです。
`password` が指定されていないようですが,それが原因ではないでしょうか。
2017/12/08 23:49
ありがとうございます。