Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sukechannnn opened this issue Dec 6, 2021 · 6 comments

Comments

@sukechannnn
Copy link

sukechannnn commented Dec 6, 2021

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
@jbirdjavi
Copy link

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

@newx
Copy link

newx commented Jan 19, 2022

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.

@praagyajoshi
Copy link

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!

@mandarvaze
Copy link

mandarvaze commented Feb 16, 2022

@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/mysql@5.7/5.7.37/lib \
  --with-mysql-dir=/usr/local/Cellar/mysql@5.7/5.7.37 \
  --with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.37/bin/mysql_config \
  --with-mysql-include=/usr/local/Cellar/mysql@5.7/5.7.37/include/mysql 

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

@tianlaitdoc
Copy link

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

@williantenfen
Copy link

I could install version 0.4.4 on M1 with mysql@5.7 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants