尝试使用 mysql2 gem 安装应用程序时出错

Error when trying to install app with mysql2 gem

我正在尝试安装使用 mysql2 gem 的开源 rails 3.2.21 应用程序,但是当我尝试 运行 bundle 命令我收到以下错误:

Fetching: mysql2-0.3.18.gem (100%)
Building native extensions.  This could take a while...
p
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /Users/my_username/.rvm/rubies/ruby-2.1.2/bin/ruby -r ./siteconf20150614-72129-orqsb7.rb extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/Cellar/mysql/5.6.25/lib
-----
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
linking shared-object mysql2/mysql2.bundle
ld: warning: directory not found for option '-L/Users/travis/.sm/pkg/active/lib'
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

make failed, exit code 2

Gem files will remain installed in /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/gems/mysql2-0.3.18 for inspection.
Results logged to /Users/my_username/.rvm/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/extensions/x86

我尝试卸载每个版本的 mysql 我通过自制软件安装并重新安装它们,如下所示:

brew uninstall --force mysql && brew install mysql

然后运行宁:

sudo gem install mysql2

正如此处提出的许多类似问题所建议的那样,但它仍然会导致与上述相同的错误。

有人可以提供有关如何启动和 运行ning 的指导吗?

错误日志显示:

ld: library not found for -lssl

所以,你需要安装libssl:

brew install openssl

正如评论中指出的那样,可能需要导出库的路径。

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

感谢@mudasobwa 为我指明了正确的方向。事实证明错误是由未链接的 openssl 文件引起的,所以 运行:

brew reinstall openssl && brew link openssl --force 

问题解决了。我在这里找到了解决方案:OpenSSL, RVM, Brew, conflicting error

对于仍然遇到问题的任何人:

当您通过 brew 安装 openssl 时,您应该收到以下消息:

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your own software and it requires this formula, you'll need to add to your build variables:

LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig

您可以通过运行以下设置这些构建标志(对于本地应用程序):

bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"

这对我有用。

有关详细信息,请参阅 bundler's documentation

我的解决方案是安装 Xcode 命令行工具。

我最近通过 Mac App Store 更新了 Xcode,每次更新时,我发现我必须重新安装命令行工具。

xcode-select --install
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

从这里开始: https://gorails.com/setup/osx/10.14-mojave

我发现我必须使用 --with-opt-dir=/usr/local/opt

具体来说,我将以下内容添加到我的 ~/.bundle/config 文件中:

BUNDLE_BUILD__MYSQL2: "--with-opt-dir=/usr/local/opt"

试试这个:

gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include

(适当更新版本)

基于解决方案here

brew install openssl

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

解决了问题。

命令组合解决了我的问题。我在莫哈韦沙漠。

brew reinstall openssl && brew link openssl --force

然后是

gem install mysql2 -v '0.4.10' -- \
  --with-ldflags=-L/usr/local/opt/openssl/lib \
  --with-cppflags=-I/usr/local/opt/openssl/include

您似乎错过了构建 mysql2 gem

所需的主要文件
sudo apt-get install libsqlite3-dev libmysqlclient-dev -y

libsqlite3-dev 不是强制性的,但安装它,因为它是默认的 rails 数据库。

Homebrew 更新后 (openssl@1.1) 有一个新的库路径,因此可以使用:

bundle config build.mysql2 --with-opt-dir=$(brew --prefix openssl)
bundle install

它将修复 ld: library not found for -lssl error

在 MacBook air M1(macOS) 上它对我有用。

安装 zstd

brew install zstd

安装mysql2

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib

Mac Catalina 使用 Homebrew 修复: gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"

我在蒙特雷 M1 上的步骤 Mac

brew install openssl@3

brew install zstd

gem install mysql2 -v '0.5.3' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib

bundle config --local build.mysql2 "--with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L/opt/homebrew/Cellar/zstd/1.5.0/lib"

bundle install

以下命令适用于我的 Mac os 12.1 MacOs Monterey

 gem install mysql2 -v '0.5.3' -- \
 --with-mysql-lib=/opt/homebrew/Cellar/mysql/8.0.28/lib \
 --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28 \
 --with-mysql-config=/opt/homebrew/Cellar/mysql/8.0.28/bin/mysql_config \
 --with-mysql-include=/opt/homebrew/Cellar/mysql/8.0.28/include 

请参阅此link了解更多详情 https://github.com/brianmario/mysql2/issues/1175

我终于在 macOS Monterey 12.3 (M1 Pro) 上工作了:

 gem install mysql2 -- --with-mysql-dir=/opt/homebrew/Cellar/mysql/8.0.28_1

请务必阅读 installation instructions。对我来说值得注意的是:

  • 确保 MySQL 已安装 (brew install mysql)
  • 确保 XCode select 工具已安装 (xcode-select --install)
  • with-mysql-dir 选项设置为安装 mysql 的位置(检查 brew info mysql