【问题标题】:Rails & MySQL on Apple SiliconApple Silicon 上的 Rails 和 MySQL
【发布时间】:2021-08-10 08:50:25
【问题描述】:

有没有人能够通过 Apple Silicon 上的 mysql2 gem 让 Rails 与 MySQL 一起运行?我正在使用 Ruby 2.5.3 和 Rails 5.2.3,但很想听到任何版本的成功。目前我遇到了 mysql2 gem 安装失败的问题:

linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1

谢谢!

【问题讨论】:

    标签: mysql ruby-on-rails homebrew mysql2 apple-silicon


    【解决方案1】:

    经过多次尝试和搜索,我得到了一个可行的答案。首先,你必须在你的 M1 Mac 上安装两个 brew——arm 下的 homebrew 和 emulated x86 下的 homebrew (ibrew)。请参阅https://soffes.blog/homebrew-on-apple-silicon。然后,我能够在仿真下安装mysql@5.7,以及openssl。这是必需的,因为 mysql2 似乎在仿真下编译,并且正在引用这些库。

    ibrew install mysql@5.7
    ibrew install openssl
    

    然后我需要将 mysql 及其库添加到我的 PATH。

     export PATH="/opt/homebrew/bin:/usr/local/bin:/opt/homebrew/opt/mysql:/opt/homebrew/opt/mysql/lib:/usr/local/Cellar//mysql@5.7//5.7.34:/usr/local/Cellar//mysql@5.7//5.7.34/lib:/usr/local/Cellar//mysql@5.7//5.7.34/bin:$PATH"
    

    您可能需要根据版本号对其进行调整 - 当您使用它时,将其放入您的 ~/.zshrc 或其他 shell 初始化程序文件中。

    然后我可以使用这些标志安装 mysql2 gem:

     gem install mysql2 -V -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include --with-opt-dir="$(ibrew --prefix openssl)"
    

    我不是 C 编码员,所以我不完全了解 mysql2 如何链接到 mysql 库的详细信息。我更喜欢在 ARM 下使用可用的 mysql,期望它必须运行得更快,但不知道如何让它连接起来。

    【讨论】:

      【解决方案2】:

      我在以下方面取得了成功:

      rbenv exec gem install mysql2 -- \
      --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
      --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
      --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
      --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include 
      

      (请注意,您可能需要更改这些路径中的版本号)

      值得注意的是,这适用于最新版本的 mysql,不需要任何 Intel 版本或使用 HomeBrew 的模拟版本(例如ibrew)。

      配置 Bundler 以自动使用此构建配置:

      您可能还想将此配置设置为mysql2 的默认配置。这样,只要 bundler 必须重新安装 mysql2(在此项目或同一台计算机上的任何其他项目上),它将自动使用此配置。您可以通过以下方式做到这一点:

      bundle config set --global build.mysql2 \
      --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.25_1/lib \
      --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.25_1 \
      --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.25_1/bin/mysql_config \
      --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.25_1/include 
      

      【讨论】:

      • 就是这个。将8.0.25_1 更改为8.0.26 用于我们安装的msyql 的特定版本。但其余的工作得很好。专业提示:我实际上会将 Bundler 配置为全局使用此配置,以便在您需要重新安装 gem 时,它会自动使用此配置。我已经用这种方法修改了你的答案。
      【解决方案3】:

      根据@matthias-winkelmann 的回答,使用brew 为您找到正确的mysql 版本:

      gem install mysql2 -v '0.5.3' -- \
      --with-mysql-lib=$(brew --prefix mysql)/lib \
      --with-mysql-dir=$(brew --prefix mysql) \
      --with-mysql-config=$(brew --prefix mysql)/bin/mysql_config \
      --with-mysql-include=$(brew --prefix mysql)/include 
      

      请注意我在这里提到的mysql2 gem 的具体版本。

      【讨论】:

      • 这在全新的 M1 Pro mac 上对我有用!新鲜的 Homebrew 安装。冲泡安装rbenv。 rbenv 安装 3.1.0。冲泡安装mysql。冲泡安装openssl。冲泡安装 zstd。然后是上面的:-),然后是通常的鬼鬼祟祟
      【解决方案4】:

      我正在使用带有 M1-14inc 的 Montery12.1

      使用 brew 安装环境

      brew install openssl
      brew install zstd
      

      导出环境数据

      export LDFLAGS="-L/usr/local/opt/openssl/lib"
      export CPPFLAGS="-I/usr/local/opt/openssl/include"
      export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/
      export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix openssl@1.1)/lib/
      

      安装mysql

      gem install mysql2 -v '0.5.3' \
        --source 'https://rubygems.org/' -- \
        --with-cppflags=-I/usr/local/opt/openssl/include \
        --with-ldflags=-L/usr/local/opt/openssl/lib
      

      我遇到了这样的错误 我试过了,效果很好:

      gem install mysql2 -v 0.5.3 -- --srcdir=/usr/local/mysql/include
      

      【讨论】:

      • 这是为我做的,干杯
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 1970-01-01
      • 2021-05-11
      • 2021-09-28
      • 2021-03-02
      • 2022-01-10
      相关资源
      最近更新 更多