'Problem when creating mysql database on M1 mac: symbol not found in flat namespace '_mysql_affected_rows
I'm trying to create a db for my rails project using [email protected] which was installed via homebrew.
The installation was succesfull, but when I try to run: bin/rake db:create
. The error occurred:
LoadError: dlopen(/Users/matthewluong/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/mysql2-0.4.9/lib/mysql2/mysql2.bundle, 0x0009):
symbol not found in flat namespace '_mysql_affected_rows' - /Users/matthewluong/.rbenv/versions/2.7.4/lib/ruby/gems/2.7.0/gems/mysql2-0.4.9/lib/mysql2/mysql2.bundle
lib/tasks/db_config.rake:26:in `block in <top (required)>'
My mysql2 version is 0.4.9
Solution 1:[1]
I have been banging my head against this all day and this is how I resolved the issue. I am not 100% positive that a single thing I'm saying is accurate, but I have been able to get multiple versions of Ruby and mysql2 running by doing the following:
1. Make sure you're running homebrew 3.0.0 (supports Apple silicon)
2. I used rbenv as my version manager and installed via homebrew
brew install rbenv
3. Install some libraries we are going to need for the mysql2 gem
brew install mysql openssl zstd readline
I'm doing this ahead of time so I can use brew prefixes for all my other commands. Note that on M1's, the default location for homebrew installs has changed from /usr/local/opt/
to /opt/homebrew/opt/
so, if you aren't using the brew prefixes and you are trying combinations of other solutions, check the commands for the older /user/local/opt/
path and swap it out for the new one or else it isn't going to work without some manual configuration.
4. Ruby installation
CFLAGS="-Wno-error=implicit-function-declaration" RUBY_CONFIGURE_OPTS='--with-readline-dir=/opt/homebrew/opt/readline' rbenv install 2.7.4
- The CFLAGs argument is to not break the build on compiler warnings (still breaks on errors) and the readline argument gets us the version we just installed with homebrew.
- I have only had success with this command for Rubies as recent as 2.4.5. I am still working on 1.9.3p392 (old project, not my fault).
5. mysql2 gem install
Once you've installed Ruby, switch to that version or the project you're working on and run bundle install
. You will likely get an error about mysql2 failing and asking you to make sure the gem is installed before bundling again.
I resolved that with:
gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix [email protected])/bin/mysql_config --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include
- Basically what we are doing here is we are using the config for mysql5.7 as installed by homebrew, and setting ld and cpp flags for zstd and openssl.
These are the steps I took to get the gem installed and working correctly. Did my best for my first SO post but please let me know if I need to do anything different here or next time.
Edits:
For Ruby 2.4.5 I needed a slightly different command for the mysql2 gem:
rbenv exec gem install mysql2 -- --with-mysql-lib=$(brew --prefix [email protected])/lib --with-mysql-dir=$(brew --prefix [email protected]) --with-mysql-config=$(brew --prefix [email protected])/bin/mysql_config --with-mysql-include=$(brew --prefix [email protected])/include --with-ldflags="-L$(brew --prefix zstd)/lib -L$(brew --prefix openssl)/lib" --with-cppflags=-I$(brew --prefix openssl)/include
I also was running into errors with the ffi gem (versions older than 1.14.2) after that which was preventing me from running db:create
again. I have not found a real solution (that will work for me) to this problem yet (there are lots of posts out there) and, due to time constraints, just edited the Gemfile.lock to use ffi version 1.15.5. No idea what kind of trouble thats going to cause me (not checking it in) but needed to get this "fixed" quick.
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 |