讃岐小僧のEngineering×Techメモ

未経験から挑戦中のプログラミングや、趣味の野球や狩猟について、その他、ビジネスやテクノロジーをテーマに様々なことをつぶやく場所です。

mysql8.0.1にアップグレードしたらRailsに怒られた

起こった事

rails sでローカルでアプリを起動しようとしたところ、mysqlのエラーが発生したため対応を行った。

内容を理解していないが、後から振り返るため対応をここにメモしておく。

発生したエラー

/Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.10/lib/mysql2.rb:31:in require': dlopen(/Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle, 9): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib (LoadError) Referenced from: /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle Reason: image not found - /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.10/lib/mysql2/mysql2.bundle from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.10/lib/mysql2.rb:31:in' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:81:in require' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:81:inblock (2 levels) in require' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:76:in each' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:76:inblock in require' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:65:in each' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler/runtime.rb:65:inrequire' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.16.5/lib/bundler.rb:114:in require' from /Users/user_name/projects/chat-space/config/application.rb:5:in' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:88:in require' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:88:inblock in server' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:85:in tap' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:85:inserver' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands/commands_tasks.rb:49:in run_command!' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.7/lib/rails/commands.rb:18:in' from /Users/user_name/projects/chat-space/bin/rails:9:in require' from /Users/user_name/projects/chat-space/bin/rails:9:in' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in load' from /Users/user_name名/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:incall' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in call' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/client.rb:30:inrun' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/bin/spring:49:in <top (required)>' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:inload' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in <top (required)>' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:inrequire' from /Users/user_name/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:68:in require' from /Users/user_name/projects/chat-space/bin/spring:15:in' from bin/rails:3:in load' from bin/rails:3:in

'

次にbundle doctorで原因を調べてみる。

bundle doctor

こういうのが帰ってきた。

The Gemfile's dependencies are satisfied The following gems are missing OS dependencies: * mysql2: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib

解決のために実行したコマンド

bundle exec gem uninstall mysql2 && bundle install

こんなのが出てくる

Select gem to uninstall:
1. mysql2-0.3.18
2. mysql2-0.3.20
3. mysql2-0.3.21
4. mysql2-0.4.5
5. mysql2-0.4.10
6. mysql2-0.5.1
7. All versions

  1. All versionsで実行した。

次にこちらのコマンドを実行

mysql_upgrade -uroot

こんなのが帰ってきた。

Checking if update is needed. Checking server version. Error: Server version (5.6.42) does not match with the version of the server (8.0.12) with which this program was built/distributed. You can use --skip-version-check to skip this check.

こちらも実行してみる

ls -l /usr/local/opt/mysql/lib

total 22408 -rw-r--r-- 1 user_name staff 5418880 11 26 01:16 libmysqlclient.21.dylib -r--r--r-- 1 user_name staff 6038128 6 29 01:42 libmysqlclient.a lrwxr-xr-x 1 user_name staff 23 6 29 01:42 libmysqlclient.dylib -> libmysqlclient.21.dylib -r--r--r-- 1 user_name staff 9760 6 29 01:42 libmysqlservices.a drwxr-xr-x 3 user_name staff 96 11 26 01:16 pkgconfig drwxr-xr-x 91 user_name staff 2912 6 29 01:42 plugin

とりあえず、このコマンド達を流すとエラーは解決した。

参考にしたサイト

Homebrew で MySQL を 5.7.22 から 8.0.11 にバージョンアップしたら Rails が動かなくなったので対処する