Help us understand the problem. What is going on with this article?

MySQL5.7のLibrary not loaded: libmysqlclient.18.dylib (LoadError)を解決

More than 3 years have passed since last update.

動作環境

rails -v: Rails 5.0.1
mysql -v: 5.7.16

症状

rails sを叩くと

/xxxxx/.bundle/ruby/1.9.1/gems/activesupport-3.1.12/lib/active_support/dependencies.rb:240:in `require':
    dlopen(/xxxxx/.bundle/ruby/1.9.1/gems/mysql2-0.3.16/lib/mysql2/mysql2.bundle, 9):
        Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.18.dylib (LoadError)
  Referenced from: /xxxxx/.bundle/ruby/1.9.1/gems/mysql2-0.3.16/lib/mysql2/mysql2.bundle
  Reason: image not found - /xxxxx/.bundle/ruby/1.9.1/gems/mysql2-0.3.16/lib/mysql2/mysql2.bundle

というエラーが出た。

他の記事を見ると、ダウングレードを推奨しているのがあったりしたが、今回はダウングレードせずに続行。

対処

1, mysqlの場所を確認

$ brew info mysql

mysql: stable 5.7.16 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with: mariadb, mysql-cluster, mysql-connector-c, percona-server
/usr/local/Cellar/mysql/5.7.16 (12629 files, 464M) ←ココ!
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/mysql.rb

2, /usr/local/Cellar/mysql/5.7.16/libにて、libmysqlclient.dylibを確認

-r--r--r--   2 User  admin   3781112 12 25 11:56 libmysqlclient.20.dylib
-r--r--r--   1 User  admin   4283752  9 29 02:13 libmysqlclient.a
lrwxr-xr-x   1 User  admin        23  9 29 02:13 libmysqlclient.dylib -> libmysqlclient.20.dylib
-r--r--r--   1 User  admin  33209248  9 29 02:13 libmysqld.a
-r--r--r--   1 User  admin      9608  9 29 02:13 libmysqlservices.a
drwxr-xr-x   3 User  admin       102 12 25 11:56 pkgconfig
drwxr-xr-x  47 User  admin      1598  9 29 02:13 plugin

libmysqlclient.18.dylibが無い事を確認。

3, /usr/local/lib/にてlibmysqlclient.18.dylibを確認。

cd /usr/local/lib/
libmysqlclient.18.dylibがあるか、確認。

4, 以下のコマンドを叩く

sudo ln -s /usr/local/Cellar/mysql/バージョン/lib/libmysqlclient.dylib /usr/local/lib/libmysqlclient.18.dylib

参考資料:
http://afshinm.name/2016/02/05/how-to-fix-library-not-loaded-libmysqlclient-18-dylib-in-mac-os-x/

future
ITを武器とした課題解決型のコンサルティングサービスを提供します
http://future-architect.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away