【问题标题】:Can't install mysql2 gem on macOS Sierra无法在 macOS Sierra 上安装 mysql2 gem
【发布时间】:2017-01-29 18:36:29
【问题描述】:

我正在新的 ma​​cOS 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

【问题讨论】:

标签: mysql ruby macos rubygems macos-sierra


【解决方案1】:

我只是遇到了同样的问题,尝试了上面列出的所有解决方案,然后开始用头敲打他们的键盘几个小时。

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

xcode-select --install

一旦我这样做了,mysql2 gem 安装就没有问题了。我希望这能解决问题!

【讨论】:

  • 我在更新到 MacOS Sierra 10.12.1 后遇到了同样的问题,谢谢!
  • 执行此操作后,我得到了一个不同的错误:Permission denied @ rb_sysopen。使用 Sudo 解决了这个问题:sudo bundle install。不是一个理想的解决方案,但嘿,它奏效了!
  • 在我的 10.12.4 上只是说xcode-select: error: command line tools are already installed, use "Software Update" to install updates。还有其他方法可以强制重新安装吗?
  • @NicCottrell 运行 brew doctor 时,我看到此消息警告:有更新的命令行工具版本可用。从系统偏好设置中的软件更新更新它们或运行:softwareupdate --all --install --force 如果没有显示更新运行:sudo rm -rf /Library/Developer/CommandLineTools sudo xcode-select --install
【解决方案2】:

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

Apple 已弃用 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's documentation

【讨论】:

  • 这对我有用。它给了我“零构建”警告。我在下一行尝试了 bundle install ,它对我有用。谢谢大佬。
  • 是的,工作,谢谢你让我免于敲打我的头几个小时
  • 感谢上帝...亚历山德罗,Lady Gaga 应该为你写一首歌
  • 我支持 ^^
  • 命令后响应:You are replacing the current local value of build.mysql2, which is currently nil 运行 bundle install 在此工作后
【解决方案3】:

很多很棒的答案,我能够将它们组合成这样:

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
【解决方案4】:

我在这里分享我的修复,因为其他答案不起作用。

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

brew install mysql56 而不是brew install mysql

安装 mysql2 gem 的捆绑包一直失败,直到:

brew link mysql56

我后来也跑了:

mysql.server start

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

【讨论】:

  • 对 Percona 也有同样的效果,虽然我不得不强迫它:brew link --force percona-server56
【解决方案5】:

使用 Mac OS 10.15 Catalina,当我尝试 Alessandro 的修复时,可以正确安装 gem 和扩展,但 bundle install 失败。起作用的只是:

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

没有cppflags 部分。

【讨论】:

  • 线索在mkmf.log中提供:clang: error: unsupported option '--with-cppflags=-I/usr/local/opt/openssl/include'
【解决方案6】:

这对我有用。

原来我跑了:

$ 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:错误:不支持的选项 '--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。
【解决方案7】:

与@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 并且对我有用。
【解决方案8】:

尝试安装 xcode-select --install

【讨论】:

  • 此解决方案有时会起作用。 10.14 之后是指 App Store。
【解决方案9】:

所以我遇到了这个类似的问题,对我来说这是一个错误的 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 并立即启动。希望这对某人有所帮助。

【讨论】:

    【解决方案10】:

    如果上述方法都不起作用..就像我的情况一样,这样做可以解决问题 brew install openssl

    仅供参考:我正在使用 MacOS Catalina

    【讨论】:

      【解决方案11】:

      所以我在这里尝试了一切都无济于事。似乎是 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"

      【讨论】:

        【解决方案12】:

        首先,您应该在here 中尝试 2 个答案 如果您安装了 openssl 但它仍然无法正常工作。您应该尝试刷新 gems 参考。 A 遇到了同样的问题,它对我有用。

        gem source -r https://rubygems.org/

        gem source -a https://rubygems.org/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-02-03
          • 2021-11-15
          • 2021-12-23
          • 2015-04-25
          • 2017-04-19
          • 2017-06-16
          • 2011-08-16
          相关资源
          最近更新 更多