无法安装mysql2 gem用于Rails项目

26

我在Stackoverflow.com上查看并尝试了几乎所有与此主题相关的内容,但仍然无法解决...

当我运行'gem install mysql2'时,会出现权限错误。当我运行'sudo gem install mysql2'时,会出现以下错误:

    Teds-MacBook-Pro:~ tedmartin$ sudo gem install mysql2
Building native extensions.  This could take a while...
ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
/Users/tedmartin/.rbenv/versions/2.4.1/bin/ruby -r ./siteconf20170420-50202-1nekuvd.rb extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
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
checking for rb_big_cmp()... yes
-----
Using mysql_config at /usr/local/bin/mysql_config
-----
checking for mysql.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in 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-connector-c/6.1.9/lib
-----
creating Makefile

current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/ext/mysql2
make "DESTDIR=" clean

current directory: /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5/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 -l-lpthread
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/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/mysql2-0.4.5 for inspection.
Results logged to /Users/tedmartin/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/extensions/x86_64-darwin-15/2.4.0-static/mysql2-0.4.5/gem_make.out

任何有想法、建议、技巧、修复等相关内容的人,我都非常感激。

谢谢!


不确定为什么有人给你点了踩.. 你是否首先在你的Mac上安装了mysql?与其他gems不同,它不仅仅是bundle install的情况,你可以通过从终端运行mysql -u root并查看它是否识别该命令来检查。如果没有,请转到mysql网站并按照OSX的设置说明进行设置。 - Mark
谢谢 Mike。我已经使用 Homebrew 和下载社区版安装了 mysql... 我也不知道为什么会有人对我投反对票... 哦,匿名网络的乐趣 :) - Ted Martin
这就是生活!当您尝试从终端运行mysql -u root时会发生什么?它是否识别该命令? - Mark
请查看@TedMartin的这个链接 - Sebastián Palma
6个回答

43

根据这个问题上的回答,对我而言,执行以下命令可行:

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


1
这是一行代码解决方案的最佳答案。 - Hanmaslah

41
我在Mojave上遇到了同样的问题,之前的答案都没有解决我的问题,但是这个Github问题中提到的命令comment确实有效。
步骤1。
brew install openssl

第二步。

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

在我的情况下,我已经安装了 openssl,所以真正解决问题的是导出命令。


5
这也是我的问题。我已经安装了openssl,但需要进行导出。谢谢! - Tony Gaeta

20
  1. 确保在Mac上通过Homebrew安装了openssl。
brew install openssl
  1. 安装mysql2 gem。
gem install mysql2 -v '0.5.2' -- --with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include
  1. 如果Bug没有被修复,以上解决方案将是最后的选择。
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

如果上述方法无效,另一个解决方案是:

cd /usr/local/Cellar/openssl/1.0.2s/lib/

sudo cp libssl.1.0.0.dylib libcrypto.1.0.0.dylib /usr/local/lib

1
只有最后一个解决方案对我有效。而且还应该复制libssl.a、libcrypto.a。第二个解决方案有助于运行gem install,但是接下来的bundle install仍然失败。 - Neo Li

15

以下方法适用于我在macOS Catalina上的操作:

确保您已安装openssl。如果没有,请执行以下步骤:

brew install openssl

然后执行gem install:

gem install mysql2 -- --with-opt-dir="$(brew --prefix openssl)"

1

试试这个:

which mysql

然后,使用输出结果来安装gem,使用现有的mysql安装目录:

gem install mysql2 -- --with-mysql-dir=<mysql-installation-directory>

0

2小时疯狂尝试后,终于解决了问题

  1. 卸载 Ruby 并安装 Ruby v2.6.6
  2. 以管理员身份运行命令行 gem install mysql2
  3. 安装 Rails gem install rails
  4. 创建新项目 rails new my_proj -d mysql

在 Windows 上百分之百工作


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