mysql2 icon indicating copy to clipboard operation
mysql2 copied to clipboard

Cannot execute `rails db:create` even if installed mysql2 gem with M1 Apple Silicon

Open sukechannnn opened this issue 4 years ago • 6 comments

Overview

Hi there. I installed mysql2 to M1 Mac (Apple Silicon) & macOS Monterey.

I can install mysql2 with bellow bundler option (.bundle/config):

---
BUNDLE_BUILD__MYSQL2: "--with-ldflags="-L/opt/homebrew/lib -L/opt/homebrew/opt/openssl@3/lib" --with-cppflags="-I/opt/homebrew/include -I/opt/homebrew/opt/openssl@3/include"

but I cannot execute any rails db command like rails db:create. I get this error message consistently:

$ bin/rails db:create
rails aborted!
LoadError: dlopen(/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle, 0x0009): symbol not found in flat namespace '_mysql_affected_rows' - /Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2/mysql2.bundle
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `require'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `block in require'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:299:in `load_dependency'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/dependencies.rb:332:in `require'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/mysql2-0.5.3/lib/mysql2.rb:36:in `<top (required)>'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/config/application.rb:21:in `<top (required)>'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/Rakefile:6:in `require_relative'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/Rakefile:6:in `<top (required)>'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/railties-6.1.4/lib/rails/commands/rake/rake_command.rb:20:in `block in perform'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/railties-6.1.4/lib/rails/commands/rake/rake_command.rb:18:in `perform'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/railties-6.1.4/lib/rails/command.rb:50:in `invoke'
/Users/sukechannnn/go/src/github.com/aldagram/kanna-api/vendor/bundle/ruby/3.0.0/gems/railties-6.1.4/lib/rails/commands.rb:18:in `<top (required)>'
bin/rails:6:in `require'
bin/rails:6:in `<main>'
(See full trace by running task with --trace)

Any ideas on how to fix it? I can't understand why _mysql_affected_rows is not found. Help would be appreciated.

Environment

  • Apple M1 Pro
  • masOS Monterey 12.0.1
  • mysql Ver 8.0.27 for macos12.0 on arm64 (Homebrew)
  • ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-darwin21]
  • mysql2 0.5.3

sukechannnn avatar Dec 06 '21 09:12 sukechannnn

I'm having the same issue. It's likely due to the fact that you're running an x86_64 version of ruby. I can't switch to an arm version of ruby due to the fact that I'm also using the ruby_oci8 gem. What worked for me is to build the mysql2 gem as x86_64 (this also requires an x86_64 install of mysql, which I suspect that you do not have, since /opt/homebrew indicates arm versions).

The easiest solution is probably going to be to switch your ruby install to an arm version, but if you need x86_64, the instructions below should help.

These instructions should help you install x86_64 brew packages: https://medium.com/mkdir-awesome/how-to-install-x86-64-homebrew-packages-on-apple-m1-macbook-54ba295230f

And then this should install the mysql2 gem (you'll want to modify BUNDLE_BUILD__MYSQL2 to point to the x86_64 version of mysql in /usr/local/homebrew): arch -x86_64 gem install mysql2 -v 0.5.3

jbirdjavi avatar Dec 18 '21 16:12 jbirdjavi

I had the same issue and I could only fix this by installing mysql (arm64), ruby 2.7.3 (arm64). Then I used the following command to install mysql2 gem

bundle config build.mysql2 \
  --with-mysql-lib=/opt/homebrew/opt/mysql/lib \
  --with-mysql-dir=/opt/homebrew/opt/mysql\
  --with-mysql-config=/opt/homebrew/opt/mysql/bin/mysql_config \
  --with-mysql-include=/opt/homebrew/opt/mysql/include\
  --with-ldflags=-L/opt/homebrew/opt/openssl@3/lib\
  --with-cppflags=-I/opt/homebrew/opt/openssl@3/include

And then run gem install mysql2 -v '0.5.3' or bundle install on your rails app.

Make sure to replace mysql path (/opt/homebrew/opt/mysql) according to your local mysql path. You can get it by running brew --prefix mysql.

newx avatar Jan 19 '22 03:01 newx

I had the same issue and I could only fix this by installing mysql (arm64), ruby 2.7.3 (arm64).

Just want to say thank you @newx. Your comment helped me to solve the issue!

praagyajoshi avatar Jan 20 '22 22:01 praagyajoshi

@newx Your comment helped.

I'm on macOS Catalina on Intel (Not M1) I had slightly different config (based on my local setup) Leaving these details here for posterity

bundle config build.mysql2 \
  --with-mysql-lib=/usr/local/Cellar/[email protected]/5.7.37/lib \
  --with-mysql-dir=/usr/local/Cellar/[email protected]/5.7.37 \
  --with-mysql-config=/usr/local/Cellar/[email protected]/5.7.37/bin/mysql_config \
  --with-mysql-include=/usr/local/Cellar/[email protected]/5.7.37/include/mysql 

For me include folder has another mysql - that kept giving me Cannot find include dir(s) message.

mandarvaze avatar Feb 16 '22 14:02 mandarvaze

Please make sure do you a brew link <YOUR VERSION OF SQL>. It solved my issue

tianlaitdoc avatar Apr 21 '22 20:04 tianlaitdoc

I could install version 0.4.4 on M1 with [email protected] but it fails on Runtime. Upgraded gem to last 0.5 and it worked. I guess some default mysql homebrew paths for M1 where included in config files.

williantenfen avatar May 10 '22 14:05 williantenfen