LoginSignup
10
16

More than 3 years have passed since last update.

RailsのDBをmysqlにするときにでるエラーを解決する魔法のコマンド

Posted at

概要

最近Railsの勉強をしているのですが、データベースをデフォルトのSQLiteからmysqlに変えるのにめちゃくちゃハマりましたので自分用メモです。
ちなみにmysqlのバージョンは0.8.16です。ここが1番ハマるポイントになるのですが。。。

mysql --version

mysql  Ver 8.0.16 for osx10.14 on x86_64 (Homebrew)

rails newコマンドでエラー

rails newコマンドをするとこんなメッセージが帰ってきます。

rails new sample -d mysql

An error occurred while installing mysql2 (0.5.2), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  mysql2
         run  bundle exec spring binstub --all
bundler: command not found: spring
Install missing gem executables with `bundle install`

作られたsampleのGemfileのmysqlのところをみてみると、

Gemfile
gem 'mysql2', '>= 0.4.4', '< 0.6.0'

要するに自分のローカルのmysqlは0.8.16なのに、範囲が間違っているということですね。
書き換えます。

Gemfile
gem 'mysql2', '>= 0.4.4', '<= 0.8.16'

bundle installでエラー

ここまできたら、bundle installで設定を更新します。

bundle install


An error occurred while installing mysql2 (0.5.2), and Bundler cannot
continue.
Make sure that `gem install mysql2 -v '0.5.2' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  mysql2

またエラーが出てきました。ここで詰まってググったらどうやら魔法のコマンドがあるらしい。

それがこちら。

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib"

意味は全くわからないが、どうやらこれでいけるらしい。

もう一度bundle install

bundle install

Bundle complete! 18 Gemfile dependencies, 78 gems now installed.
Bundled gems are installed into `./vendor/bundle`

通った!!!!!

余談

解決するのにめちゃくちゃ時間かかりました。
mysqlのバージョンを下げることも考えたのですが、めんどくさそうなのでやめました。
同じく苦しんでいる人に届けば幸いです。

もしsequel proで繋げる場合は、mysqlのバージョン8からは認証方法が変わっているので変更しないといけません。以下をみるとわかると思います。

参考: https://pqtomblog.com/2018/09/20/connecting_mysql_by_sequelpro/

10
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
16