简体   繁体   English

在 M1 mac 上创建 mysql 数据库时出现问题:在平面命名空间 '_mysql_affected_rows 中找不到符号

[英]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 mysql@5.7 which was installed via homebrew.我正在尝试使用通过自制软件安装的 mysql@5.7 为我的 rails 项目创建一个数据库。 The installation was succesfull, but when I try to run: bin/rake db:create .安装成功,但是当我尝试运行时: 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我的mysql2版本是0.4.9

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:我不是 100% 肯定我所说的一件事是准确的,但我已经能够通过执行以下操作来运行 Ruby 和 mysql2 的多个版本:

1. Make sure you're running homebrew 3.0.0 (supports Apple silicon) 1. 确保您运行的是 homebrew 3.0.0(支持 Apple 芯片)

2. I used rbenv as my version manager and installed via homebrew 2. 我使用 rbenv 作为我的版本管理器并通过 homebrew 安装

brew install rbenv

3. Install some libraries we are going to need for the mysql2 gem 3. 安装 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.我提前这样做了,所以我可以为所有其他命令使用 brew 前缀。 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.请注意,在 M1 上,homebrew 安装的默认位置已从/usr/local/opt/更改为/opt/homebrew/opt/因此,如果您不使用 brew 前缀并且您正在尝试其他解决方案的组合,请检查旧/user/local/opt/路径的命令并将其换成新路径,否则如果没有一些手动配置,它将无法工作。

4. Ruby installation 4. Ruby安装

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. CFLAGs 参数不会破坏编译器警告的构建(仍然会破坏错误),而 readline 参数为我们提供了我们刚刚使用自制软件安装的版本。
  • I have only had success with this command for Rubies as recent as 2.4.5.我只在最近 2.4.5 的红宝石上使用这个命令取得了成功。 I am still working on 1.9.3p392 (old project, not my fault).我仍在研究 1.9.3p392(旧项目,不是我的错)。

5. mysql2 gem install 5. mysql2 gem 安装

Once you've installed Ruby, switch to that version or the project you're working on and run bundle install .安装 Ruby 后,切换到该版本或您正在处理的项目并运行bundle install You will likely get an error about mysql2 failing and asking you to make sure the gem is installed before bundling again.您可能会收到有关 mysql2 失败的错误,并要求您确保在再次捆绑之前安装了 gem。

I resolved that with:我解决了这个问题:

gem install mysql2 -v '0.5.3' -- --with-mysql-config=$(brew --prefix mysql@5.7)/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.基本上我们在这里所做的是使用自制软件安装的 mysql5.7 的配置,并为 zstd 和 openssl 设置 ld 和 cpp 标志。

These are the steps I took to get the gem installed and working correctly.这些是我为安装 gem 并正常工作所采取的步骤。 Did my best for my first SO post but please let me know if I need to do anything different here or next time.我为我的第一个 SO 帖子尽了最大努力,但如果我需要在这里或下次做任何不同的事情,请告诉我。

Edits:编辑:

For Ruby 2.4.5 I needed a slightly different command for the mysql2 gem:对于 Ruby 2.4.5,我需要一个稍微不同的 mysql2 gem 命令:

rbenv exec gem install mysql2 -- --with-mysql-lib=$(brew --prefix mysql@5.7)/lib --with-mysql-dir=$(brew --prefix mysql@5.7) --with-mysql-config=$(brew --prefix mysql@5.7)/bin/mysql_config --with-mysql-include=$(brew --prefix mysql@5.7)/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.在那之后,我也遇到了 ffi gem(版本早于 1.14.2)的错误,这阻止了我再次运行db:create 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.我还没有找到解决这个问题的真正解决方案(对我有用)(那里有很多帖子),并且由于时间限制,我只是编辑了 Gemfile.lock 以使用 ffi 版本 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.不知道这会给我带来什么样的麻烦(不检查),但需要快速“修复”这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM