'MySQL2 Gem is failing following a MacBooKpro OS upgrade

OS: MacOS Monterey-version 12.3
Rails 7.0.2
Ruby 3.1.0
MySQL gem: mysql2 (0.5.3) (from Gemfile.lock)

My app Rails development server was running great, until Apple said I should update to Monterey version 12.3, which included an xcode update.

I next tried to run the db migrate:

rake db:migrate

To which I received the following message:

rake aborted!
LoadError: dlopen(/Users/joseph-mac/.asdf/installs/ruby/3.1.0/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle, 0x0009): Library not loaded: libssl.1.1.dylib
  Referenced from: /Users/joseph-mac/.asdf/installs/ruby/3.1.0/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle
  Reason: tried: 'libssl.1.1.dylib' (no such file), '/usr/local/lib/libssl.1.1.dylib' (no such file), '/usr/lib/libssl.1.1.dylib' (no such file), '/Users/joseph-mac/ProxLearn/Development/PLIConnectV2/repositories/pli2_tw/libssl.1.1.dylib' (no such file), '/usr/local/lib/libssl.1.1.dylib' (no such file), '/usr/lib/libssl.1.1.dylib' (no such file) - /Users/joseph-mac/.asdf/installs/ruby/3.1.0/lib/ruby/gems/3.1.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle

My original MySQL2 gem was built as follows:

gem install mysql2 -- --with-opt-dir=/opt/homebrew/opt/[email protected] –with-mysql-dir=/opt/homebrew/opt/mysql

And as far as I remember, this is where homebrew was. I tried looking for the files:

/opt/homebrew/opt/[email protected]

And the response was:

no such file or directory: /opt/homebrew/opt/[email protected]

The following:

ls -l /opt/homebrew

Produces:

ls: /opt/homebrew: No such file or directory

Which means that the OS upgrade eradicated the /opt/homebrew, or at least eradicated the Symlinks to it.

However, doing:

brew list

Produces:

==> Formulae
...  [email protected]
...  openssl@3

Digging further into it, it looks like the homebrew real folder, is at:

/usr/local/Cellar

And I found the following folders:

/usr/local/Cellar/[email protected]/1.1.1m/include
/usr/local/Cellar/[email protected]/1.1.1m/lib

Great, so off to build the MySQL2 gem again:

gem install mysql2 -- --with-opt-dir=ls -l /usr/local/Cellar/[email protected]/1.1.1m/lib --with-cppflags=-I/usr/local/Cellar/[email protected]/1.1.1m/include

And the response is:

Building native extensions with: '--with-opt-dir=ls -l /usr/local/Cellar/[email protected]/1.1.1m/lib --with-cppflags=-I/usr/local/Cellar/[email protected]/1.1.1m/include'
This could take a while...
Successfully installed mysql2-0.5.3
1 gem installed

But, when I run:

rake db:migrate

I get the exact same error I started with.

When I do:

which openssl

The response is:

/usr/bin/openssl

Which is an executable, not a folder.

I tried uninstalling the MySQL2 gem before every step, and then re-installing, and then re-bundling. Same error.

Any ideas?

Fix:

The fix that worked for me (essentially allowing homebrew to fix all the links that the Monterey update broke):

brew update --preinstall
brew install [email protected]

Then when this is done, add the following to ~/.zshrc:

export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"
export PKG_CONFIG_PATH="/usr/local/opt/[email protected]/lib/pkgconfig"

And adding the following line, as the last statement in ~/.zshrc:

export PATH="/usr/local/opt/[email protected]/bin:$PATH"

To make sure the homebrew installed [email protected] is the one to go, instead of the one installed in /usr/local/bin

Then restart your terminal, and do the following:

gem install mysql2 -- --with-opt-dir=/usr/local/opt/[email protected] –with-mysql-dir=/usr/local/opt/mysql

This should fix the problem.



Solution 1:[1]

I was having the same problem. I could solve it with the following.

% sudo gem install mysql2 -v '0.5.3' -- --with-mysql-dir=/usr/local/mysql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Felipe Almeida