无法在macOS Sierra上安装mysql2 gem

53

我正在为新的 macOS Sierra 设置开发环境。

首先,我安装了RbenvRuby (2.3.1)Homebrew和最新版本的MySQL (5.7.15)

$ brew install mysql
$ mysql.server start

好的,MySQL已初始化。现在是安装mysql2 gem的时候了...

$ gem install mysql2 -- --with-mysql-config=/usr/local/Cellar/mysql/5.7.15/bin/mysql_config

但是它没有起作用。


Building native extensions with: '--with-mysql-config=/usr/local/Cellar/mysql/5.7.15/bin/mysql_config'
This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/ext/mysql2
/Users/macuser/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20160921-16853-x1boio.rb extconf.rb --with-mysql-config=/usr/local/Cellar/mysql/5.7.15/bin/mysql_config
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... no
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/Cellar/mysql/5.7.15/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for mysqld_error.h... yes
-----
Dont 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.7.15/lib
-----
creating Makefile

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.4.4/mkmf.log

current directory: /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4/ext/mysql2
make "DESTDIR="
compiling client.c
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
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

make failed, exit code 2

Gem files will remain installed in /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/mysql2-0.4.4 for inspection.
Results logged to /Users/macuser/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/extensions/x86_64-darwin-16/2.3.0-static/mysql2-0.4.4/gem_make.out

我想知道这是否回答了你的问题:https://dev59.com/jF0Z5IYBdhLWcg3w_0d8 - spickermann
谢谢,@spickermann。但是这对我没有帮助,在“brew install openssl”之后结果仍然相同。 - Caio Tarifa
2
你是否也尝试过 brew reinstall openssl && brew link openssl --force - spickermann
@spickermann 这对我有效! - Tony Hsieh
https://qiita.com/tktcorporation/items/0ef8c930fc18ce72c301 - zx1986
12个回答

103

我遇到了同样的问题,尝试了上面列出的所有解决方案,然后开始在键盘上猛击几个小时。

然后我想尝试安装/重新安装Xcode命令行工具:

xcode-select --install

我这样做后,mysql2 gem 顺利安装了。希望这个解决方案有效!


1
我在升级到MacOS Sierra 10.12.1后遇到了同样的问题。谢谢! - arthur bryant
之后我遇到了一个不同的错误:'Permission denied @ rb_sysopen'。通过使用Sudo,该问题得以解决:'sudo bundle install'。这不是一个理想的解决方案,但它能起作用! - Qasim
9
在我的10.12.4版本中,仅显示xcode-select:error:command line tools are already installed,use“Software Update”to install updates。有没有其他方法来强制重新安装? - Nic Cottrell
@NicCottrell 当运行brew doctor时,我看到了这个消息警告:有一个更新的命令行工具版本可用。 从“系统偏好设置”中的“软件更新”更新它们或运行:   softwareupdate --all --install --force如果这不显示更新,请运行:   sudo rm -rf /Library/Developer/CommandLineTools   sudo xcode-select --install - Fralcon

68

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

苹果已不推荐使用OpenSSL库,而是使用自己的TLS和加密库

一般情况下,这对您没有影响。如果您构建自己的软件并且需要此公式,则需要向构建变量中添加:

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的文档


1
这对我有用。它给了我“nil build”警告。我尝试在下一行运行bundle install,它对我起作用了。谢谢啊。 - Naveen Honest Raj K
2
谢天谢地... Alessandro,Lady Gaga应该为你写一首歌。 - Tallboy
我赞同 ^^ - Amituuush
2
执行命令后的响应:您正在替换当前本地值为nil的build.mysql2 在此之后运行bundle install即可。 - Sathish
这对我有用,尽管我不得不将路径更改为 /usr/local/Cellar/openssl@3/3.0.0_1。 - Brad
显示剩余3条评论

23

很多很棒的回答,我能够将它们结合起来:

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

因为我对bundle config不太熟悉。


谢谢!我也不熟悉 bundle config - MIA

12

其他回答没有生效,我来分享我的解决方法。

因为我的环境需要使用 MySQL 5.6,所以我不得不使用:

brew install mysql56 而不是 brew install mysql

安装 mysql2 gem 时一直失败,直到执行:

brew link mysql56

我之后还运行了以下命令:

mysql.server start

最后一步可能不必要,但是以防万一


同样的方法适用于Percona,尽管我必须强制执行它:brew link --force percona-server56 - Cory Ringdahl

9

在Mac OS 10.15 Catalina上,当我尝试了Alessandro的修复方法时,宝石和扩展程序可以正确安装,但是bundle install失败了。有效的解决方法只是:

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

没有 cppflags 部分。

1
线索在mkmf.log中提供: clang: error: 不支持选项 '--with-cppflags=-I/usr/local/opt/openssl/include' - David Cook

5

Here's what worked for me.

Originally I ran:

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

那么

$ bundle install

我在 /Users/.../.bundle/ruby/2.5.0/extensions/x86_64-darwin-18/2.5.0/mysql2-0.5.3/mkmf.log 收到了一个错误:

clang: error: 不支持的选项 '--with-cppflags=-I/usr/local/opt/openssl/include'

因此我删除了 "--with-cppflags=-I/usr/local/opt/openssl/include"

然后运行了以下命令:

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

接下来:

$ bundle install

这个起作用了。


这对我帮助的一个使用Sierra的人有效。然而,我的电脑是在Mojave上运行的,包括cppflags也可以工作。 - thekingoftruth
在重新运行没有 cppflags 的 bundle config 之前,我不得不删除 .bundle/config,但这个解决方案起作用了。谢谢。 - Minimul

2
几乎和@Caio Tarifa相同的情况,使用Ruby 2.3.3,mysql 5.6和mysql2。尝试了上面几个解决方案,最终采用@kylekeesling的方法使其工作。

首先,尝试了@spickermann的解决方案1:

brew reinstall openssl && brew link openssl --force

什么都没有发生,显示相同的错误。

其次,尝试了@Alessandro Berardi提供的解决方案:

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

由于它覆盖了gem扩展的配置,因此所有gem扩展安装都失败了,因此出现了不同但更多的错误。

最后,尝试了@kylekeesling的解决方案:

xcode-select --install

它修复了mysql gem和nikogiri的问题。由于我已经安装了Xcode,在我的情况下,需要重新安装Xcode命令行工具。


我也运行了命令 xcode-select --install,并且它对我起作用了。 - Ray Hunter

1

我遇到了类似的问题,原因是使用了错误的 Ruby 版本和不兼容的 MySQL 版本。我的大多数项目都使用 Ruby 2.3,但我继承了一个 2.1 项目。将 rvm 更改为使用 2.1 可以让我走得更远。

然后我发现了这个链接:https://github.com/brianmario/mysql2/issues/603 它说你必须使用 mysql2 gem 版本大于 0.3.17 与 MySQL 版本 5.7 一起使用。

将 gem 更新到 0.3.17 就可以正常启动了。希望这能帮助到某些人。


1
尝试安装 xcode-select --install。

1
这个解决方案有时候有效。10.14版本后,它会引用App Store。 - sergey.radov

0

我尝试了所有的方法,但都没有用。看起来是与 Ruby 2.6.0 有关的问题,我降级到了 2.3.4p301,一切正常(使用 bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接