質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

10085閲覧

ruby2.4でrailsでmysqlを使いたい。

defeatist

総合スコア35

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2017/12/07 12:58

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

まず Ruby 2.4 のほうについて。

mysql2 が「インストールはできているが正常にインストールできていない」のを確かめるのは

ruby -r mysql2 -e puts

とでもすれば分かります。
-r mysql2require "mysql2" する,ということです。
-e putsputs を実行する,ということです。したがって,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 アカウントを作る必要があり,住所・電話まで入れさせられるのでやめました)

投稿2017/12/08 08:47

scivola

総合スコア2108

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

defeatist

2017/12/08 14:50

わざわざ同じ環境で試していただきありがとうございます! とりあえず2.3で使えそうなので、このまま2.3で使ってみようと思います。
guest

0

ベストアンサー

調べたところ,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 用のバイナリーを含んだバージョンでなく,インストール時にコンパイルする方法を試してみましょう。

rb

1gem install mysql2 --platform=ruby

とします。
MSYS2 が適切にインストール,セットアップされているという前提です。(Ruby は RubyInstaller for Windows でインストールされたんですよね?)
ただ,私は mysql2 でこれを試したことはないので,これでうまくいくかどうかは全く分かりません。

ところで,「rubyを2.3にダウングレード」を試されたとのことですが,こちらはどのようにうまくいかなかったのでしょうか。

投稿2017/12/07 15:38

scivola

総合スコア2108

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

defeatist

2017/12/08 07:12 編集

ruby 2.3ではrails db:createで以下のログが出ます #<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
scivola

2017/12/08 08:48

これを見ると,Ruby 2.3 のほうは gem は正常にインストールできているぽいですね。 Can't connect to MySQL server on 'localhost' とあるので,データベース接続ができないようです。 `password` が指定されていないようですが,それが原因ではないでしょうか。
defeatist

2017/12/08 14:49

できました! ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問